Merge branch 'maint'
[platform/upstream/isl.git] / isl_pw_templ.c
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
5
6 #ifdef HAS_TYPE
7 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
8         enum isl_fold type, int n)
9 #else
10 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
11 #endif
12 {
13         isl_ctx *ctx;
14         struct PW *pw;
15
16         if (!dim)
17                 return NULL;
18         ctx = isl_space_get_ctx(dim);
19         isl_assert(ctx, n >= 0, goto error);
20         pw = isl_alloc(ctx, struct PW,
21                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
22         if (!pw)
23                 goto error;
24
25         pw->ref = 1;
26 #ifdef HAS_TYPE
27         pw->type = type;
28 #endif
29         pw->size = n;
30         pw->n = 0;
31         pw->dim = dim;
32         return pw;
33 error:
34         isl_space_free(dim);
35         return NULL;
36 }
37
38 #ifdef HAS_TYPE
39 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
40 {
41         return FN(PW,alloc_size)(dim, type, 0);
42 }
43 #else
44 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
45 {
46         return FN(PW,alloc_size)(dim, 0);
47 }
48 #endif
49
50 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
51         __isl_take isl_set *set, __isl_take EL *el)
52 {
53         isl_ctx *ctx;
54         isl_space *el_dim = NULL;
55
56         if (!pw || !set || !el)
57                 goto error;
58
59         if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
60                 isl_set_free(set);
61                 FN(EL,free)(el);
62                 return pw;
63         }
64
65         ctx = isl_set_get_ctx(set);
66 #ifdef HAS_TYPE
67         if (pw->type != el->type)
68                 isl_die(ctx, isl_error_invalid, "fold types don't match",
69                         goto error);
70 #endif
71         el_dim = FN(EL,get_space(el));
72         isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
73         isl_assert(ctx, pw->n < pw->size, goto error);
74
75         pw->p[pw->n].set = set;
76         pw->p[pw->n].FIELD = el;
77         pw->n++;
78         
79         isl_space_free(el_dim);
80         return pw;
81 error:
82         isl_space_free(el_dim);
83         FN(PW,free)(pw);
84         isl_set_free(set);
85         FN(EL,free)(el);
86         return NULL;
87 }
88
89 #ifdef HAS_TYPE
90 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
91         __isl_take isl_set *set, __isl_take EL *el)
92 #else
93 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
94 #endif
95 {
96         PW *pw;
97
98         if (!set || !el)
99                 goto error;
100
101 #ifdef HAS_TYPE
102         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
103 #else
104         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
105 #endif
106
107         return FN(PW,add_piece)(pw, set, el);
108 error:
109         isl_set_free(set);
110         FN(EL,free)(el);
111         return NULL;
112 }
113
114 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
115 {
116         int i;
117         PW *dup;
118
119         if (!pw)
120                 return NULL;
121
122 #ifdef HAS_TYPE
123         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
124 #else
125         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
126 #endif
127         if (!dup)
128                 return NULL;
129
130         for (i = 0; i < pw->n; ++i)
131                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
132                                             FN(EL,copy)(pw->p[i].FIELD));
133
134         return dup;
135 }
136
137 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
138 {
139         if (!pw)
140                 return NULL;
141
142         if (pw->ref == 1)
143                 return pw;
144         pw->ref--;
145         return FN(PW,dup)(pw);
146 }
147
148 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
149 {
150         if (!pw)
151                 return NULL;
152
153         pw->ref++;
154         return pw;
155 }
156
157 void *FN(PW,free)(__isl_take PW *pw)
158 {
159         int i;
160
161         if (!pw)
162                 return NULL;
163         if (--pw->ref > 0)
164                 return NULL;
165
166         for (i = 0; i < pw->n; ++i) {
167                 isl_set_free(pw->p[i].set);
168                 FN(EL,free)(pw->p[i].FIELD);
169         }
170         isl_space_free(pw->dim);
171         free(pw);
172
173         return NULL;
174 }
175
176 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
177         unsigned pos)
178 {
179         return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
180 }
181
182 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
183         unsigned pos)
184 {
185         return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
186 }
187
188 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
189 {
190         return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
191 }
192
193 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
194 {
195         return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
196 }
197
198 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
199 {
200         return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
201 }
202
203 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
204 {
205         if (!pw)
206                 return -1;
207
208         return pw->n == 0;
209 }
210
211 #ifndef NO_REALIGN
212 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
213         __isl_take isl_reordering *exp)
214 {
215         int i;
216
217         pw = FN(PW,cow)(pw);
218         if (!pw || !exp)
219                 return NULL;
220
221         for (i = 0; i < pw->n; ++i) {
222                 pw->p[i].set = isl_set_realign(pw->p[i].set,
223                                                     isl_reordering_copy(exp));
224                 if (!pw->p[i].set)
225                         goto error;
226                 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
227                                                     isl_reordering_copy(exp));
228                 if (!pw->p[i].FIELD)
229                         goto error;
230         }
231
232         pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
233
234         isl_reordering_free(exp);
235         return pw;
236 error:
237         isl_reordering_free(exp);
238         FN(PW,free)(pw);
239         return NULL;
240 }
241
242 /* Align the parameters of "pw" to those of "model".
243  */
244 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
245 {
246         isl_ctx *ctx;
247
248         if (!pw || !model)
249                 goto error;
250
251         ctx = isl_space_get_ctx(model);
252         if (!isl_space_has_named_params(model))
253                 isl_die(ctx, isl_error_invalid,
254                         "model has unnamed parameters", goto error);
255         if (!isl_space_has_named_params(pw->dim))
256                 isl_die(ctx, isl_error_invalid,
257                         "input has unnamed parameters", goto error);
258         if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
259                 isl_reordering *exp;
260
261                 model = isl_space_drop_dims(model, isl_dim_in,
262                                         0, isl_space_dim(model, isl_dim_in));
263                 model = isl_space_drop_dims(model, isl_dim_out,
264                                         0, isl_space_dim(model, isl_dim_out));
265                 exp = isl_parameter_alignment_reordering(pw->dim, model);
266                 exp = isl_reordering_extend_space(exp,
267                                         FN(PW,get_domain_space)(pw));
268                 pw = FN(PW,realign_domain)(pw, exp);
269         }
270
271         isl_space_free(model);
272         return pw;
273 error:
274         isl_space_free(model);
275         FN(PW,free)(pw);
276         return NULL;
277 }
278
279 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
280         __isl_take PW *pw2,
281         __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
282 {
283         isl_ctx *ctx;
284
285         if (!pw1 || !pw2)
286                 goto error;
287         if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
288                 return fn(pw1, pw2);
289         ctx = FN(PW,get_ctx)(pw1);
290         if (!isl_space_has_named_params(pw1->dim) ||
291             !isl_space_has_named_params(pw2->dim))
292                 isl_die(ctx, isl_error_invalid,
293                         "unaligned unnamed parameters", goto error);
294         pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
295         pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
296         return fn(pw1, pw2);
297 error:
298         FN(PW,free)(pw1);
299         FN(PW,free)(pw2);
300         return NULL;
301 }
302
303 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
304         __isl_take isl_set *set,
305         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
306 {
307         isl_ctx *ctx;
308
309         if (!pw || !set)
310                 goto error;
311         if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
312                 return fn(pw, set);
313         ctx = FN(PW,get_ctx)(pw);
314         if (!isl_space_has_named_params(pw->dim) ||
315             !isl_space_has_named_params(set->dim))
316                 isl_die(ctx, isl_error_invalid,
317                         "unaligned unnamed parameters", goto error);
318         pw = FN(PW,align_params)(pw, isl_set_get_space(set));
319         set = isl_set_align_params(set, FN(PW,get_space)(pw));
320         return fn(pw, set);
321 error:
322         FN(PW,free)(pw);
323         isl_set_free(set);
324         return NULL;
325 }
326 #endif
327
328 static __isl_give PW *FN(PW,add_aligned)(__isl_take PW *pw1, __isl_take PW *pw2)
329 {
330         int i, j, n;
331         struct PW *res;
332         isl_ctx *ctx;
333         isl_set *set;
334
335         if (!pw1 || !pw2)
336                 goto error;
337
338         ctx = isl_space_get_ctx(pw1->dim);
339 #ifdef HAS_TYPE
340         if (pw1->type != pw2->type)
341                 isl_die(ctx, isl_error_invalid,
342                         "fold types don't match", goto error);
343 #endif
344         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
345
346         if (FN(PW,IS_ZERO)(pw1)) {
347                 FN(PW,free)(pw1);
348                 return pw2;
349         }
350
351         if (FN(PW,IS_ZERO)(pw2)) {
352                 FN(PW,free)(pw2);
353                 return pw1;
354         }
355
356         n = (pw1->n + 1) * (pw2->n + 1);
357 #ifdef HAS_TYPE
358         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
359 #else
360         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
361 #endif
362
363         for (i = 0; i < pw1->n; ++i) {
364                 set = isl_set_copy(pw1->p[i].set);
365                 for (j = 0; j < pw2->n; ++j) {
366                         struct isl_set *common;
367                         EL *sum;
368                         set = isl_set_subtract(set,
369                                         isl_set_copy(pw2->p[j].set));
370                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
371                                                 isl_set_copy(pw2->p[j].set));
372                         if (isl_set_plain_is_empty(common)) {
373                                 isl_set_free(common);
374                                 continue;
375                         }
376
377                         sum = FN(EL,add_on_domain)(common,
378                                                    FN(EL,copy)(pw1->p[i].FIELD),
379                                                    FN(EL,copy)(pw2->p[j].FIELD));
380
381                         res = FN(PW,add_piece)(res, common, sum);
382                 }
383                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
384         }
385
386         for (j = 0; j < pw2->n; ++j) {
387                 set = isl_set_copy(pw2->p[j].set);
388                 for (i = 0; i < pw1->n; ++i)
389                         set = isl_set_subtract(set,
390                                         isl_set_copy(pw1->p[i].set));
391                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
392         }
393
394         FN(PW,free)(pw1);
395         FN(PW,free)(pw2);
396
397         return res;
398 error:
399         FN(PW,free)(pw1);
400         FN(PW,free)(pw2);
401         return NULL;
402 }
403
404 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
405 {
406         return FN(PW,align_params_pw_pw_and)(pw1, pw2, &FN(PW,add_aligned));
407 }
408
409 /* Make sure "pw" has room for at least "n" more pieces.
410  *
411  * If there is only one reference to pw, we extend it in place.
412  * Otherwise, we create a new PW and copy the pieces.
413  */
414 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
415 {
416         int i;
417         isl_ctx *ctx;
418         PW *res;
419
420         if (!pw)
421                 return NULL;
422         if (pw->n + n <= pw->size)
423                 return pw;
424         ctx = FN(PW,get_ctx)(pw);
425         n += pw->n;
426         if (pw->ref == 1) {
427                 res = isl_realloc(ctx, pw, struct PW,
428                             sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
429                 if (!res)
430                         return FN(PW,free)(pw);
431                 res->size = n;
432                 return res;
433         }
434 #ifdef HAS_TYPE
435         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
436 #else
437         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
438 #endif
439         if (!res)
440                 return FN(PW,free)(pw);
441         for (i = 0; i < pw->n; ++i)
442                 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
443                                             FN(EL,copy)(pw->p[i].FIELD));
444         FN(PW,free)(pw);
445         return res;
446 }
447
448 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
449         __isl_take PW *pw2)
450 {
451         int i;
452         isl_ctx *ctx;
453
454         if (!pw1 || !pw2)
455                 goto error;
456
457         if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
458                 return FN(PW,add_disjoint_aligned)(pw2, pw1);
459
460         ctx = isl_space_get_ctx(pw1->dim);
461 #ifdef HAS_TYPE
462         if (pw1->type != pw2->type)
463                 isl_die(ctx, isl_error_invalid,
464                         "fold types don't match", goto error);
465 #endif
466         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
467
468         if (FN(PW,IS_ZERO)(pw1)) {
469                 FN(PW,free)(pw1);
470                 return pw2;
471         }
472
473         if (FN(PW,IS_ZERO)(pw2)) {
474                 FN(PW,free)(pw2);
475                 return pw1;
476         }
477
478         pw1 = FN(PW,grow)(pw1, pw2->n);
479         if (!pw1)
480                 goto error;
481
482         for (i = 0; i < pw2->n; ++i)
483                 pw1 = FN(PW,add_piece)(pw1,
484                                 isl_set_copy(pw2->p[i].set),
485                                 FN(EL,copy)(pw2->p[i].FIELD));
486
487         FN(PW,free)(pw2);
488
489         return pw1;
490 error:
491         FN(PW,free)(pw1);
492         FN(PW,free)(pw2);
493         return NULL;
494 }
495
496 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
497 {
498         return FN(PW,align_params_pw_pw_and)(pw1, pw2,
499                                                 &FN(PW,add_disjoint_aligned));
500 }
501
502 #ifndef NO_NEG
503 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
504 {
505         int i;
506
507         if (!pw)
508                 return NULL;
509
510         if (FN(PW,IS_ZERO)(pw))
511                 return pw;
512
513         pw = FN(PW,cow)(pw);
514         if (!pw)
515                 return NULL;
516
517         for (i = 0; i < pw->n; ++i) {
518                 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
519                 if (!pw->p[i].FIELD)
520                         return FN(PW,free)(pw);
521         }
522
523         return pw;
524 }
525
526 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
527 {
528         return FN(PW,add)(pw1, FN(PW,neg)(pw2));
529 }
530 #endif
531
532 #ifndef NO_EVAL
533 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
534         __isl_take isl_point *pnt)
535 {
536         int i;
537         int found = 0;
538         isl_ctx *ctx;
539         isl_space *pnt_dim = NULL;
540         isl_qpolynomial *qp;
541
542         if (!pw || !pnt)
543                 goto error;
544         ctx = isl_point_get_ctx(pnt);
545         pnt_dim = isl_point_get_space(pnt);
546         isl_assert(ctx, isl_space_is_domain(pnt_dim, pw->dim), goto error);
547
548         for (i = 0; i < pw->n; ++i) {
549                 found = isl_set_contains_point(pw->p[i].set, pnt);
550                 if (found < 0)
551                         goto error;
552                 if (found)
553                         break;
554         }
555         if (found)
556                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
557                                             isl_point_copy(pnt));
558         else
559                 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
560         FN(PW,free)(pw);
561         isl_space_free(pnt_dim);
562         isl_point_free(pnt);
563         return qp;
564 error:
565         FN(PW,free)(pw);
566         isl_space_free(pnt_dim);
567         isl_point_free(pnt);
568         return NULL;
569 }
570 #endif
571
572 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
573 {
574         int i;
575         isl_set *dom;
576
577         if (!pw)
578                 return NULL;
579
580         dom = isl_set_empty(FN(PW,get_domain_space)(pw));
581         for (i = 0; i < pw->n; ++i)
582                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
583
584         FN(PW,free)(pw);
585
586         return dom;
587 }
588
589 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
590         __isl_take isl_set *set)
591 {
592         int i;
593
594         if (!pw || !set)
595                 goto error;
596
597         if (pw->n == 0) {
598                 isl_set_free(set);
599                 return pw;
600         }
601
602         pw = FN(PW,cow)(pw);
603         if (!pw)
604                 goto error;
605
606         for (i = pw->n - 1; i >= 0; --i) {
607                 isl_basic_set *aff;
608                 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
609                 if (!pw->p[i].set)
610                         goto error;
611                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
612                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
613                                                                 aff);
614                 if (!pw->p[i].FIELD)
615                         goto error;
616                 if (isl_set_plain_is_empty(pw->p[i].set)) {
617                         isl_set_free(pw->p[i].set);
618                         FN(EL,free)(pw->p[i].FIELD);
619                         if (i != pw->n - 1)
620                                 pw->p[i] = pw->p[pw->n - 1];
621                         pw->n--;
622                 }
623         }
624         
625         isl_set_free(set);
626         return pw;
627 error:
628         isl_set_free(set);
629         FN(PW,free)(pw);
630         return NULL;
631 }
632
633 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
634         __isl_take isl_set *context)
635 {
636         return FN(PW,align_params_pw_set_and)(pw, context,
637                                         &FN(PW,intersect_domain_aligned));
638 }
639
640 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
641         __isl_take isl_set *context)
642 {
643         int i;
644         isl_basic_set *hull = NULL;
645
646         if (!pw || !context)
647                 goto error;
648
649         if (pw->n == 0) {
650                 isl_set_free(context);
651                 return pw;
652         }
653
654         if (!isl_space_match(pw->dim, isl_dim_param,
655                                 context->dim, isl_dim_param)) {
656                 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
657                 context = isl_set_align_params(context, FN(PW,get_space)(pw));
658         }
659
660         context = isl_set_compute_divs(context);
661         hull = isl_set_simple_hull(isl_set_copy(context));
662
663         pw = FN(PW,cow)(pw);
664         if (!pw)
665                 goto error;
666
667         for (i = pw->n - 1; i >= 0; --i) {
668                 pw->p[i].set = isl_set_intersect(pw->p[i].set,
669                                                  isl_set_copy(context));
670                 if (!pw->p[i].set)
671                         goto error;
672                 pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
673                                              isl_set_copy(pw->p[i].set));
674                 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
675                                                 isl_basic_set_copy(hull));
676                 if (!pw->p[i].set)
677                         goto error;
678                 if (isl_set_plain_is_empty(pw->p[i].set)) {
679                         isl_set_free(pw->p[i].set);
680                         FN(EL,free)(pw->p[i].FIELD);
681                         if (i != pw->n - 1)
682                                 pw->p[i] = pw->p[pw->n - 1];
683                         pw->n--;
684                 }
685         }
686
687         isl_basic_set_free(hull);
688         isl_set_free(context);
689
690         return pw;
691 error:
692         FN(PW,free)(pw);
693         isl_basic_set_free(hull);
694         isl_set_free(context);
695         return NULL;
696 }
697
698 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
699 {
700         return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
701 }
702
703 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
704 {
705         int i, j;
706
707         if (!pw)
708                 return NULL;
709         if (pw->n == 0)
710                 return pw;
711
712         for (i = pw->n - 1; i >= 0; --i) {
713                 for (j = i - 1; j >= 0; --j) {
714                         if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
715                                                         pw->p[j].FIELD))
716                                 continue;
717                         pw->p[j].set = isl_set_union(pw->p[j].set,
718                                                         pw->p[i].set);
719                         FN(EL,free)(pw->p[i].FIELD);
720                         if (i != pw->n - 1)
721                                 pw->p[i] = pw->p[pw->n - 1];
722                         pw->n--;
723                         break;
724                 }
725                 if (j >= 0)
726                         continue;
727                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
728                 if (!pw->p[i].set)
729                         goto error;
730         }
731
732         return pw;
733 error:
734         FN(PW,free)(pw);
735         return NULL;
736 }
737
738 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
739 {
740         return pw ? isl_space_get_ctx(pw->dim) : NULL;
741 }
742
743 #ifndef NO_INVOLVES_DIMS
744 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
745         unsigned first, unsigned n)
746 {
747         int i;
748         enum isl_dim_type set_type;
749
750         if (!pw)
751                 return -1;
752         if (pw->n == 0 || n == 0)
753                 return 0;
754
755         set_type = type == isl_dim_in ? isl_dim_set : type;
756
757         for (i = 0; i < pw->n; ++i) {
758                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
759                                                         type, first, n);
760                 if (involves < 0 || involves)
761                         return involves;
762                 involves = isl_set_involves_dims(pw->p[i].set,
763                                                         set_type, first, n);
764                 if (involves < 0 || involves)
765                         return involves;
766         }
767         return 0;
768 }
769 #endif
770
771 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
772         enum isl_dim_type type, unsigned pos, const char *s)
773 {
774         int i;
775         enum isl_dim_type set_type;
776
777         pw = FN(PW,cow)(pw);
778         if (!pw)
779                 return NULL;
780
781         set_type = type == isl_dim_in ? isl_dim_set : type;
782
783         pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
784         if (!pw->dim)
785                 goto error;
786
787         for (i = 0; i < pw->n; ++i) {
788                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
789                                                         set_type, pos, s);
790                 if (!pw->p[i].set)
791                         goto error;
792                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
793                 if (!pw->p[i].FIELD)
794                         goto error;
795         }
796
797         return pw;
798 error:
799         FN(PW,free)(pw);
800         return NULL;
801 }
802
803 #ifndef NO_DROP_DIMS
804 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
805         enum isl_dim_type type, unsigned first, unsigned n)
806 {
807         int i;
808         enum isl_dim_type set_type;
809
810         if (!pw)
811                 return NULL;
812         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
813                 return pw;
814
815         set_type = type == isl_dim_in ? isl_dim_set : type;
816
817         pw = FN(PW,cow)(pw);
818         if (!pw)
819                 return NULL;
820         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
821         if (!pw->dim)
822                 goto error;
823         for (i = 0; i < pw->n; ++i) {
824                 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
825                 if (!pw->p[i].set)
826                         goto error;
827                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
828                 if (!pw->p[i].FIELD)
829                         goto error;
830         }
831
832         return pw;
833 error:
834         FN(PW,free)(pw);
835         return NULL;
836 }
837
838 /* This function is very similar to drop_dims.
839  * The only difference is that the cells may still involve
840  * the specified dimensions.  They are removed using
841  * isl_set_project_out instead of isl_set_drop.
842  */
843 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
844         enum isl_dim_type type, unsigned first, unsigned n)
845 {
846         int i;
847         enum isl_dim_type set_type;
848
849         if (!pw)
850                 return NULL;
851         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
852                 return pw;
853
854         set_type = type == isl_dim_in ? isl_dim_set : type;
855
856         pw = FN(PW,cow)(pw);
857         if (!pw)
858                 return NULL;
859         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
860         if (!pw->dim)
861                 goto error;
862         for (i = 0; i < pw->n; ++i) {
863                 pw->p[i].set = isl_set_project_out(pw->p[i].set,
864                                                         set_type, first, n);
865                 if (!pw->p[i].set)
866                         goto error;
867                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
868                 if (!pw->p[i].FIELD)
869                         goto error;
870         }
871
872         return pw;
873 error:
874         FN(PW,free)(pw);
875         return NULL;
876 }
877
878 /* Project the domain of pw onto its parameter space.
879  */
880 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
881 {
882         isl_space *space;
883         unsigned n;
884
885         n = FN(PW,dim)(pw, isl_dim_in);
886         pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
887         space = FN(PW,get_domain_space)(pw);
888         space = isl_space_params(space);
889         pw = FN(PW,reset_domain_space)(pw, space);
890         return pw;
891 }
892 #endif
893
894 #ifndef NO_INSERT_DIMS
895 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
896         unsigned first, unsigned n)
897 {
898         int i;
899         enum isl_dim_type set_type;
900
901         if (!pw)
902                 return NULL;
903         if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
904                 return pw;
905
906         set_type = type == isl_dim_in ? isl_dim_set : type;
907
908         pw = FN(PW,cow)(pw);
909         if (!pw)
910                 return NULL;
911
912         pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
913         if (!pw->dim)
914                 goto error;
915
916         for (i = 0; i < pw->n; ++i) {
917                 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
918                                                             set_type, first, n);
919                 if (!pw->p[i].set)
920                         goto error;
921                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
922                                                                 type, first, n);
923                 if (!pw->p[i].FIELD)
924                         goto error;
925         }
926
927         return pw;
928 error:
929         FN(PW,free)(pw);
930         return NULL;
931 }
932 #endif
933
934 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
935         enum isl_dim_type type, unsigned pos, isl_int v)
936 {
937         int i;
938
939         if (!pw)
940                 return NULL;
941
942         if (type == isl_dim_in)
943                 type = isl_dim_set;
944
945         pw = FN(PW,cow)(pw);
946         if (!pw)
947                 return NULL;
948         for (i = 0; i < pw->n; ++i) {
949                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
950                 if (!pw->p[i].set)
951                         goto error;
952         }
953
954         return pw;
955 error:
956         FN(PW,free)(pw);
957         return NULL;
958 }
959
960 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
961 {
962         return pw ? isl_space_dim(pw->dim, type) : 0;
963 }
964
965 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
966         enum isl_dim_type type, unsigned first, unsigned n)
967 {
968         int i;
969
970         if (!pw)
971                 return NULL;
972         if (n == 0)
973                 return pw;
974
975         if (type == isl_dim_in)
976                 type = isl_dim_set;
977
978         pw = FN(PW,cow)(pw);
979         if (!pw)
980                 return NULL;
981         if (!pw->dim)
982                 goto error;
983         for (i = 0; i < pw->n; ++i) {
984                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
985                 if (!pw->p[i].set)
986                         goto error;
987         }
988
989         return pw;
990 error:
991         FN(PW,free)(pw);
992         return NULL;
993 }
994
995 #ifndef NO_OPT
996 /* Compute the maximal value attained by the piecewise quasipolynomial
997  * on its domain or zero if the domain is empty.
998  * In the worst case, the domain is scanned completely,
999  * so the domain is assumed to be bounded.
1000  */
1001 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1002 {
1003         int i;
1004         isl_qpolynomial *opt;
1005
1006         if (!pw)
1007                 return NULL;
1008
1009         if (pw->n == 0) {
1010                 isl_space *dim = isl_space_copy(pw->dim);
1011                 FN(PW,free)(pw);
1012                 return isl_qpolynomial_zero_on_domain(dim);
1013         }
1014
1015         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1016                                         isl_set_copy(pw->p[0].set), max);
1017         for (i = 1; i < pw->n; ++i) {
1018                 isl_qpolynomial *opt_i;
1019                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1020                                                 isl_set_copy(pw->p[i].set), max);
1021                 if (max)
1022                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1023                 else
1024                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1025         }
1026
1027         FN(PW,free)(pw);
1028         return opt;
1029 }
1030
1031 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1032 {
1033         return FN(PW,opt)(pw, 1);
1034 }
1035
1036 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1037 {
1038         return FN(PW,opt)(pw, 0);
1039 }
1040 #endif
1041
1042 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1043 {
1044         return pw ? isl_space_copy(pw->dim) : NULL;
1045 }
1046
1047 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1048 {
1049         return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1050 }
1051
1052 #ifndef NO_RESET_DIM
1053 /* Reset the space of "pw".  Since we don't know if the elements
1054  * represent the spaces themselves or their domains, we pass along
1055  * both when we call their reset_space_and_domain.
1056  */
1057 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1058         __isl_take isl_space *space, __isl_take isl_space *domain)
1059 {
1060         int i;
1061
1062         pw = FN(PW,cow)(pw);
1063         if (!pw || !space || !domain)
1064                 goto error;
1065
1066         for (i = 0; i < pw->n; ++i) {
1067                 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1068                                                  isl_space_copy(domain));
1069                 if (!pw->p[i].set)
1070                         goto error;
1071                 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1072                               isl_space_copy(space), isl_space_copy(domain));
1073                 if (!pw->p[i].FIELD)
1074                         goto error;
1075         }
1076
1077         isl_space_free(domain);
1078
1079         isl_space_free(pw->dim);
1080         pw->dim = space;
1081
1082         return pw;
1083 error:
1084         isl_space_free(domain);
1085         isl_space_free(space);
1086         FN(PW,free)(pw);
1087         return NULL;
1088 }
1089
1090 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1091         __isl_take isl_space *domain)
1092 {
1093         isl_space *space;
1094
1095         space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1096                                                    FN(PW,get_space)(pw));
1097         return FN(PW,reset_space_and_domain)(pw, space, domain);
1098 }
1099
1100 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1101 {
1102         isl_space *domain;
1103
1104         domain = isl_space_domain(isl_space_copy(dim));
1105         return FN(PW,reset_space_and_domain)(pw, dim, domain);
1106 }
1107
1108 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1109         __isl_take isl_id *id)
1110 {
1111         isl_space *space;
1112
1113         pw = FN(PW,cow)(pw);
1114         if (!pw)
1115                 return isl_id_free(id);
1116
1117         space = FN(PW,get_space)(pw);
1118         space = isl_space_set_tuple_id(space, type, id);
1119
1120         return FN(PW,reset_space)(pw, space);
1121 }
1122
1123 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1124         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1125 {
1126         pw = FN(PW,cow)(pw);
1127         if (!pw)
1128                 return isl_id_free(id);
1129         pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1130         return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1131 }
1132 #endif
1133
1134 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1135 {
1136         if (!pw1 || !pw2)
1137                 return -1;
1138
1139         return isl_space_is_equal(pw1->dim, pw2->dim);
1140 }
1141
1142 #ifndef NO_MORPH
1143 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1144         __isl_take isl_morph *morph)
1145 {
1146         int i;
1147         isl_ctx *ctx;
1148
1149         if (!pw || !morph)
1150                 goto error;
1151
1152         ctx = isl_space_get_ctx(pw->dim);
1153         isl_assert(ctx, isl_space_is_domain(morph->dom->dim, pw->dim),
1154                 goto error);
1155
1156         pw = FN(PW,cow)(pw);
1157         if (!pw)
1158                 goto error;
1159         pw->dim = isl_space_extend_domain_with_range(
1160                         isl_space_copy(morph->ran->dim), pw->dim);
1161         if (!pw->dim)
1162                 goto error;
1163
1164         for (i = 0; i < pw->n; ++i) {
1165                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1166                 if (!pw->p[i].set)
1167                         goto error;
1168                 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1169                                                 isl_morph_copy(morph));
1170                 if (!pw->p[i].FIELD)
1171                         goto error;
1172         }
1173
1174         isl_morph_free(morph);
1175
1176         return pw;
1177 error:
1178         FN(PW,free)(pw);
1179         isl_morph_free(morph);
1180         return NULL;
1181 }
1182 #endif
1183
1184 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1185         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1186         void *user)
1187 {
1188         int i;
1189
1190         if (!pw)
1191                 return -1;
1192
1193         for (i = 0; i < pw->n; ++i)
1194                 if (fn(isl_set_copy(pw->p[i].set),
1195                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1196                         return -1;
1197
1198         return 0;
1199 }
1200
1201 #ifndef NO_LIFT
1202 static int any_divs(__isl_keep isl_set *set)
1203 {
1204         int i;
1205
1206         if (!set)
1207                 return -1;
1208
1209         for (i = 0; i < set->n; ++i)
1210                 if (set->p[i]->n_div > 0)
1211                         return 1;
1212
1213         return 0;
1214 }
1215
1216 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1217         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1218                     void *user), void *user)
1219 {
1220         int i;
1221
1222         if (!set || !el)
1223                 goto error;
1224
1225         for (i = 0; i < set->n; ++i) {
1226                 isl_set *lift;
1227                 EL *copy;
1228
1229                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1230                 lift = isl_set_lift(lift);
1231
1232                 copy = FN(EL,copy)(el);
1233                 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1234
1235                 if (fn(lift, copy, user) < 0)
1236                         goto error;
1237         }
1238
1239         isl_set_free(set);
1240         FN(EL,free)(el);
1241
1242         return 0;
1243 error:
1244         isl_set_free(set);
1245         FN(EL,free)(el);
1246         return -1;
1247 }
1248
1249 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1250         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1251                     void *user), void *user)
1252 {
1253         int i;
1254
1255         if (!pw)
1256                 return -1;
1257
1258         for (i = 0; i < pw->n; ++i) {
1259                 isl_set *set;
1260                 EL *el;
1261
1262                 set = isl_set_copy(pw->p[i].set);
1263                 el = FN(EL,copy)(pw->p[i].FIELD);
1264                 if (!any_divs(set)) {
1265                         if (fn(set, el, user) < 0)
1266                                 return -1;
1267                         continue;
1268                 }
1269                 if (foreach_lifted_subset(set, el, fn, user) < 0)
1270                         return -1;
1271         }
1272
1273         return 0;
1274 }
1275 #endif
1276
1277 #ifndef NO_MOVE_DIMS
1278 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1279         enum isl_dim_type dst_type, unsigned dst_pos,
1280         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1281 {
1282         int i;
1283
1284         pw = FN(PW,cow)(pw);
1285         if (!pw)
1286                 return NULL;
1287
1288         pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1289         if (!pw->dim)
1290                 goto error;
1291
1292         for (i = 0; i < pw->n; ++i) {
1293                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1294                                         dst_type, dst_pos, src_type, src_pos, n);
1295                 if (!pw->p[i].FIELD)
1296                         goto error;
1297         }
1298
1299         if (dst_type == isl_dim_in)
1300                 dst_type = isl_dim_set;
1301         if (src_type == isl_dim_in)
1302                 src_type = isl_dim_set;
1303
1304         for (i = 0; i < pw->n; ++i) {
1305                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1306                                                 dst_type, dst_pos,
1307                                                 src_type, src_pos, n);
1308                 if (!pw->p[i].set)
1309                         goto error;
1310         }
1311
1312         return pw;
1313 error:
1314         FN(PW,free)(pw);
1315         return NULL;
1316 }
1317 #endif
1318
1319 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1320 {
1321         int i;
1322
1323         if (isl_int_is_one(v))
1324                 return pw;
1325         if (pw && isl_int_is_zero(v)) {
1326                 PW *zero;
1327                 isl_space *dim = FN(PW,get_space)(pw);
1328 #ifdef HAS_TYPE
1329                 zero = FN(PW,ZERO)(dim, pw->type);
1330 #else
1331                 zero = FN(PW,ZERO)(dim);
1332 #endif
1333                 FN(PW,free)(pw);
1334                 return zero;
1335         }
1336         pw = FN(PW,cow)(pw);
1337         if (!pw)
1338                 return NULL;
1339         if (pw->n == 0)
1340                 return pw;
1341
1342 #ifdef HAS_TYPE
1343         if (isl_int_is_neg(v))
1344                 pw->type = isl_fold_type_negate(pw->type);
1345 #endif
1346         for (i = 0; i < pw->n; ++i) {
1347                 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1348                 if (!pw->p[i].FIELD)
1349                         goto error;
1350         }
1351
1352         return pw;
1353 error:
1354         FN(PW,free)(pw);
1355         return NULL;
1356 }
1357
1358 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1359 {
1360         return FN(PW,mul_isl_int)(pw, v);
1361 }
1362
1363 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1364 {
1365         const isl_set *set1 = *(const isl_set **)p1;
1366         const isl_set *set2 = *(const isl_set **)p2;
1367
1368         return isl_set_plain_cmp(set1, set2);
1369 }
1370
1371 /* We normalize in place, but if anything goes wrong we need
1372  * to return NULL, so we need to make sure we don't change the
1373  * meaning of any possible other copies of map.
1374  */
1375 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1376 {
1377         int i, j;
1378         isl_set *set;
1379
1380         if (!pw)
1381                 return NULL;
1382         for (i = 0; i < pw->n; ++i) {
1383                 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1384                 if (!set)
1385                         return FN(PW,free)(pw);
1386                 isl_set_free(pw->p[i].set);
1387                 pw->p[i].set = set;
1388         }
1389         qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1390         for (i = pw->n - 1; i >= 1; --i) {
1391                 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1392                         continue;
1393                 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1394                         continue;
1395                 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1396                                     isl_set_copy(pw->p[i].set));
1397                 if (!set)
1398                         return FN(PW,free)(pw);
1399                 isl_set_free(pw->p[i].set);
1400                 FN(EL,free)(pw->p[i].FIELD);
1401                 isl_set_free(pw->p[i - 1].set);
1402                 pw->p[i - 1].set = set;
1403                 for (j = i + 1; j < pw->n; ++j)
1404                         pw->p[j - 1] = pw->p[j];
1405                 pw->n--;
1406         }
1407
1408         return pw;
1409 }
1410
1411 /* Is pw1 obviously equal to pw2?
1412  * That is, do they have obviously identical cells and obviously identical
1413  * elements on each cell?
1414  */
1415 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1416 {
1417         int i;
1418         int equal;
1419
1420         if (!pw1 || !pw2)
1421                 return -1;
1422
1423         if (pw1 == pw2)
1424                 return 1;
1425         if (!isl_space_is_equal(pw1->dim, pw2->dim))
1426                 return 0;
1427
1428         pw1 = FN(PW,copy)(pw1);
1429         pw2 = FN(PW,copy)(pw2);
1430         pw1 = FN(PW,normalize)(pw1);
1431         pw2 = FN(PW,normalize)(pw2);
1432         if (!pw1 || !pw2)
1433                 goto error;
1434
1435         equal = pw1->n == pw2->n;
1436         for (i = 0; equal && i < pw1->n; ++i) {
1437                 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1438                 if (equal < 0)
1439                         goto error;
1440                 if (!equal)
1441                         break;
1442                 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1443                 if (equal < 0)
1444                         goto error;
1445         }
1446
1447         FN(PW,free)(pw1);
1448         FN(PW,free)(pw2);
1449         return equal;
1450 error:
1451         FN(PW,free)(pw1);
1452         FN(PW,free)(pw2);
1453         return -1;
1454 }