48#define WSIZE (8 * sizeof(int))
50#define bexchange(a, b, l) \
52 char *t = (char *)malloc(l * sizeof(char)); \
53 memcpy((t), (char *)(a), (int)(l)); \
54 memcpy((char *)(a), (char *)(b), (int)(l)); \
55 memcpy((char *)(b), (t), (int)(l)); \
59#define exchange(a, b, t) \
70void errormsg1(
const char *f,
const char *msgname,
const char *msg);
98 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
103 if (rows == 0 || cols == 0) {
107 result->
p = q = (
int **)malloc(rows *
sizeof(
int *));
109 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
112 result->
p_init = p = (
int *)malloc(rows * cols *
sizeof(
int));
114 errormsg1(
"SMAlloc",
"outofmem",
"out of memory space");
117 for (i = 0; i < rows; i++) {
148 unsigned NbRows, NbColumns;
152 for (i = 0; i < NbRows; i++) {
154 for (j = 0; j < NbColumns; j++)
155 fprintf(stderr,
" %10X ", *p++);
156 fprintf(stderr,
"\n");
165 int *cp1, *cp2, *cp3;
171 for (i = 0; i < length; i++) {
182#define SMVector_Copy(p1, p2, length) \
183 memcpy((char *)(p2), (char *)(p1), (int)((length) * sizeof(int)))
188#define SMVector_Init(p1, length) \
189 memset((char *)(p1), 0, (int)((length) * sizeof(int)))
201static void Combine(Value *p1, Value *p2, Value *p3,
int pos,
unsigned length) {
204 Value abs_a1, abs_a2, neg_a1;
259 int i, j, sat_nbcolumns;
260 unsigned jx1, jx2, bx1, bx2;
264 sat_nbcolumns = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
271 for (i = 0, jx1 = 0, bx1 =
MSB; i < Ray->
NbRows; i++) {
272 for (j = 0, jx2 = 0, bx2 =
MSB; j < Mat->
NbRows; j++) {
273 if (
Sat->
p[j][jx1] & bx1)
274 result->
p[i][jx2] |= bx2;
297 int *equal_bound,
int *sup_bound,
unsigned RowSize1,
298 unsigned RowSize2,
unsigned bx,
unsigned jx) {
300 Value **uni_eq, **uni_sup, **uni_inf;
301 int **inc_eq, **inc_sup, **inc_inf;
312 *sup_bound = *equal_bound = NbBid;
313 uni_sup = uni_eq = Ray->
p + NbBid;
314 inc_sup = inc_eq =
Sat->
p + NbBid;
316 uni_inf = Ray->
p + NbRay;
317 inc_inf =
Sat->
p + NbRay;
319 while (inf_bound > *sup_bound) {
321 if (inc_eq != inc_sup) {
332 *((*inc_sup) + jx) |= bx;
339 if (inc_inf != inc_sup) {
355 cols = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
357 Sat->
p = (
int **)realloc(
Sat->
p, rows *
sizeof(
int *));
359 errormsg1(
"SatMatrix_Extend",
"outofmem",
"out of memory space");
364 errormsg1(
"SatMatrix_Extend",
"outofmem",
"out of memory space");
367 for (i = 0; i < rows; ++i)
384 unsigned NbMaxRays,
unsigned FirstConstraint,
386 unsigned NbRay, Dimension, NbConstraints, RowSize1, RowSize2, sat_nbcolumns;
387 int sup_bound, equal_bound, index_non_zero, bound;
388 int i, j, k, l, redundant, rayonly, nbcommonconstraints;
395 fprintf(stderr,
"[Chernikova: Input]\nRay = ");
397 fprintf(stderr,
"\nConstraints = ");
399 fprintf(stderr,
"\nSat = ");
403 NbConstraints = Mat->
NbRows;
408 RowSize1 = (Dimension + 1);
409 RowSize2 = sat_nbcolumns *
sizeof(int);
411 Temp = (
int *)malloc(RowSize2);
413 errormsg1(
"Chernikova",
"outofmem",
"out of memory space");
426 jx = FirstConstraint /
WSIZE;
428 bx >>= FirstConstraint %
WSIZE;
429 for (k = FirstConstraint; k < NbConstraints; k++) {
436 index_non_zero = NbRay;
437 for (i = 0; i < NbRay; i++) {
446 for (j = 1; j < Dimension; j++) {
457 fprintf(stderr,
"[Chernikova: A]\nRay = ");
459 fprintf(stderr,
"\nConstraints = ");
461 fprintf(stderr,
"\nSat = ");
466 if (index_non_zero < NbBid) {
469 if (NbBid != index_non_zero)
473 fprintf(stderr,
"************\n");
474 for (i = 0; i < RowSize1; i++) {
477 fprintf(stderr,
"\n******\n");
478 for (i = 0; i < RowSize1; i++) {
481 fprintf(stderr,
"\n*******\n");
485 for (i = 0; i < NbBid; i++)
487 Combine(Ray->
p[i], Ray->
p[NbBid], Ray->
p[i], 0, Dimension);
494 for (j = 0; j < Dimension + 1; j++) {
502 fprintf(stderr,
"[Chernikova: B]\nRay = ");
505 fprintf(stderr,
"\nConstraints = ");
507 fprintf(stderr,
"\nSat = ");
512 for (i = NbBid + 1; i < NbRay; i++)
514 Combine(Ray->
p[i], Ray->
p[NbBid], Ray->
p[i], 0, Dimension);
518 for (j = 0; j < sat_nbcolumns; j++) {
519 Sat->
p[NbBid][j] = 0;
522 Sat->
p[NbBid][jx] |= bx;
524 if (--NbRay != NbBid) {
531 fprintf(stderr,
"[Chernikova: C]\nRay = ");
534 fprintf(stderr,
"\nConstraints = ");
536 fprintf(stderr,
"\nSat = ");
541 RaySort(Ray,
Sat, NbBid, NbRay, &equal_bound, &sup_bound, RowSize1,
555 fprintf(stderr,
"[Chernikova: D]\nRay = ");
558 fprintf(stderr,
"\nConstraints = ");
560 fprintf(stderr,
"\nSat = ");
566 for (i = equal_bound; i < sup_bound;
568 for (j = sup_bound; j < bound; j++) {
575 nbcommonconstraints = 0;
576 for (l = 0; l < jx; l++) {
577 aux = Temp[l] =
Sat->
p[i][l] |
Sat->
p[j][l];
578 for (
m =
MSB;
m != 0;
m >>= 1)
580 nbcommonconstraints++;
582 aux = Temp[jx] =
Sat->
p[i][jx] |
Sat->
p[j][jx];
583 for (
m =
MSB;
m != bx;
m >>= 1)
585 nbcommonconstraints++;
589 nbcommonconstraints++;
594 if (nbcommonconstraints + NbBid >=
598 for (
m = NbBid;
m < bound;
m++)
599 if ((
m != i) && (
m != j)) {
611 for (l = 0; l <= jx; l++, ip2++, ip1++)
621 fprintf(stderr,
"[Chernikova: E]\nRay = ");
624 fprintf(stderr,
"\nConstraints = ");
626 fprintf(stderr,
"\nSat = ");
635 if (NbRay == NbMaxRays) {
643 Combine(Ray->
p[j], Ray->
p[i], Ray->
p[NbRay], 0, Dimension);
646 Sat->
p[NbRay][jx] &= ~bx;
661 sup_bound, equal_bound, bound, NbRay, Dimension);
665 fprintf(stderr,
"[Chernikova: F]:\nRay = ");
676 fprintf(stderr,
"i = %d\nj = %d \n", i, j);
678 while ((j < bound) && (i > bound)) {
686 fprintf(stderr,
"i = %d\nj = %d \n", i, j);
695 sup_bound, equal_bound, bound, NbRay, Dimension);
699 fprintf(stderr,
"[Chernikova: G]\nRay = ");
718 fprintf(stderr,
"[Chernikova: Output]\nRay = ");
720 fprintf(stderr,
"\nConstraints = ");
722 fprintf(stderr,
"\nSat = ");
729static int Gauss4(Value **p,
int NbEq,
int NbRows,
int Dimension) {
730 int i, j, k, pivot, Rank;
731 int *column_index = NULL;
735 column_index = (
int *)malloc(Dimension *
sizeof(
int));
737 errormsg1(
"Gauss",
"outofmem",
"out of memory space");
751 for (j = 1; j <= Dimension; j++) {
752 for (i = Rank; i < NbEq; i++)
772 for (i = pivot + 1; i < NbEq;
777 Combine(p[i], p[Rank], p[i], j, Dimension);
783 column_index[Rank] = j;
789 for (k = Rank - 1; k >= 0; k--) {
793 for (i = 0; i < k; i++) {
797 Combine(p[i], p[k], p[i], j, Dimension);
801 for (i = NbEq; i < NbRows; i++) {
805 Combine(p[i], p[k], p[i], j, Dimension);
811 free(column_index), column_index = NULL;
827 fprintf(stderr,
"[Gauss : Input]\nRay =");
834 fprintf(stderr,
"[Gauss : Output]\nRay =");
856 unsigned Dimension, sat_nbcolumns, NbRay, NbConstraints, RowSize2,
857 *Trace = NULL, *bx = NULL, *jx = NULL, Dim_RaySpace, b;
858 unsigned NbBid, NbUni, NbEq, NbIneq;
859 unsigned NbBid2, NbUni2, NbEq2, NbIneq2;
861 int aux, *temp2 = NULL;
869 NbConstraints = Mat->
NbRows;
870 RowSize2 = sat_nbcolumns *
sizeof(int);
874 errormsg1(
"Remove_Redundants",
"outofmem",
"out of memory space");
879 temp2 = (
int *)calloc(sat_nbcolumns,
sizeof(
int));
886 bx = (
unsigned *)malloc(NbConstraints *
sizeof(
unsigned));
889 jx = (
unsigned *)malloc(NbConstraints *
sizeof(
unsigned));
917 for (j = 0; j < NbConstraints; j++) {
932 for (i = 0; i < NbRay; i++) {
947 fprintf(stderr,
"[Remove_redundants : Init]\nConstraints =");
949 fprintf(stderr,
"\nRays =");
965 fprintf(stderr,
" j = ");
968 for (j = 0; j < NbConstraints; j++) {
971 fprintf(stderr,
" %i ", j);
977 temp2[jx[j]] |= bx[j];
985 fprintf(stderr,
"[Remove_redundants : IntoStep1]\nConstraints =");
987 fprintf(stderr,
" j = %i \n", j);
995 for (i = 0; i < NbRay; i++)
996 if (!(
Sat->
p[i][jx[j]] & bx[j])) {
1010 if (j == NbConstraints)
1013 exchange(jx[j], jx[NbConstraints], aux);
1014 exchange(bx[j], bx[NbConstraints], aux);
1021 for (i = 0; i < NbRay; i++)
1022 if (!(
Sat->
p[i][jx[j]] & bx[j])) {
1035 Mat->
NbRows = NbConstraints;
1038 for (i = 0; i < NbRay; i++) {
1056 fprintf(stderr,
"[Remove_redundants : Step1]\nConstraints =");
1058 fprintf(stderr,
"\nRay =");
1069 for (i = 0; i < NbEq; i++) {
1076 k < NbConstraints &&
value_cmp_si(Mat->
p[k][0], NbRay) != 0; k++)
1078 if (k >= NbConstraints)
1086 for (; k > i; k--) {
1105 for (i = 0; i < NbEq; i++) {
1113 for (i = 0; i < NbEq; i++) {
1116 for (j = i + 1; j < NbEq; j++) {
1118 if (!(temp2[jx[j]] & bx[j]))
1129 Filter[jx[i]] |= bx[i];
1134 fprintf(stderr,
"[Remove_redundants : Step2]\nConstraints =");
1136 fprintf(stderr,
"\nRay =");
1147 NbEq2 =
Gauss(Mat, NbEq, Dimension);
1152 if (NbEq2 >= Dimension)
1156 fprintf(stderr,
"[Remove_redundants : Step3]\nConstraints =");
1158 fprintf(stderr,
"\nRay =");
1169 for (i = 0, k = NbRay; i < NbBid && k > i; i++) {
1174 while (--k > i &&
value_cmp_si(Ray->
p[k][0], NbConstraints + 1) != 0)
1185 fprintf(stderr,
"[Remove_redundants : Step4]\nConstraints =");
1187 fprintf(stderr,
"\nRay =");
1198 NbBid2 =
Gauss(Ray, NbBid, Dimension);
1201 fprintf(stderr,
"[Remove_redundants : After Gauss]\nRay =");
1207 if (NbBid2 >= Dimension) {
1208 errormsg1(
"RemoveRedundants",
"rmrdt",
"dimension error");
1213 Dim_RaySpace = Dimension - 1 - NbEq2 - NbBid2;
1216 fprintf(stderr,
"[Remove_redundants : Step5]\nConstraints =");
1218 fprintf(stderr,
"\nRay =");
1230 for (j = 0; j < NbConstraints; j++) {
1250 Status = VALUE_TO_INT(Mat->
p[j][0]);
1254 else if (Status < Dim_RaySpace)
1256 else if (Status == NbRay)
1265 fprintf(stderr,
"[Remove_redundants : Step6]\nConstraints =");
1267 fprintf(stderr,
"\nRay =");
1277 for (j = 0; j < NbRay; j++) {
1278 Status = VALUE_TO_INT(Ray->
p[j][0]);
1280 if (Status < Dim_RaySpace)
1282 else if (Status == NbConstraints + 1)
1303 Pol->
NbBid = NbBid2;
1313 fprintf(stderr,
"[Remove_redundants : Step7]\nConstraints =");
1315 fprintf(stderr,
"\nRay =");
1332 Trace = (
unsigned *)malloc(sat_nbcolumns *
sizeof(
unsigned));
1360 for (j = NbEq; j < NbConstraints; j++) {
1364 for (k = 0; k < sat_nbcolumns; k++)
1369 for (i = NbBid; i < NbRay; i++)
1373 if (!(
Sat->
p[i][jx[j]] & bx[j]))
1374 for (k = 0; k < sat_nbcolumns; k++)
1375 Trace[k] |=
Sat->
p[i][k];
1382 for (i = NbEq; i < NbConstraints; i++) {
1386 !(Trace[jx[i]] & bx[i])) {
1397 Filter[jx[j]] |= bx[j];
1402 free(Trace), Trace = NULL;
1405 fprintf(stderr,
"[Remove_redundants : Step8]\nConstraints =");
1407 fprintf(stderr,
"\nRay =");
1420 Trace = (
unsigned *)malloc(NbRay *
sizeof(
unsigned));
1447 for (i = NbBid; i < NbRay; i++) {
1454 for (k = NbBid; k < NbRay; k++)
1461 for (k = NbBid; k < NbRay; k++)
1468 for (j = NbEq; j < NbConstraints; j++)
1472 if (!(
Sat->
p[i][jx[j]] & bx[j]))
1473 for (k = NbBid; k < NbRay; k++)
1474 Trace[k] |=
Sat->
p[k][jx[j]] & bx[j];
1486 for (j = NbBid; j < NbRay; j++) {
1489 if (
value_one_p(Ray->
p[j][0]) && (i != j) && !Trace[j]) {
1508 if (aux >= Dim_RaySpace) {
1519 fprintf(stderr,
"[Remove_redundants : Step9]\nConstraints =");
1521 fprintf(stderr,
"\nRay =");
1532 Pol->
NbRays = NbBid2 + NbUni2;
1540 errormsg1(
"Remove_Redundants",
"outofmem",
"out of memory space");
1570 unsigned NbRows, NbColumns;
1576 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1587 NbRows = NbConstraints + NbRays;
1588 NbColumns = Dimension + 2;
1590 q = (Value **)malloc(NbRows *
sizeof(Value *));
1592 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1598 errormsg1(
"Polyhedron_Alloc",
"outofmem",
"out of memory space");
1602 Pol->
Ray = q + NbConstraints;
1604 for (i = 0; i < NbRows; i++) {
1629 for (; Pol; Pol = Next) {
1640 unsigned Dimension, NbConstraints, NbRays;
1645 fprintf(Dst,
"<null polyhedron>\n");
1652 fprintf(Dst,
"POLYHEDRON Dimension:%d\n", Pol->
Dimension);
1653 fprintf(Dst,
" Constraints:%d Equations:%d Rays:%d Lines:%d\n",
1655 fprintf(Dst,
"Constraints %d %d\n", NbConstraints, Dimension);
1657 for (i = 0; i < NbConstraints; i++) {
1662 fprintf(Dst,
"Inequality: [");
1664 fprintf(Dst,
"Equality: [");
1666 for (j = 1; j < Dimension; j++) {
1669 (void)fprintf(Dst,
" ]\n");
1672 (void)fprintf(Dst,
"Rays %d %d\n", NbRays, Dimension);
1673 for (i = 0; i < NbRays; i++) {
1682 fprintf(Dst,
"Vertex: [");
1684 fprintf(Dst,
"Ray: [");
1687 fprintf(Dst,
"Line: [");
1689 for (j = 1; j < Dimension - 1; j++) {
1695 fprintf(Dst,
" ]/");
1699 fprintf(Dst,
" ]\n");
1702 fprintf(Dst,
"UNION ");
1728 errormsg1(
"Empty_Polyhedron",
"outofmem",
"out of memory space");
1732 for (i = 0; i <= Dimension; i++) {
1737 Pol->
NbEq = Dimension + 1;
1761 errormsg1(
"Universe_Polyhedron",
"outofmem",
"out of memory space");
1771 Vector_Set(Pol->
Ray[0], 0, (Dimension + 1) * (Dimension + 2));
1772 for (i = 0; i <= Dimension; i++) {
1781 Pol->
NbBid = Dimension;
1794 for (i = NbEq; i < Constraints->
NbRows; ++i) {
1796 for (k = i + 1; k < Constraints->
NbRows; ++k) {
1797 for (j = 1; j < Constraints->
NbColumns - 1; ++j) {
1816 if (k < Constraints->NbRows)
1818 Constraints->
p[Constraints->
NbRows],
1840 for (row = NbEq; row < Constraints->
NbRows; ++row) {
1852 for (nrow = row + 1; nrow < Constraints->
NbRows; ++nrow) {
1857 for (k = d; k < Constraints->
NbColumns - 1; ++k) {
1859 Constraints->
p[nrow][1 + k]))
1863 if (
value_eq(Constraints->
p[row][1 + k], Constraints->
p[nrow][1 + k]))
1878 value_addto(tmp, Constraints->
p[row][1 + k], Constraints->
p[nrow][1 + k]);
1910 unsigned Dimension, nbcolumns;
1914 if (Dimension < 1) {
1915 errormsg1(
"Constraints2Polyhedron",
"invalidpoly",
1916 "invalid polyhedron dimension");
1922 if (Constraints->
NbRows == 0) {
1936 for (i = 0; i < Constraints->
NbRows; ++i)
1940 Dimension + 1, &tmp)) {
1955 Rank =
Gauss(Constraints, NbEq, Dimension);
1957 for (i = NbEq; i < Constraints->
NbRows; ++i)
1959 Dimension + 1, &tmp);
1969 if (Constraints->
NbRows > NbEq)
1979 if (Dimension > NbMaxRays)
1980 NbMaxRays = Dimension;
1994 errormsg1(
"Constraints2Polyhedron",
"outofmem",
"out of memory space");
1998 for (i = 0; i < Dimension; i++) {
2009 nbcolumns = (Constraints->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2028 Chernikova(Constraints, Ray,
Sat, Dimension - 1, NbMaxRays, 0, 0);
2031 fprintf(stderr,
"[constraints2polyhedron]\nConstraints = ");
2033 fprintf(stderr,
"\nRay = ");
2035 fprintf(stderr,
"\nSat = ");
2046 fprintf(stderr,
"\nPol = ");
2063 unsigned NbConstraints, Dimension;
2071 errormsg1(
"Polyhedron2Constraints",
"outofmem",
"out of memory space");
2092 unsigned Dimension, nbcolumns;
2097 SatTranspose = NULL;
2111 if (Dimension > NbMaxConstrs)
2112 NbMaxConstrs = Dimension;
2117 errormsg1(
"Rays2Polyhedron",
"outofmem",
"out of memory space");
2123 for (i = 0; i < Dimension; i++) {
2132 nbcolumns = (Ray->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2133 SatTranspose =
SMAlloc(NbMaxConstrs, nbcolumns);
2135 SatTranspose->NbRows = Dimension;
2138 fprintf(stderr,
"[ray2polyhedron: Before]\nRay = ");
2140 fprintf(stderr,
"\nConstraints = ");
2142 fprintf(stderr,
"\nSatTranspose = ");
2164 Chernikova(Ray, Mat, SatTranspose, Dimension, NbMaxConstrs, 0, 1);
2167 fprintf(stderr,
"[ray2polyhedron: After]\nRay = ");
2169 fprintf(stderr,
"\nConstraints = ");
2171 fprintf(stderr,
"\nSatTranspose = ");
2180 fprintf(stderr,
"\nSat =");
2184 SMFree(&SatTranspose), SatTranspose = NULL;
2193 fprintf(stderr,
"\nPol = ");
2245 unsigned NbMaxRays) {
2249 Value *p1, *p2, *p3;
2250 unsigned Dimension, NbRay, bx, nbcolumns;
2262 nbcolumns = (Mat->
NbRows - 1) / (
sizeof(
int) * 8) + 1;
2268 for (k = 0; k < NbConstraints; k++) {
2269 for (i = 0; i < NbRay; i++) {
2277 for (j = 0; j < Dimension; j++) {
2283 for (j = 0; j < NbRay; j++) {
2288 Sat->
p[j][jx] |= bx;
2304 unsigned NbMaxRays) {
2307 Matrix *Mat = NULL, *Ray = NULL;
2309 unsigned NbRay, NbCon, Dimension;
2311 if (NbConstraints == 0)
2322 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2349 if (NbRay > NbMaxRays)
2354 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2368 errormsg1(
"AddConstraints",
"outofmem",
"out of memory space");
2372 Ray->NbRows = NbRay;
2417 for (i = 0; i < Pol2->
NbRays; i++) {
2420 p1 = Pol2->
Ray[i] + 1;
2423 for (j = 0; j < Dimension; j++) {
2479 for (p = PolDomain, PolDomain = (
Polyhedron *)0; p; p = pnext) {
2493 p_domain_end->
next = p;
2510 p_domain_end->
next = Pol;
2533 Matrix *Mat = NULL, *Ray = NULL;
2535 unsigned NbRay, NbCon, NbEle1, Dimension;
2556 for (i = 1; i < Dimension; i++)
2559 if (i == Dimension) {
2567 NbEle1 = NbCon * Dimension;
2573 if (NbRay > NbMaxRays)
2578 errormsg1(
"SubConstraint",
"outofmem",
"out of memory space");
2589 for (i = 1; i < Dimension; i++)
2592 for (i = 1; i < Dimension; i++)
2596 Mat->
p[NbCon][Dimension - 1]);
2601 errormsg1(
"SubConstraint",
"outofmem",
"out of memory space");
2607 Ray->NbRows = NbRay;
2638 unsigned NbMaxRays) {
2645 errormsg1(
"DomainIntersection",
"diffdim",
2646 "operation on different dimensions");
2654 for (p1 = Pol1; p1; p1 = p1->
next) {
2655 for (p2 = Pol2; p2; p2 = p2->
next) {
2675 unsigned NbRays, Dimension;
2683 errormsg1(
"Polyhedron2Rays",
"outofmem",
"out of memory space");
2697 unsigned NbMaxConstrs) {
2700 Matrix *Mat = NULL, *Ray = NULL;
2702 unsigned NbCon, NbRay, NbEle1, Dimension;
2725 NbEle1 = NbRay * Dimension;
2729 errormsg1(
"AddRays",
"outofmem",
"out of memory space");
2739 Vector_Copy(AddedRays, Ray->p_Init + NbEle1, NbAddedRays * Dimension);
2746 if (NbMaxConstrs < NbCon)
2747 NbMaxConstrs = NbCon;
2752 errormsg1(
"AddRays",
"outofmem",
"out of memory space");
2764 SatTranspose =
BuildSat(Ray, Mat, NbRay, NbMaxConstrs);
2768 Chernikova(Ray, Mat, SatTranspose, Pol->
NbEq, NbMaxConstrs, NbRay, 1);
2773 SMFree(&SatTranspose), SatTranspose = NULL;
2801 if (!Ray || Ray->
NbRows == 0)
2804 errormsg1(
"DomainAddRays",
"diffdim",
"operation on different dimensions");
2810 for (p1 = Pol; p1; p1 = p1->
next) {
2815 for (p2 = PolA; p2; p2 = p2->
next) {
2827 PolEndA = PolA = p3;
2830 PolEndA = PolEndA->
next;
2850 errormsg1(
"Polyhedron_Copy",
"outofmem",
"out of memory space");
2908 Value *tmpC,
int NbRays,
int NbConstraints) {
2921 for (i = 0; i < NbRays; i++)
2923 if (
Sat->
p[i][kj] & kb)
2933 for (j = 0; j < NbConstraints; j++) {
2953 unsigned NbMaxRays) {
2957 int i, j, k, jx, found;
2959 unsigned Dimension, NbRays, NbConstraints, bx, nc;
2960 Value NbConstraintsLeft, tmp;
2961 Value *tmpC = NULL, *tmpR = NULL;
2971 for (i = 0; i < NbConstraints; i++)
2976 for (i = 0; i < NbRays; i++)
2984 if (Pol2 && Pol2 != P2)
2986 if (Pol && Pol != Pol2 && Pol != P2)
3000 errormsg1(
"FindSimple",
"outofmem",
"out of memory space");
3016 if (Filter[jx] & bx) {
3051 tmpR = (Value *)malloc(NbRays *
sizeof(Value));
3053 errormsg1(
"FindSimple",
"outofmem",
"out of memory space");
3062 for (i = 0; i < NbRays; i++)
3064 tmpC = (Value *)malloc(NbConstraints *
sizeof(Value));
3066 errormsg1(
"FindSimple",
"outofmem",
"out of memory space");
3073 for (i = 0; i < NbRays; i++)
3078 for (i = 0; i < NbConstraints; i++)
3084 nc = (NbConstraints - 1) / (
sizeof(
int) * 8) + 1;
3091 for (k = 0; k < NbConstraints; k++) {
3092 if (Filter[jx] & bx)
3095 for (i = 0; i < NbRays; i++) {
3096 p1 = Pol->
Ray[i] + 1;
3099 for (j = 0; j < Dimension; j++) {
3106 Sat->
p[i][jx] |= bx;
3116 for (i = 0; i < NbRays; i++)
3119 if (
value_eq(tmp, NbConstraintsLeft)) {
3124 for (k = 0; k < NbConstraints; k++) {
3147#ifndef LINEAR_VALUE_IS_CHARS
3154 for (k = 0; k < NbConstraints; k++)
3163 errormsg1(
"DomSimplify",
"logerror",
"logic error");
3172 for (i = 0; i < NbConstraints; i++)
3174 free(tmpC), tmpC = NULL;
3175 for (i = 0; i < NbRays; i++)
3177 free(tmpR), tmpR = NULL;
3202 unsigned *Filter,
unsigned NbMaxRays) {
3205 Matrix *Mat = NULL, *Ray = NULL;
3207 unsigned NbRay, NbCon, NbCon1, NbCon2, NbEle1, Dimension, notempty;
3225 NbCon = NbCon1 + NbCon2;
3227 NbEle1 = NbCon1 * Dimension;
3233 if (NbRay > NbMaxRays)
3239 errormsg1(
"SimplifyConstraints",
"outofmem",
"out of memory space");
3253 errormsg1(
"SimplifyConstraints",
"outofmem",
"out of memory space");
3257 Ray->NbRows = NbRay;
3272 if (Filter &&
emptyQ(Pol)) {
3297 unsigned ix, bx, NbEqn, NbEqn1, NbEqn2, NbEle2, Dimension;
3300 NbEqn1 = Pol1->
NbEq;
3301 NbEqn2 = Pol2->
NbEq;
3302 NbEqn = NbEqn1 + NbEqn2;
3304 NbEle2 = NbEqn2 * Dimension;
3308 errormsg1(
"SimplifyEqualities",
"outofmem",
"out of memory space");
3319 Gauss(Mat, NbEqn2, Dimension - 1);
3323 for (i = NbEqn2; i < NbEqn; i++) {
3324 for (j = 1; j < Dimension; j++) {
3347 unsigned NbMaxRays) {
3350 unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
3357 errormsg1(
"DomSimplify",
"diffdim",
"operation on different dimensions");
3369 for (p2 = Pol2; p2; p2 = p2->
next)
3375 for (p1 = Pol1; p1; p1 = p1->
next) {
3385 nbentries = (NbConstraints + NbCon - 1) / (
sizeof(
int) * 8) + 1;
3388 Filter = (
unsigned *)malloc(nbentries *
sizeof(
int));
3390 errormsg1(
"DomSimplify",
"outofmem",
"out of memory space\n");
3400 for (p2 = Pol2; p2; p2 = p2->
next) {
3421 errormsg1(
"DomSimplify",
"outofmem",
"out of memory space\n");
3426 for (k = 0, jx = 0, bx =
MSB; k < NbConstraints; k++) {
3430 if (Filter[jx] & bx) {
3460 unsigned NbMaxRays) {
3463 unsigned k, jx, bx, nbentries, NbConstraints, Dimension, NbCon, empty;
3464 unsigned *Filter = NULL;
3465 Matrix *Constraints = NULL;
3479 if (!Pol1 || !Pol2) {
3485 "operation on different dimensions");
3499 for (p2 = Pol2; p2; p2 = p2->
next)
3505 for (p1 = Pol1; p1; p1 = p1->
next) {
3513 nbentries = (NbConstraints + NbCon - 1) / (
sizeof(
int) * 8) + 1;
3516 Filter = (
unsigned *)malloc(nbentries *
sizeof(
int));
3518 errormsg1(
"DomainSimplify",
"outofmem",
"out of memory space");
3528 for (p2 = Pol2; p2; p2 = p2->
next) {
3544 errormsg1(
"DomainSimplify",
"outofmem",
"out of memory space");
3549 for (k = 0, jx = 0, bx =
MSB; k < NbConstraints; k++) {
3553 if (Filter[jx] & bx) {
3570 free(Filter), Filter = NULL;
3586 unsigned NbMaxRays) {
3588 Polyhedron *PolA, *PolEndA, *PolB, *PolEndB, *p1, *p2;
3594 errormsg1(
"DomainUnion",
"diffdim",
"operation on different dimensions");
3600 for (p1 = Pol1; p1; p1 = p1->
next) {
3604 for (p2 = Pol2; p2; p2 = p2->
next) {
3618 PolEndA = PolEndA->
next;
3625 for (p2 = Pol2; p2; p2 = p2->
next) {
3629 for (p1 = PolA; p1; p1 = p1->
next) {
3642 PolEndB = PolEndB->
next;
3649 PolEndA->
next = PolB;
3677 for (p = Pol->
next; p; p = p->
next) {
3695 unsigned NbMaxRays) {
3703 errormsg1(
"DomainDifference",
"diffdim",
3704 "operation on different dimensions");
3714 for (p2 = Pol2; p2; p2 = p2->
next) {
3715 for (p1 = Pol1; p1; p1 = p1->
next) {
3756 Polyhedron *p = NULL, **next, *result = NULL;
3769 if (align_dimension < Pol->Dimension) {
3770 errormsg1(
"align_context",
"diffdim",
"context dimension exceeds data");
3774 if (align_dimension == Pol->
Dimension) {
3784 for (; Pol; Pol = Pol->
next) {
3789 unsigned NbRays = have_rays ? Pol->
NbRays + k : 0;
3794 "context not of uniform dimension");
3801 for (i = 0; i < NbCons; ++i) {
3811 for (i = 0; i < k; ++i)
3813 for (i = 0; i < Pol->
NbRays; ++i) {
3860 errormsg1(
"Polyhedron_Scan",
"outofmem",
"out of memory space");
3870 for (i = 0; i < dim; i++) {
3872 for (j = i + 1; j < dim; j++) {
3875 Mat->
NbRows = dim - i - 1;
3910 Value
n, n1, d, tmp;
4029 unsigned Dimension1, Dimension2;
4044 for (i = 0; i < NbRays; i++) {
4046 for (j = 0; j < Dimension2; j++) {
4048 for (k = 0; k < Dimension1; k++) {
4071 unsigned Dimension1, Dimension2;
4086 for (i = 0; i < NbRays; i++) {
4088 for (j = 0; j < Dimension2; j++) {
4090 for (k = 0; k < Dimension1; k++) {
4114 unsigned NbMaxRays) {
4116 Matrix *Constraints = NULL;
4118 unsigned NbConstraints, Dimension1, Dimension2;
4134 if (Dimension1 != (Func->
NbRows)) {
4135 errormsg1(
"Polyhedron_Preimage",
"dimincomp",
"incompatible dimensions");
4148 Constraints =
Matrix_Alloc(NbConstraints, Dimension2 + 1);
4150 errormsg1(
"Polyhedron_Preimage",
"outofmem",
"out of memory space\n");
4185 if (!Pol || !Func) {
4190 for (p = Pol; p; p = p->
next) {
4205 unsigned NbMaxConstrs) {
4209 unsigned NbRays, Dimension1, Dimension2;
4225 Dimension2 = Func->
NbRows;
4227 errormsg1(
"Polyhedron_Image",
"dimincomp",
"incompatible dimensions");
4241 if (Dimension1 == Dimension2) {
4247 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4258 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4280 errormsg1(
"Polyhedron_Image",
"outofmem",
"out of memory space\n");
4314 if (!Pol || !Func) {
4319 for (p = Pol; p; p = p->
next) {
4342 int i, j, NbRay, Dim;
4343 Value *p1, *p2, p3, d, status;
4344 Value tmp1, tmp2, tmp3;
4376 errormsg1(
"DomainCost",
"outofmem",
"out of memory space\n");
4402 for (i = 0; i < NbRay; i++) {
4412 for (j = 1; j < Dim; j++) {
4486 unsigned NbMaxRays) {
4496 errormsg1(
"DomainAddConstraints",
"diffdim",
4497 "operation on different dimensions");
4503 for (p1 = Pol; p1; p1 = p1->
next) {
4508 for (p2 = PolA; p2; p2 = p2->
next) {
4520 PolEndA = PolA = p3;
4523 PolEndA = PolEndA->
next;
4541 Polyhedron *lP, *tmp, *Result, *lR, *prec, *reste;
4542 Polyhedron *p1, *p2, *p3, *Pol1, *dx, *d1, *d2, *pi, *newpi;
4545 if (flag != 0 && flag != 1) {
4546 errormsg1(
"Disjoint_Domain",
"invalidarg",
4547 "flag should be equal to 0 or 1");
4557 for (lP = P; lP; lP = lP->
next) {
4562 while (lR && reste) {
4565 for (p1 = reste; p1; p1 = p1->
next) {
4592 for (p1 = reste; p1; p1 = p1->
next) {
4617 if (newpi &&
emptyQ(newpi)) {
4631 for (p2 = reste; p2; p2 = p2->
next) {
4633 for (p1 = Pol1; p1; p1 = p1->
next) {
4661 if (newpi &&
emptyQ(newpi)) {
4695 for (tmp = d2; tmp->
next; tmp = tmp->
next)
4704 for (tmp = dx; tmp->
next; tmp = tmp->
next)
4713 errormsg1(
"Disjoint_Domain",
"internalerror",
"internal error");
4726 for (tmp = reste; tmp; tmp = tnext) {
4745 for (j = 0; j < Pol->
Dimension + 2; j++)
4754 for (Q = Pol; Q; Q = Q->
next)
4806 for (prev = &R; P; P = N) {
4811 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_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)
#define value_posz_p(val)
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)
Polyhedron * DomainAddConstraints(Polyhedron *Pol, Matrix *Mat, unsigned NbMaxRays)
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 void SatMatrix_Extend(SatMatrix *Sat, Matrix *Mat, unsigned rows)
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)
Return the intersection of two polyhedral domains 'Pol1' and 'Pol2'.
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 SMPrint(SatMatrix *matrix)
static void SimplifyEqualities(Polyhedron *Pol1, Polyhedron *Pol2, unsigned *Filter)
static void addToFilter(int k, unsigned *Filter, SatMatrix *Sat, Value *tmpR, Value *tmpC, int NbRays, int NbConstraints)
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)
int Vector_Equal(Value *Vec1, Value *Vec2, unsigned n)
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)
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)