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