forked from Meinersbur/isl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl_fold.c
2183 lines (1831 loc) · 54.8 KB
/
isl_fold.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010 INRIA Saclay
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
* Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
* 91893 Orsay, France
*/
#include <isl_map_private.h>
#include <isl_union_map_private.h>
#include <isl_polynomial_private.h>
#include <isl_point_private.h>
#include <isl_space_private.h>
#include <isl_lp_private.h>
#include <isl_seq.h>
#include <isl_mat_private.h>
#include <isl_val_private.h>
#include <isl_vec_private.h>
#include <isl_config.h>
#undef EL_BASE
#define EL_BASE pw_qpolynomial_fold
#include <isl_list_templ.c>
enum isl_fold isl_fold_type_negate(enum isl_fold type)
{
switch (type) {
case isl_fold_error:
return isl_fold_error;
case isl_fold_min:
return isl_fold_max;
case isl_fold_max:
return isl_fold_min;
case isl_fold_list:
return isl_fold_list;
}
isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort());
}
/* Construct a new reduction with the given type, domain space and
* list of polynomials.
*/
static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
enum isl_fold type, __isl_take isl_space *space,
__isl_take isl_qpolynomial_list *list)
{
isl_ctx *ctx;
isl_qpolynomial_fold *fold;
if (type < 0 || !space || !list)
goto error;
ctx = isl_space_get_ctx(space);
fold = isl_calloc_type(ctx, struct isl_qpolynomial_fold);
if (!fold)
goto error;
fold->ref = 1;
fold->type = type;
fold->dim = space;
fold->list = list;
return fold;
error:
isl_space_free(space);
isl_qpolynomial_list_free(list);
return NULL;
}
isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
{
return fold ? fold->dim->ctx : NULL;
}
/* Return the domain space of "fold".
*/
static __isl_keep isl_space *isl_qpolynomial_fold_peek_domain_space(
__isl_keep isl_qpolynomial_fold *fold)
{
return fold ? fold->dim : NULL;
}
__isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
__isl_keep isl_qpolynomial_fold *fold)
{
return isl_space_copy(isl_qpolynomial_fold_peek_domain_space(fold));
}
/* Return the space of the domain of "fold".
* This may be either a copy or the space itself
* if there is only one reference to "fold".
* This allows the space to be modified inplace
* if both the expression and its space have only a single reference.
* The caller is not allowed to modify "fold" between this call and
* a subsequent call to isl_qpolynomial_fold_restore_domain_space.
* The only exception is that isl_qpolynomial_fold_free can be called instead.
*/
static __isl_give isl_space *isl_qpolynomial_fold_take_domain_space(
__isl_keep isl_qpolynomial_fold *fold)
{
isl_space *space;
if (!fold)
return NULL;
if (fold->ref != 1)
return isl_qpolynomial_fold_get_domain_space(fold);
space = fold->dim;
fold->dim = NULL;
return space;
}
/* Set the space of the domain of "fold" to "space",
* where the space of "fold" may be missing
* due to a preceding call to isl_qpolynomial_fold_take_domain_space.
* However, in this case, "fold" only has a single reference and
* then the call to isl_qpolynomial_fold_cow has no effect.
*/
static
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_restore_domain_space(
__isl_keep isl_qpolynomial_fold *fold, __isl_take isl_space *space)
{
if (!fold || !space)
goto error;
if (fold->dim == space) {
isl_space_free(space);
return fold;
}
fold = isl_qpolynomial_fold_cow(fold);
if (!fold)
goto error;
isl_space_free(fold->dim);
fold->dim = space;
return fold;
error:
isl_qpolynomial_fold_free(fold);
isl_space_free(space);
return NULL;
}
__isl_give isl_space *isl_qpolynomial_fold_get_space(
__isl_keep isl_qpolynomial_fold *fold)
{
isl_space *space;
if (!fold)
return NULL;
space = isl_space_copy(fold->dim);
space = isl_space_from_domain(space);
space = isl_space_add_dims(space, isl_dim_out, 1);
return space;
}
/* Return the list of polynomials in the reduction "fold".
*/
__isl_keep isl_qpolynomial_list *isl_qpolynomial_fold_peek_list(
__isl_keep isl_qpolynomial_fold *fold)
{
return fold ? fold->list : NULL;
}
/* Return a copy of the list of polynomials in the reduction "fold".
*/
static __isl_give isl_qpolynomial_list *isl_qpolynomial_fold_get_list(
__isl_keep isl_qpolynomial_fold *fold)
{
return isl_qpolynomial_list_copy(isl_qpolynomial_fold_peek_list(fold));
}
/* Return the list of polynomials of "fold".
* This may be either a copy or the list itself
* if there is only one reference to "fold".
* This allows the list to be modified inplace
* if both the expression and its list have only a single reference.
* The caller is not allowed to modify "fold" between this call and
* a subsequent call to isl_qpolynomial_fold_restore_list.
* The only exception is that isl_qpolynomial_fold_free can be called instead.
*/
static __isl_give isl_qpolynomial_list *isl_qpolynomial_fold_take_list(
__isl_keep isl_qpolynomial_fold *fold)
{
isl_qpolynomial_list *list;
if (!fold)
return NULL;
if (fold->ref != 1)
return isl_qpolynomial_fold_get_list(fold);
list = fold->list;
fold->list = NULL;
return list;
}
/* Set the space of the list of polynomials of "fold" to "space",
* where the list of polynomials of "fold" may be missing
* due to a preceding call to isl_qpolynomial_fold_take_list.
* However, in this case, "fold" only has a single reference and
* then the call to isl_qpolynomial_fold_cow has no effect.
*/
static __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_restore_list(
__isl_keep isl_qpolynomial_fold *fold,
__isl_take isl_qpolynomial_list *list)
{
if (!fold || !list)
goto error;
if (fold->list == list) {
isl_qpolynomial_list_free(list);
return fold;
}
fold = isl_qpolynomial_fold_cow(fold);
if (!fold)
goto error;
isl_qpolynomial_list_free(fold->list);
fold->list = list;
return fold;
error:
isl_qpolynomial_fold_free(fold);
isl_qpolynomial_list_free(list);
return NULL;
}
/* isl_qpolynomial_list_map callback that calls
* isl_qpolynomial_reset_domain_space on "qp".
*/
static __isl_give isl_qpolynomial *reset_domain_space(
__isl_take isl_qpolynomial *qp, void *user)
{
isl_space *space = user;
return isl_qpolynomial_reset_domain_space(qp, isl_space_copy(space));
}
/* Replace the domain space of "fold" by "space".
*
* Replace the domain space itself and that of all polynomials
* in the list.
*/
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space)
{
isl_qpolynomial_list *list;
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &reset_domain_space, space);
fold = isl_qpolynomial_fold_restore_list(fold, list);
isl_space_free(isl_qpolynomial_fold_take_domain_space(fold));
fold = isl_qpolynomial_fold_restore_domain_space(fold, space);
return fold;
}
/* Reset the space of "fold". This function is called from isl_pw_templ.c
* and doesn't know if the space of an element object is represented
* directly or through its domain. It therefore passes along both.
*/
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_space_and_domain(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space,
__isl_take isl_space *domain)
{
isl_space_free(space);
return isl_qpolynomial_fold_reset_domain_space(fold, domain);
}
/* Internal data structure for isl_qpolynomial_fold_*_dims
* representing their arguments.
*/
struct isl_fold_dims_data {
enum isl_dim_type type;
unsigned first;
unsigned n;
};
/* isl_qpolynomial_list_every callback that checks whether "qp"
* does not involve any dimensions in the given range.
*/
static isl_bool not_involved(__isl_keep isl_qpolynomial *qp, void *user)
{
struct isl_fold_dims_data *data = user;
isl_bool involves;
involves = isl_qpolynomial_involves_dims(qp, data->type,
data->first, data->n);
return isl_bool_not(involves);
}
/* Does "fold" involve any dimensions in the given range.
*
* It involves any of those dimensions if it is not the case
* that every polynomial in the reduction does not involve
* any of the dimensions.
*/
static isl_bool isl_qpolynomial_fold_involves_dims(
__isl_keep isl_qpolynomial_fold *fold,
enum isl_dim_type type, unsigned first, unsigned n)
{
struct isl_fold_dims_data data = { type, first, n };
isl_qpolynomial_list *list;
isl_bool not;
if (!fold)
return isl_bool_error;
if (n == 0)
return isl_bool_false;
list = isl_qpolynomial_fold_peek_list(fold);
not = isl_qpolynomial_list_every(list, ¬_involved, &data);
return isl_bool_not(not);
}
/* Internal data structure for isl_qpolynomial_fold_set_dim_name
* representing its arguments.
*/
struct isl_fold_set_dim_name_data {
enum isl_dim_type type;
unsigned pos;
const char *s;
};
/* isl_qpolynomial_list_map callback for calling
* isl_qpolynomial_set_dim_name on "qp".
*/
static __isl_give isl_qpolynomial *set_dim_name(__isl_take isl_qpolynomial *qp,
void *user)
{
struct isl_fold_set_dim_name_data *data = user;
qp = isl_qpolynomial_set_dim_name(qp, data->type, data->pos, data->s);
return qp;
}
/* Given a dimension type for an isl_qpolynomial_fold,
* return the corresponding type for the domain.
*/
static enum isl_dim_type domain_type(enum isl_dim_type type)
{
if (type == isl_dim_in)
return isl_dim_set;
return type;
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
__isl_take isl_qpolynomial_fold *fold,
enum isl_dim_type type, unsigned pos, const char *s)
{
struct isl_fold_set_dim_name_data data = { type, pos, s };
enum isl_dim_type set_type;
isl_space *space;
isl_qpolynomial_list *list;
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &set_dim_name, &data);
fold = isl_qpolynomial_fold_restore_list(fold, list);
set_type = domain_type(type);
space = isl_qpolynomial_fold_take_domain_space(fold);
space = isl_space_set_dim_name(space, set_type, pos, s);
fold = isl_qpolynomial_fold_restore_domain_space(fold, space);
return fold;
}
/* isl_qpolynomial_list_map callback for calling
* isl_qpolynomial_drop_dims on "qp".
*/
static __isl_give isl_qpolynomial *drop_dims(__isl_take isl_qpolynomial *qp,
void *user)
{
struct isl_fold_dims_data *data = user;
qp = isl_qpolynomial_drop_dims(qp, data->type, data->first, data->n);
return qp;
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
__isl_take isl_qpolynomial_fold *fold,
enum isl_dim_type type, unsigned first, unsigned n)
{
struct isl_fold_dims_data data = { type, first, n };
enum isl_dim_type set_type;
isl_space *space;
isl_qpolynomial_list *list;
if (!fold)
return NULL;
if (n == 0)
return fold;
set_type = domain_type(type);
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &drop_dims, &data);
fold = isl_qpolynomial_fold_restore_list(fold, list);
space = isl_qpolynomial_fold_take_domain_space(fold);
space = isl_space_drop_dims(space, set_type, first, n);
fold = isl_qpolynomial_fold_restore_domain_space(fold, space);
return fold;
}
/* isl_qpolynomial_list_map callback for calling
* isl_qpolynomial_insert_dims on "qp".
*/
static __isl_give isl_qpolynomial *insert_dims(__isl_take isl_qpolynomial *qp,
void *user)
{
struct isl_fold_dims_data *data = user;
qp = isl_qpolynomial_insert_dims(qp, data->type, data->first, data->n);
return qp;
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
__isl_take isl_qpolynomial_fold *fold,
enum isl_dim_type type, unsigned first, unsigned n)
{
struct isl_fold_dims_data data = { type, first, n };
enum isl_dim_type set_type;
isl_space *space;
isl_qpolynomial_list *list;
if (!fold)
return NULL;
if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
return fold;
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &insert_dims, &data);
fold = isl_qpolynomial_fold_restore_list(fold, list);
set_type = domain_type(type);
space = isl_qpolynomial_fold_take_domain_space(fold);
space = isl_space_insert_dims(space, set_type, first, n);
fold = isl_qpolynomial_fold_restore_domain_space(fold, space);
return fold;
}
/* Determine the sign of the constant quasipolynomial "qp".
*
* Return
* -1 if qp <= 0
* 1 if qp >= 0
* 0 if unknown
*
* For qp == 0, we can return either -1 or 1. In practice, we return 1.
* For qp == NaN, the sign is undefined, so we return 0.
*/
static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
{
isl_poly_cst *cst;
if (isl_qpolynomial_is_nan(qp))
return 0;
cst = isl_poly_as_cst(qp->poly);
if (!cst)
return 0;
return isl_int_sgn(cst->n) < 0 ? -1 : 1;
}
static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
__isl_keep isl_qpolynomial *qp)
{
enum isl_lp_result res;
isl_vec *aff;
isl_int opt;
int sgn = 0;
aff = isl_qpolynomial_extract_affine(qp);
if (!aff)
return 0;
isl_int_init(opt);
res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
&opt, NULL, NULL);
if (res == isl_lp_error)
goto done;
if (res == isl_lp_empty ||
(res == isl_lp_ok && !isl_int_is_neg(opt))) {
sgn = 1;
goto done;
}
res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
&opt, NULL, NULL);
if (res == isl_lp_ok && !isl_int_is_pos(opt))
sgn = -1;
done:
isl_int_clear(opt);
isl_vec_free(aff);
return sgn;
}
/* Determine, if possible, the sign of the quasipolynomial "qp" on
* the domain "set".
*
* If qp is a constant, then the problem is trivial.
* If qp is linear, then we check if the minimum of the corresponding
* affine constraint is non-negative or if the maximum is non-positive.
*
* Otherwise, we check if the outermost variable "v" has a lower bound "l"
* in "set". If so, we write qp(v,v') as
*
* q(v,v') * (v - l) + r(v')
*
* if q(v,v') and r(v') have the same known sign, then the original
* quasipolynomial has the same sign as well.
*
* Return
* -1 if qp <= 0
* 1 if qp >= 0
* 0 if unknown
*/
static int isl_qpolynomial_sign(__isl_keep isl_set *set,
__isl_keep isl_qpolynomial *qp)
{
isl_size d;
int i;
isl_bool is;
isl_poly_rec *rec;
isl_vec *v;
isl_int l;
enum isl_lp_result res;
int sgn = 0;
is = isl_qpolynomial_is_cst(qp, NULL, NULL);
if (is < 0)
return 0;
if (is)
return isl_qpolynomial_cst_sign(qp);
is = isl_qpolynomial_is_affine(qp);
if (is < 0)
return 0;
if (is)
return isl_qpolynomial_aff_sign(set, qp);
if (qp->div->n_row > 0)
return 0;
rec = isl_poly_as_rec(qp->poly);
if (!rec)
return 0;
d = isl_space_dim(qp->dim, isl_dim_all);
if (d < 0)
return 0;
v = isl_vec_alloc(set->ctx, 2 + d);
if (!v)
return 0;
isl_seq_clr(v->el + 1, 1 + d);
isl_int_set_si(v->el[0], 1);
isl_int_set_si(v->el[2 + qp->poly->var], 1);
isl_int_init(l);
res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
if (res == isl_lp_ok) {
isl_qpolynomial *min;
isl_qpolynomial *base;
isl_qpolynomial *r, *q;
isl_qpolynomial *t;
min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
qp->poly->var, 1);
r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
isl_poly_copy(rec->p[rec->n - 1]));
q = isl_qpolynomial_copy(r);
for (i = rec->n - 2; i >= 0; --i) {
r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
isl_poly_copy(rec->p[i]));
r = isl_qpolynomial_add(r, t);
if (i == 0)
break;
q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
}
if (isl_qpolynomial_is_zero(q))
sgn = isl_qpolynomial_sign(set, r);
else if (isl_qpolynomial_is_zero(r))
sgn = isl_qpolynomial_sign(set, q);
else {
int sgn_q, sgn_r;
sgn_r = isl_qpolynomial_sign(set, r);
sgn_q = isl_qpolynomial_sign(set, q);
if (sgn_r == sgn_q)
sgn = sgn_r;
}
isl_qpolynomial_free(min);
isl_qpolynomial_free(base);
isl_qpolynomial_free(q);
isl_qpolynomial_free(r);
}
isl_int_clear(l);
isl_vec_free(v);
return sgn;
}
/* Check that "fold1" and "fold2" have the same type.
*/
static isl_stat isl_qpolynomial_fold_check_equal_type(
__isl_keep isl_qpolynomial_fold *fold1,
__isl_keep isl_qpolynomial_fold *fold2)
{
enum isl_fold type1, type2;
type1 = isl_qpolynomial_fold_get_type(fold1);
type2 = isl_qpolynomial_fold_get_type(fold2);
if (type1 < 0 || type2 < 0)
return isl_stat_error;
if (type1 != type2)
isl_die(isl_qpolynomial_fold_get_ctx(fold1), isl_error_invalid,
"fold types don't match", return isl_stat_error);
return isl_stat_ok;
}
/* Check that "fold1" and "fold2" have the same (domain) space.
*/
static isl_stat isl_qpolynomial_fold_check_equal_space(
__isl_keep isl_qpolynomial_fold *fold1,
__isl_keep isl_qpolynomial_fold *fold2)
{
isl_bool equal;
isl_space *space1, *space2;
space1 = isl_qpolynomial_fold_peek_domain_space(fold1);
space2 = isl_qpolynomial_fold_peek_domain_space(fold2);
equal = isl_space_is_equal(space1, space2);
if (equal < 0)
return isl_stat_error;
if (!equal)
isl_die(isl_qpolynomial_fold_get_ctx(fold1), isl_error_invalid,
"spaces don't match", return isl_stat_error);
return isl_stat_ok;
}
/* Combine "list1" and "list2" into a single list, eliminating
* those elements of one list that are already covered by the other
* list on "set".
*
* "better" is the sign that the difference qp1 - qp2 needs to have for qp1
* to be covered by qp2.
*/
static __isl_give isl_qpolynomial_list *merge_lists(__isl_keep isl_set *set,
__isl_take isl_qpolynomial_list *list1,
__isl_take isl_qpolynomial_list *list2, int better)
{
int i, j;
isl_size n1, n2;
n1 = isl_qpolynomial_list_size(list1);
n2 = isl_qpolynomial_list_size(list2);
if (n1 < 0 || n2 < 0)
goto error;
for (i = n2 - 1; i >= 0; --i) {
for (j = n1 - 1; j >= 0; --j) {
isl_qpolynomial *qp1, *qp2, *d;
int sgn;
isl_bool equal;
qp1 = isl_qpolynomial_list_peek(list1, j);
qp2 = isl_qpolynomial_list_peek(list2, i);
equal = isl_qpolynomial_plain_is_equal(qp1, qp2);
if (equal < 0)
goto error;
if (equal)
break;
d = isl_qpolynomial_sub(
isl_qpolynomial_copy(qp1),
isl_qpolynomial_copy(qp2));
sgn = isl_qpolynomial_sign(set, d);
isl_qpolynomial_free(d);
if (sgn == 0)
continue;
if (sgn != better)
break;
list1 = isl_qpolynomial_list_drop(list1, j, 1);
n1--;
}
if (j < 0)
continue;
list2 = isl_qpolynomial_list_drop(list2, i, 1);
n2--;
}
return isl_qpolynomial_list_concat(list1, list2);
error:
isl_qpolynomial_list_free(list1);
isl_qpolynomial_list_free(list2);
return NULL;
}
/* Combine "fold1" and "fold2" into a single reduction, eliminating
* those elements of one reduction that are already covered by the other
* reduction on "set".
*
* If "fold1" or "fold2" is an empty reduction, then return
* the other reduction.
* If "fold1" or "fold2" is a NaN, then return this NaN.
*/
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
__isl_keep isl_set *set,
__isl_take isl_qpolynomial_fold *fold1,
__isl_take isl_qpolynomial_fold *fold2)
{
isl_qpolynomial_list *list1;
isl_qpolynomial_list *list2;
int better;
if (isl_qpolynomial_fold_check_equal_type(fold1, fold2) < 0)
goto error;
if (isl_qpolynomial_fold_check_equal_space(fold1, fold2) < 0)
goto error;
better = fold1->type == isl_fold_max ? -1 : 1;
if (isl_qpolynomial_fold_is_empty(fold1) ||
isl_qpolynomial_fold_is_nan(fold2)) {
isl_qpolynomial_fold_free(fold1);
return fold2;
}
if (isl_qpolynomial_fold_is_empty(fold2) ||
isl_qpolynomial_fold_is_nan(fold1)) {
isl_qpolynomial_fold_free(fold2);
return fold1;
}
list1 = isl_qpolynomial_fold_take_list(fold1);
list2 = isl_qpolynomial_fold_take_list(fold2);
list1 = merge_lists(set, list1, list2, better);
fold1 = isl_qpolynomial_fold_restore_list(fold1, list1);
isl_qpolynomial_fold_free(fold2);
return fold1;
error:
isl_qpolynomial_fold_free(fold1);
isl_qpolynomial_fold_free(fold2);
return NULL;
}
/* isl_qpolynomial_list_map callback for adding "qp2" to "qp".
*/
static __isl_give isl_qpolynomial *add_qpolynomial(
__isl_take isl_qpolynomial *qp, void *user)
{
isl_qpolynomial *qp2 = user;
return isl_qpolynomial_add(qp, isl_qpolynomial_copy(qp2));
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
{
isl_qpolynomial_list *list;
if (!fold || !qp)
goto error;
if (isl_qpolynomial_is_zero(qp)) {
isl_qpolynomial_free(qp);
return fold;
}
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &add_qpolynomial, qp);
fold = isl_qpolynomial_fold_restore_list(fold, list);
isl_qpolynomial_free(qp);
return fold;
error:
isl_qpolynomial_fold_free(fold);
isl_qpolynomial_free(qp);
return NULL;
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
__isl_keep isl_set *dom,
__isl_take isl_qpolynomial_fold *fold1,
__isl_take isl_qpolynomial_fold *fold2)
{
int i;
isl_size n1, n2;
isl_qpolynomial_fold *res = NULL;
isl_qpolynomial *qp;
isl_qpolynomial_list *list1, *list2;
if (!fold1 || !fold2)
goto error;
if (isl_qpolynomial_fold_is_empty(fold1)) {
isl_qpolynomial_fold_free(fold1);
return fold2;
}
if (isl_qpolynomial_fold_is_empty(fold2)) {
isl_qpolynomial_fold_free(fold2);
return fold1;
}
list1 = isl_qpolynomial_fold_peek_list(fold1);
list2 = isl_qpolynomial_fold_peek_list(fold2);
n1 = isl_qpolynomial_list_size(list1);
n2 = isl_qpolynomial_list_size(list2);
if (n1 < 0 || n2 < 0)
goto error;
if (n1 == 1 && n2 != 1)
return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
qp = isl_qpolynomial_list_get_at(list2, 0);
if (n2 == 1) {
res = isl_qpolynomial_fold_add_qpolynomial(fold1, qp);
isl_qpolynomial_fold_free(fold2);
return res;
}
res = isl_qpolynomial_fold_add_qpolynomial(
isl_qpolynomial_fold_copy(fold1), qp);
for (i = 1; i < n2; ++i) {
isl_qpolynomial_fold *res_i;
qp = isl_qpolynomial_list_get_at(list2, i);
res_i = isl_qpolynomial_fold_add_qpolynomial(
isl_qpolynomial_fold_copy(fold1), qp);
res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
}
isl_qpolynomial_fold_free(fold1);
isl_qpolynomial_fold_free(fold2);
return res;
error:
isl_qpolynomial_fold_free(res);
isl_qpolynomial_fold_free(fold1);
isl_qpolynomial_fold_free(fold2);
return NULL;
}
/* isl_qpolynomial_list_map callback for calling
* isl_qpolynomial_substitute_equalities on "qp" and "eq".
*/
static __isl_give isl_qpolynomial *substitute_equalities(
__isl_take isl_qpolynomial *qp, void *user)
{
isl_basic_set *eq = user;
eq = isl_basic_set_copy(eq);
return isl_qpolynomial_substitute_equalities(qp, eq);
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
{
isl_qpolynomial_list *list;
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &substitute_equalities, eq);
fold = isl_qpolynomial_fold_restore_list(fold, list);
isl_basic_set_free(eq);
return fold;
}
/* isl_qpolynomial_list_map callback for calling
* isl_qpolynomial_substitute_equalities on "qp" and "context".
*/
static __isl_give isl_qpolynomial *gist(__isl_take isl_qpolynomial *qp,
void *user)
{
isl_set *context = user;
return isl_qpolynomial_gist(qp, isl_set_copy(context));
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
{
isl_qpolynomial_list *list;
list = isl_qpolynomial_fold_take_list(fold);
list = isl_qpolynomial_list_map(list, &gist, context);
fold = isl_qpolynomial_fold_restore_list(fold, list);
isl_set_free(context);
return fold;
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
__isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
{
isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
isl_set *dom_context = isl_set_universe(space);
dom_context = isl_set_intersect_params(dom_context, context);
return isl_qpolynomial_fold_gist(fold, dom_context);
}
/* Return a zero (i.e., empty) isl_qpolynomial_fold in the given space.
*
* This is a helper function for isl_pw_*_as_* that ensures a uniform
* interface over all piecewise types.
*/
static __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_zero_in_space(
__isl_take isl_space *space, enum isl_fold type)
{
return isl_qpolynomial_fold_empty(type, isl_space_domain(space));
}
#define isl_qpolynomial_fold_involves_nan isl_qpolynomial_fold_is_nan
#define HAS_TYPE
#undef PW
#define PW isl_pw_qpolynomial_fold
#undef BASE
#define BASE qpolynomial_fold
#undef EL_IS_ZERO
#define EL_IS_ZERO is_empty
#undef ZERO
#define ZERO zero
#undef IS_ZERO
#define IS_ZERO is_zero
#undef FIELD
#define FIELD fold
#undef DEFAULT_IS_ZERO
#define DEFAULT_IS_ZERO 1
#include <isl_pw_templ.c>
#include <isl_pw_eval.c>
#include <isl_pw_insert_dims_templ.c>
#include <isl_pw_lift_templ.c>
#include <isl_pw_morph_templ.c>
#include <isl_pw_move_dims_templ.c>
#include <isl_pw_opt_templ.c>
#undef BASE
#define BASE pw_qpolynomial_fold
#define NO_SUB
#include <isl_union_single.c>
#include <isl_union_eval.c>
/* Construct a new reduction of the given type and space
* with an empty list of polynomials.
*/
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
__isl_take isl_space *space)
{
isl_ctx *ctx;
isl_qpolynomial_list *list;
if (!space)
return NULL;
ctx = isl_space_get_ctx(space);
list = isl_qpolynomial_list_alloc(ctx, 0);
return qpolynomial_fold_alloc(type, space, list);
}
/* Construct a new reduction of the given type and
* a single given polynomial.
*/
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
enum isl_fold type, __isl_take isl_qpolynomial *qp)
{
isl_space *space;
isl_qpolynomial_list *list;
space = isl_qpolynomial_get_domain_space(qp);
list = isl_qpolynomial_list_from_qpolynomial(qp);
return qpolynomial_fold_alloc(type, space, list);
}
__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
__isl_keep isl_qpolynomial_fold *fold)