-
Notifications
You must be signed in to change notification settings - Fork 26
/
isl_aff.c
10050 lines (8657 loc) · 265 KB
/
isl_aff.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 2011 INRIA Saclay
* Copyright 2011 Sven Verdoolaege
* Copyright 2012-2014 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
* Copyright 2016 Sven Verdoolaege
* Copyright 2018,2020 Cerebras Systems
* Copyright 2021 Sven Verdoolaege
* Copyright 2022 Cerebras Systems
*
* 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
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
* and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
* and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
*/
#include <isl_ctx_private.h>
#include <isl_map_private.h>
#include <isl_union_map_private.h>
#include <isl_aff_private.h>
#include <isl_space_private.h>
#include <isl_local_space_private.h>
#include <isl_vec_private.h>
#include <isl_mat_private.h>
#include <isl_id_private.h>
#include <isl/constraint.h>
#include <isl_seq.h>
#include <isl/set.h>
#include <isl_val_private.h>
#include <isl_point_private.h>
#include <isl_config.h>
#undef EL_BASE
#define EL_BASE aff
#include <isl_list_templ.c>
#include <isl_list_read_templ.c>
#undef EL_BASE
#define EL_BASE pw_aff
#include <isl_list_templ.c>
#include <isl_list_read_templ.c>
#undef EL_BASE
#define EL_BASE pw_multi_aff
#include <isl_list_templ.c>
#include <isl_list_read_templ.c>
#undef EL_BASE
#define EL_BASE union_pw_aff
#include <isl_list_templ.c>
#include <isl_list_read_templ.c>
#undef EL_BASE
#define EL_BASE union_pw_multi_aff
#include <isl_list_templ.c>
/* Construct an isl_aff from the given domain local space "ls" and
* coefficients "v", where the local space is known to be valid
* for an affine expression.
*/
static __isl_give isl_aff *isl_aff_alloc_vec_validated(
__isl_take isl_local_space *ls, __isl_take isl_vec *v)
{
isl_aff *aff;
if (!ls || !v)
goto error;
aff = isl_calloc_type(v->ctx, struct isl_aff);
if (!aff)
goto error;
aff->ref = 1;
aff->ls = ls;
aff->v = v;
return aff;
error:
isl_local_space_free(ls);
isl_vec_free(v);
return NULL;
}
/* Construct an isl_aff from the given domain local space "ls" and
* coefficients "v".
*
* First check that "ls" is a valid domain local space
* for an affine expression.
*/
__isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
__isl_take isl_vec *v)
{
isl_ctx *ctx;
if (!ls)
return NULL;
ctx = isl_local_space_get_ctx(ls);
if (!isl_local_space_divs_known(ls))
isl_die(ctx, isl_error_invalid, "local space has unknown divs",
goto error);
if (!isl_local_space_is_set(ls))
isl_die(ctx, isl_error_invalid,
"domain of affine expression should be a set",
goto error);
return isl_aff_alloc_vec_validated(ls, v);
error:
isl_local_space_free(ls);
isl_vec_free(v);
return NULL;
}
__isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
{
isl_ctx *ctx;
isl_vec *v;
isl_size total;
if (!ls)
return NULL;
ctx = isl_local_space_get_ctx(ls);
total = isl_local_space_dim(ls, isl_dim_all);
if (total < 0)
goto error;
v = isl_vec_alloc(ctx, 1 + 1 + total);
return isl_aff_alloc_vec(ls, v);
error:
isl_local_space_free(ls);
return NULL;
}
__isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
{
if (!aff)
return NULL;
aff->ref++;
return aff;
}
__isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
{
if (!aff)
return NULL;
return isl_aff_alloc_vec_validated(isl_local_space_copy(aff->ls),
isl_vec_copy(aff->v));
}
__isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
{
if (!aff)
return NULL;
if (aff->ref == 1)
return aff;
aff->ref--;
return isl_aff_dup(aff);
}
__isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
{
isl_aff *aff;
aff = isl_aff_alloc(ls);
if (!aff)
return NULL;
isl_int_set_si(aff->v->el[0], 1);
isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
return aff;
}
/* Return an affine expression that is equal to zero on domain space "space".
*/
__isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
{
return isl_aff_zero_on_domain(isl_local_space_from_space(space));
}
/* This function performs the same operation as isl_aff_zero_on_domain_space,
* but is considered as a function on an isl_space when exported.
*/
__isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
{
return isl_aff_zero_on_domain_space(space);
}
/* Return a piecewise affine expression defined on the specified domain
* that is equal to zero.
*/
__isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
{
return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
}
/* Change "aff" into a NaN.
*
* Note that this function gets called from isl_aff_nan_on_domain,
* so "aff" may not have been initialized yet.
*/
static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
{
aff = isl_aff_cow(aff);
if (!aff)
return NULL;
aff->v = isl_vec_clr(aff->v);
if (!aff->v)
return isl_aff_free(aff);
return aff;
}
/* Return an affine expression defined on the specified domain
* that represents NaN.
*/
__isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
{
isl_aff *aff;
aff = isl_aff_alloc(ls);
return isl_aff_set_nan(aff);
}
/* Return an affine expression defined on the specified domain space
* that represents NaN.
*/
__isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
{
return isl_aff_nan_on_domain(isl_local_space_from_space(space));
}
/* Return a piecewise affine expression defined on the specified domain space
* that represents NaN.
*/
__isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
__isl_take isl_space *space)
{
return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
}
/* Return a piecewise affine expression defined on the specified domain
* that represents NaN.
*/
__isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
{
return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
}
/* Return an affine expression that is equal to "val" on
* domain local space "ls".
*
* Note that the encoding for the special value NaN
* is the same in isl_val and isl_aff, so this does not need
* to be treated in any special way.
*/
__isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
__isl_take isl_val *val)
{
isl_aff *aff;
if (!ls || !val)
goto error;
if (!isl_val_is_rat(val) && !isl_val_is_nan(val))
isl_die(isl_val_get_ctx(val), isl_error_invalid,
"expecting rational value or NaN", goto error);
aff = isl_aff_alloc(isl_local_space_copy(ls));
if (!aff)
goto error;
isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
isl_int_set(aff->v->el[1], val->n);
isl_int_set(aff->v->el[0], val->d);
isl_local_space_free(ls);
isl_val_free(val);
return aff;
error:
isl_local_space_free(ls);
isl_val_free(val);
return NULL;
}
/* Return an affine expression that is equal to "val" on domain space "space".
*/
__isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
__isl_take isl_val *val)
{
return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
}
/* Return an affine expression that is equal to the specified dimension
* in "ls".
*/
__isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
enum isl_dim_type type, unsigned pos)
{
isl_space *space;
isl_aff *aff;
if (!ls)
return NULL;
space = isl_local_space_get_space(ls);
if (!space)
goto error;
if (isl_space_is_map(space))
isl_die(isl_space_get_ctx(space), isl_error_invalid,
"expecting (parameter) set space", goto error);
if (isl_local_space_check_range(ls, type, pos, 1) < 0)
goto error;
isl_space_free(space);
aff = isl_aff_alloc(ls);
if (!aff)
return NULL;
pos += isl_local_space_offset(aff->ls, type);
isl_int_set_si(aff->v->el[0], 1);
isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
isl_int_set_si(aff->v->el[1 + pos], 1);
return aff;
error:
isl_local_space_free(ls);
isl_space_free(space);
return NULL;
}
/* Return a piecewise affine expression that is equal to
* the specified dimension in "ls".
*/
__isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
enum isl_dim_type type, unsigned pos)
{
return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
}
/* Return an affine expression that is equal to the parameter
* in the domain space "space" with identifier "id".
*/
__isl_give isl_aff *isl_aff_param_on_domain_space_id(
__isl_take isl_space *space, __isl_take isl_id *id)
{
int pos;
isl_local_space *ls;
if (!space || !id)
goto error;
pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
if (pos < 0)
isl_die(isl_space_get_ctx(space), isl_error_invalid,
"parameter not found in space", goto error);
isl_id_free(id);
ls = isl_local_space_from_space(space);
return isl_aff_var_on_domain(ls, isl_dim_param, pos);
error:
isl_space_free(space);
isl_id_free(id);
return NULL;
}
/* This function performs the same operation as
* isl_aff_param_on_domain_space_id,
* but is considered as a function on an isl_space when exported.
*/
__isl_give isl_aff *isl_space_param_aff_on_domain_id(
__isl_take isl_space *space, __isl_take isl_id *id)
{
return isl_aff_param_on_domain_space_id(space, id);
}
__isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
{
if (!aff)
return NULL;
if (--aff->ref > 0)
return NULL;
isl_local_space_free(aff->ls);
isl_vec_free(aff->v);
free(aff);
return NULL;
}
isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
{
return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
}
/* Return a hash value that digests "aff".
*/
uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
{
uint32_t hash, ls_hash, v_hash;
if (!aff)
return 0;
hash = isl_hash_init();
ls_hash = isl_local_space_get_hash(aff->ls);
isl_hash_hash(hash, ls_hash);
v_hash = isl_vec_get_hash(aff->v);
isl_hash_hash(hash, v_hash);
return hash;
}
/* Return the domain local space of "aff".
*/
static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
__isl_keep isl_aff *aff)
{
return aff ? aff->ls : NULL;
}
/* Return the number of variables of the given type in the domain of "aff".
*/
isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
{
isl_local_space *ls;
ls = isl_aff_peek_domain_local_space(aff);
return isl_local_space_dim(ls, type);
}
/* Externally, an isl_aff has a map space, but internally, the
* ls field corresponds to the domain of that space.
*/
isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
{
if (!aff)
return isl_size_error;
if (type == isl_dim_out)
return 1;
if (type == isl_dim_in)
type = isl_dim_set;
return isl_aff_domain_dim(aff, type);
}
/* Return the offset of the first coefficient of type "type" in
* the domain of "aff".
*/
isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
{
isl_local_space *ls;
ls = isl_aff_peek_domain_local_space(aff);
return isl_local_space_offset(ls, type);
}
/* Return the position of the dimension of the given type and name
* in "aff".
* Return -1 if no such dimension can be found.
*/
int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
const char *name)
{
if (!aff)
return -1;
if (type == isl_dim_out)
return -1;
if (type == isl_dim_in)
type = isl_dim_set;
return isl_local_space_find_dim_by_name(aff->ls, type, name);
}
/* Return the domain space of "aff".
*/
static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
{
return aff ? isl_local_space_peek_space(aff->ls) : NULL;
}
__isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
{
return isl_space_copy(isl_aff_peek_domain_space(aff));
}
__isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
{
isl_space *space;
if (!aff)
return NULL;
space = isl_local_space_get_space(aff->ls);
space = isl_space_from_domain(space);
space = isl_space_add_dims(space, isl_dim_out, 1);
return space;
}
/* Return a copy of the domain space of "aff".
*/
__isl_give isl_local_space *isl_aff_get_domain_local_space(
__isl_keep isl_aff *aff)
{
return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
}
__isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
{
isl_local_space *ls;
if (!aff)
return NULL;
ls = isl_local_space_copy(aff->ls);
ls = isl_local_space_from_domain(ls);
ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
return ls;
}
/* Return the local space of the domain of "aff".
* This may be either a copy or the local space itself
* if there is only one reference to "aff".
* This allows the local space to be modified inplace
* if both the expression and its local space have only a single reference.
* The caller is not allowed to modify "aff" between this call and
* a subsequent call to isl_aff_restore_domain_local_space.
* The only exception is that isl_aff_free can be called instead.
*/
__isl_give isl_local_space *isl_aff_take_domain_local_space(
__isl_keep isl_aff *aff)
{
isl_local_space *ls;
if (!aff)
return NULL;
if (aff->ref != 1)
return isl_aff_get_domain_local_space(aff);
ls = aff->ls;
aff->ls = NULL;
return ls;
}
/* Set the local space of the domain of "aff" to "ls",
* where the local space of "aff" may be missing
* due to a preceding call to isl_aff_take_domain_local_space.
* However, in this case, "aff" only has a single reference and
* then the call to isl_aff_cow has no effect.
*/
__isl_give isl_aff *isl_aff_restore_domain_local_space(
__isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
{
if (!aff || !ls)
goto error;
if (aff->ls == ls) {
isl_local_space_free(ls);
return aff;
}
aff = isl_aff_cow(aff);
if (!aff)
goto error;
isl_local_space_free(aff->ls);
aff->ls = ls;
return aff;
error:
isl_aff_free(aff);
isl_local_space_free(ls);
return NULL;
}
/* Externally, an isl_aff has a map space, but internally, the
* ls field corresponds to the domain of that space.
*/
const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
enum isl_dim_type type, unsigned pos)
{
if (!aff)
return NULL;
if (type == isl_dim_out)
return NULL;
if (type == isl_dim_in)
type = isl_dim_set;
return isl_local_space_get_dim_name(aff->ls, type, pos);
}
__isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
__isl_take isl_space *space)
{
aff = isl_aff_cow(aff);
if (!aff || !space)
goto error;
aff->ls = isl_local_space_reset_space(aff->ls, space);
if (!aff->ls)
return isl_aff_free(aff);
return aff;
error:
isl_aff_free(aff);
isl_space_free(space);
return NULL;
}
/* Reset the space of "aff". 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_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
__isl_take isl_space *space, __isl_take isl_space *domain)
{
isl_space_free(space);
return isl_aff_reset_domain_space(aff, domain);
}
/* Reorder the dimensions of the domain of "aff" according
* to the given reordering.
*/
__isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
__isl_take isl_reordering *r)
{
aff = isl_aff_cow(aff);
if (!aff)
goto error;
r = isl_reordering_extend(r, aff->ls->div->n_row);
aff->v = isl_vec_reorder(aff->v, 2, isl_reordering_copy(r));
aff->ls = isl_local_space_realign(aff->ls, r);
if (!aff->v || !aff->ls)
return isl_aff_free(aff);
return aff;
error:
isl_aff_free(aff);
isl_reordering_free(r);
return NULL;
}
__isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
__isl_take isl_space *model)
{
isl_space *domain_space;
isl_bool equal_params;
domain_space = isl_aff_peek_domain_space(aff);
equal_params = isl_space_has_equal_params(domain_space, model);
if (equal_params < 0)
goto error;
if (!equal_params) {
isl_reordering *exp;
exp = isl_parameter_alignment_reordering(domain_space, model);
aff = isl_aff_realign_domain(aff, exp);
}
isl_space_free(model);
return aff;
error:
isl_space_free(model);
isl_aff_free(aff);
return NULL;
}
#undef TYPE
#define TYPE isl_aff
#include "isl_unbind_params_templ.c"
/* Is "aff" obviously equal to zero?
*
* If the denominator is zero, then "aff" is not equal to zero.
*/
isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
{
int pos;
if (!aff)
return isl_bool_error;
if (isl_int_is_zero(aff->v->el[0]))
return isl_bool_false;
pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
return isl_bool_ok(pos < 0);
}
/* Does "aff" represent NaN?
*/
isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
{
if (!aff)
return isl_bool_error;
return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
}
/* Are "aff1" and "aff2" obviously equal?
*
* NaN is not equal to anything, not even to another NaN.
*/
isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
__isl_keep isl_aff *aff2)
{
isl_bool equal;
if (!aff1 || !aff2)
return isl_bool_error;
if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
return isl_bool_false;
equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
if (equal < 0 || !equal)
return equal;
return isl_vec_is_equal(aff1->v, aff2->v);
}
/* Return the common denominator of "aff" in "v".
*
* We cannot return anything meaningful in case of a NaN.
*/
isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
{
if (!aff)
return isl_stat_error;
if (isl_aff_is_nan(aff))
isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
"cannot get denominator of NaN", return isl_stat_error);
isl_int_set(*v, aff->v->el[0]);
return isl_stat_ok;
}
/* Return the common denominator of "aff".
*/
__isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
{
isl_ctx *ctx;
if (!aff)
return NULL;
ctx = isl_aff_get_ctx(aff);
if (isl_aff_is_nan(aff))
return isl_val_nan(ctx);
return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
}
/* Return the constant term of "aff".
*/
__isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
{
isl_ctx *ctx;
isl_val *v;
if (!aff)
return NULL;
ctx = isl_aff_get_ctx(aff);
if (isl_aff_is_nan(aff))
return isl_val_nan(ctx);
v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
return isl_val_normalize(v);
}
/* Return the coefficient of the variable of type "type" at position "pos"
* of "aff".
*/
__isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
enum isl_dim_type type, int pos)
{
isl_ctx *ctx;
isl_val *v;
if (!aff)
return NULL;
ctx = isl_aff_get_ctx(aff);
if (type == isl_dim_out)
isl_die(ctx, isl_error_invalid,
"output/set dimension does not have a coefficient",
return NULL);
if (type == isl_dim_in)
type = isl_dim_set;
if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
return NULL;
if (isl_aff_is_nan(aff))
return isl_val_nan(ctx);
pos += isl_local_space_offset(aff->ls, type);
v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
return isl_val_normalize(v);
}
/* Return the sign of the coefficient of the variable of type "type"
* at position "pos" of "aff".
*/
int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
int pos)
{
isl_ctx *ctx;
if (!aff)
return 0;
ctx = isl_aff_get_ctx(aff);
if (type == isl_dim_out)
isl_die(ctx, isl_error_invalid,
"output/set dimension does not have a coefficient",
return 0);
if (type == isl_dim_in)
type = isl_dim_set;
if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
return 0;
pos += isl_local_space_offset(aff->ls, type);
return isl_int_sgn(aff->v->el[1 + pos]);
}
/* Replace the numerator of the constant term of "aff" by "v".
*
* A NaN is unaffected by this operation.
*/
__isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
{
if (!aff)
return NULL;
if (isl_aff_is_nan(aff))
return aff;
aff = isl_aff_cow(aff);
if (!aff)
return NULL;
aff->v = isl_vec_cow(aff->v);
if (!aff->v)
return isl_aff_free(aff);
isl_int_set(aff->v->el[1], v);
return aff;
}
/* Replace the constant term of "aff" by "v".
*
* A NaN is unaffected by this operation.
*/
__isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
__isl_take isl_val *v)
{
if (!aff || !v)
goto error;
if (isl_aff_is_nan(aff)) {
isl_val_free(v);
return aff;
}
if (!isl_val_is_rat(v))
isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
"expecting rational value", goto error);
if (isl_int_eq(aff->v->el[1], v->n) &&
isl_int_eq(aff->v->el[0], v->d)) {
isl_val_free(v);
return aff;
}
aff = isl_aff_cow(aff);
if (!aff)
goto error;
aff->v = isl_vec_cow(aff->v);
if (!aff->v)
goto error;
if (isl_int_eq(aff->v->el[0], v->d)) {
isl_int_set(aff->v->el[1], v->n);
} else if (isl_int_is_one(v->d)) {
isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
} else {
isl_seq_scale(aff->v->el + 1,
aff->v->el + 1, v->d, aff->v->size - 1);
isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
aff->v = isl_vec_normalize(aff->v);
if (!aff->v)
goto error;
}
isl_val_free(v);
return aff;
error:
isl_aff_free(aff);
isl_val_free(v);
return NULL;
}
/* Add "v" to the constant term of "aff".
*
* A NaN is unaffected by this operation.
*/
__isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
{
if (isl_int_is_zero(v))
return aff;
if (!aff)
return NULL;
if (isl_aff_is_nan(aff))
return aff;
aff = isl_aff_cow(aff);
if (!aff)
return NULL;
aff->v = isl_vec_cow(aff->v);
if (!aff->v)
return isl_aff_free(aff);
isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
return aff;
}
/* Add "v" to the constant term of "aff",
* in case "aff" is a rational expression.
*/
static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
__isl_take isl_val *v)
{
aff = isl_aff_cow(aff);
if (!aff)
goto error;
aff->v = isl_vec_cow(aff->v);
if (!aff->v)
goto error;
if (isl_int_is_one(v->d)) {
isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
} else if (isl_int_eq(aff->v->el[0], v->d)) {
isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
aff->v = isl_vec_normalize(aff->v);
if (!aff->v)
goto error;
} else {
isl_seq_scale(aff->v->el + 1,
aff->v->el + 1, v->d, aff->v->size - 1);
isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
aff->v = isl_vec_normalize(aff->v);
if (!aff->v)
goto error;
}
isl_val_free(v);
return aff;
error:
isl_aff_free(aff);
isl_val_free(v);
return NULL;
}
/* Return the first argument and free the second.
*/
static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
__isl_take isl_val *v)
{
isl_val_free(v);
return aff;
}
/* Replace the first argument by NaN and free the second argument.
*/
static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
__isl_take isl_val *v)
{
isl_val_free(v);
return isl_aff_set_nan(aff);
}
/* Add "v" to the constant term of "aff".
*
* A NaN is unaffected by this operation.
* Conversely, adding a NaN turns "aff" into a NaN.
*/
__isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
__isl_take isl_val *v)
{
isl_bool is_nan, is_zero, is_rat;