isl_equalities.c: fix typo in comment
[isl.git] / isl_dim.c
blobde53db234de9c872335e5f18f4bdf7204bcebece
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_dim.h>
11 #include "isl_name.h"
13 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
14 unsigned nparam, unsigned n_in, unsigned n_out)
16 struct isl_dim *dim;
18 dim = isl_alloc_type(ctx, struct isl_dim);
19 if (!dim)
20 return NULL;
22 dim->ctx = ctx;
23 isl_ctx_ref(ctx);
24 dim->ref = 1;
25 dim->nparam = nparam;
26 dim->n_in = n_in;
27 dim->n_out = n_out;
29 dim->n_name = 0;
30 dim->names = NULL;
32 return dim;
35 struct isl_dim *isl_dim_set_alloc(struct isl_ctx *ctx,
36 unsigned nparam, unsigned dim)
38 return isl_dim_alloc(ctx, nparam, 0, dim);
41 static unsigned global_pos(struct isl_dim *dim,
42 enum isl_dim_type type, unsigned pos)
44 struct isl_ctx *ctx = dim->ctx;
46 switch (type) {
47 case isl_dim_param:
48 isl_assert(ctx, pos < dim->nparam, return isl_dim_total(dim));
49 return pos;
50 case isl_dim_in:
51 isl_assert(ctx, pos < dim->n_in, return isl_dim_total(dim));
52 return pos + dim->nparam;
53 case isl_dim_out:
54 isl_assert(ctx, pos < dim->n_out, return isl_dim_total(dim));
55 return pos + dim->nparam + dim->n_in;
56 default:
57 isl_assert(ctx, 0, return isl_dim_total(dim));
59 return isl_dim_total(dim);
62 /* Extend length of names array to the total number of dimensions.
64 static __isl_give isl_dim *extend_names(__isl_take isl_dim *dim)
66 struct isl_name **names;
67 int i;
69 if (isl_dim_total(dim) <= dim->n_name)
70 return dim;
72 if (!dim->names) {
73 dim->names = isl_calloc_array(dim->ctx,
74 struct isl_name *, isl_dim_total(dim));
75 if (!dim->names)
76 goto error;
77 } else {
78 names = isl_realloc_array(dim->ctx, dim->names,
79 struct isl_name *, isl_dim_total(dim));
80 if (!names)
81 goto error;
82 dim->names = names;
83 for (i = dim->n_name; i < isl_dim_total(dim); ++i)
84 dim->names[i] = NULL;
87 dim->n_name = isl_dim_total(dim);
89 return dim;
90 error:
91 isl_dim_free(dim);
92 return NULL;
95 static struct isl_dim *set_name(struct isl_dim *dim,
96 enum isl_dim_type type, unsigned pos,
97 struct isl_name *name)
99 struct isl_ctx *ctx = dim->ctx;
100 dim = isl_dim_cow(dim);
102 if (!dim)
103 goto error;
105 pos = global_pos(dim, type, pos);
106 isl_assert(ctx, pos != isl_dim_total(dim), goto error);
108 if (pos >= dim->n_name) {
109 if (!name)
110 return dim;
111 dim = extend_names(dim);
112 if (!dim)
113 goto error;
116 dim->names[pos] = name;
118 return dim;
119 error:
120 isl_name_free(ctx, name);
121 isl_dim_free(dim);
122 return NULL;
125 static struct isl_name *get_name(struct isl_dim *dim,
126 enum isl_dim_type type, unsigned pos)
128 if (!dim)
129 return NULL;
131 pos = global_pos(dim, type, pos);
132 if (pos == isl_dim_total(dim))
133 return NULL;
134 if (pos >= dim->n_name)
135 return NULL;
136 return dim->names[pos];
139 static unsigned offset(struct isl_dim *dim, enum isl_dim_type type)
141 switch (type) {
142 case isl_dim_param: return 0;
143 case isl_dim_in: return dim->nparam;
144 case isl_dim_out: return dim->nparam + dim->n_in;
148 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
150 switch (type) {
151 case isl_dim_param: return dim->nparam;
152 case isl_dim_in: return dim->n_in;
153 case isl_dim_out: return dim->n_out;
157 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
159 if (!dim)
160 return 0;
161 return n(dim, type);
164 unsigned isl_dim_offset(__isl_keep isl_dim *dim, enum isl_dim_type type)
166 if (!dim)
167 return 0;
168 return offset(dim, type);
171 static struct isl_dim *copy_names(struct isl_dim *dst,
172 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
173 enum isl_dim_type src_type)
175 int i;
176 struct isl_name *name;
178 for (i = 0; i < n(src, src_type); ++i) {
179 name = get_name(src, src_type, i);
180 if (!name)
181 continue;
182 dst = set_name(dst, dst_type, offset + i,
183 isl_name_copy(dst->ctx, name));
184 if (!dst)
185 return NULL;
187 return dst;
190 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
192 struct isl_dim *dup;
193 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
194 if (!dim->names)
195 return dup;
196 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
197 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
198 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
199 return dup;
202 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
204 if (!dim)
205 return NULL;
207 if (dim->ref == 1)
208 return dim;
209 dim->ref--;
210 return isl_dim_dup(dim);
213 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
215 if (!dim)
216 return NULL;
218 dim->ref++;
219 return dim;
222 void isl_dim_free(struct isl_dim *dim)
224 int i;
226 if (!dim)
227 return;
229 if (--dim->ref > 0)
230 return;
232 for (i = 0; i < dim->n_name; ++i)
233 isl_name_free(dim->ctx, dim->names[i]);
234 free(dim->names);
235 isl_ctx_deref(dim->ctx);
237 free(dim);
240 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
241 enum isl_dim_type type, unsigned pos,
242 const char *s)
244 struct isl_name *name;
245 if (!dim)
246 return NULL;
247 name = isl_name_get(dim->ctx, s);
248 if (!name)
249 goto error;
250 return set_name(dim, type, pos, name);
251 error:
252 isl_dim_free(dim);
253 return NULL;
256 const char *isl_dim_get_name(struct isl_dim *dim,
257 enum isl_dim_type type, unsigned pos)
259 struct isl_name *name = get_name(dim, type, pos);
260 return name ? name->name : NULL;
263 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
264 struct isl_dim *dim2, enum isl_dim_type dim2_type)
266 int i;
268 if (n(dim1, dim1_type) != n(dim2, dim2_type))
269 return 0;
271 if (!dim1->names && !dim2->names)
272 return 1;
274 for (i = 0; i < n(dim1, dim1_type); ++i) {
275 if (get_name(dim1, dim1_type, i) !=
276 get_name(dim2, dim2_type, i))
277 return 0;
279 return 1;
282 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
283 struct isl_dim *dim2, enum isl_dim_type dim2_type)
285 return match(dim1, dim1_type, dim2, dim2_type);
288 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
289 unsigned first, unsigned n, struct isl_name **names)
291 int i;
293 for (i = 0; i < n ; ++i)
294 names[i] = get_name(dim, type, first+i);
297 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
298 unsigned nparam, unsigned n_in, unsigned n_out)
300 struct isl_name **names = NULL;
302 if (!dim)
303 return NULL;
304 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
305 return dim;
307 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
308 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
309 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
311 dim = isl_dim_cow(dim);
313 if (dim->names) {
314 names = isl_calloc_array(dim->ctx, struct isl_name *,
315 nparam + n_in + n_out);
316 if (!names)
317 goto error;
318 get_names(dim, isl_dim_param, 0, dim->nparam, names);
319 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
320 get_names(dim, isl_dim_out, 0, dim->n_out,
321 names + nparam + n_in);
322 free(dim->names);
323 dim->names = names;
324 dim->n_name = nparam + n_in + n_out;
326 dim->nparam = nparam;
327 dim->n_in = n_in;
328 dim->n_out = n_out;
330 return dim;
331 error:
332 free(names);
333 isl_dim_free(dim);
334 return NULL;
337 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
338 unsigned n)
340 switch (type) {
341 case isl_dim_param:
342 return isl_dim_extend(dim,
343 dim->nparam + n, dim->n_in, dim->n_out);
344 case isl_dim_in:
345 return isl_dim_extend(dim,
346 dim->nparam, dim->n_in + n, dim->n_out);
347 case isl_dim_out:
348 return isl_dim_extend(dim,
349 dim->nparam, dim->n_in, dim->n_out + n);
351 return dim;
354 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
355 enum isl_dim_type type, unsigned pos, unsigned n)
357 struct isl_name **names = NULL;
359 if (!dim)
360 return NULL;
361 if (n == 0)
362 return dim;
364 isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
366 dim = isl_dim_cow(dim);
367 if (!dim)
368 return NULL;
370 if (dim->names) {
371 enum isl_dim_type t;
372 int off;
373 int size[3];
374 names = isl_calloc_array(dim->ctx, struct isl_name *,
375 dim->nparam + dim->n_in + dim->n_out + n);
376 if (!names)
377 goto error;
378 off = 0;
379 size[isl_dim_param] = dim->nparam;
380 size[isl_dim_in] = dim->n_in;
381 size[isl_dim_out] = dim->n_out;
382 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
383 if (t != type) {
384 get_names(dim, t, 0, size[t], names + off);
385 off += size[t];
386 } else {
387 get_names(dim, t, 0, pos, names + off);
388 off += pos + n;
389 get_names(dim, t, pos, size[t]-pos, names+off);
390 off += size[t] - pos;
393 free(dim->names);
394 dim->names = names;
395 dim->n_name = dim->nparam + dim->n_in + dim->n_out + n;
397 switch (type) {
398 case isl_dim_param: dim->nparam += n; break;
399 case isl_dim_in: dim->n_in += n; break;
400 case isl_dim_out: dim->n_out += n; break;
403 return dim;
404 error:
405 isl_dim_free(dim);
406 return NULL;
409 __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
410 enum isl_dim_type dst_type, unsigned dst_pos,
411 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
413 if (!dim)
414 return NULL;
415 if (n == 0)
416 return dim;
418 isl_assert(dim->ctx, src_pos + n <= isl_dim_size(dim, src_type),
419 goto error);
421 if (dst_type == src_type && dst_pos == src_pos)
422 return dim;
424 isl_assert(dim->ctx, dst_type != src_type, goto error);
426 dim = isl_dim_cow(dim);
427 if (!dim)
428 return NULL;
430 if (dim->names) {
431 struct isl_name **names;
432 enum isl_dim_type t;
433 int off;
434 int size[3];
435 names = isl_calloc_array(dim->ctx, struct isl_name *,
436 dim->nparam + dim->n_in + dim->n_out);
437 if (!names)
438 goto error;
439 off = 0;
440 size[isl_dim_param] = dim->nparam;
441 size[isl_dim_in] = dim->n_in;
442 size[isl_dim_out] = dim->n_out;
443 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
444 if (t == dst_type) {
445 get_names(dim, t, 0, dst_pos, names + off);
446 off += dst_pos;
447 get_names(dim, src_type, src_pos, n, names+off);
448 off += n;
449 get_names(dim, t, dst_pos, size[t] - dst_pos,
450 names + off);
451 off += size[t] - dst_pos;
452 } else if (t == src_type) {
453 get_names(dim, t, 0, src_pos, names + off);
454 off += src_pos;
455 get_names(dim, t, src_pos + n,
456 size[t] - src_pos - n, names + off);
457 off += size[t] - src_pos - n;
458 } else {
459 get_names(dim, t, 0, size[t], names + off);
460 off += size[t];
463 free(dim->names);
464 dim->names = names;
465 dim->n_name = dim->nparam + dim->n_in + dim->n_out;
468 switch (dst_type) {
469 case isl_dim_param: dim->nparam += n; break;
470 case isl_dim_in: dim->n_in += n; break;
471 case isl_dim_out: dim->n_out += n; break;
474 switch (src_type) {
475 case isl_dim_param: dim->nparam -= n; break;
476 case isl_dim_in: dim->n_in -= n; break;
477 case isl_dim_out: dim->n_out -= n; break;
480 return dim;
481 error:
482 isl_dim_free(dim);
483 return NULL;
486 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
488 struct isl_dim *dim;
490 if (!left || !right)
491 goto error;
493 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
494 goto error);
495 isl_assert(left->ctx, n(left, isl_dim_out) == n(right, isl_dim_in),
496 goto error);
498 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
499 if (!dim)
500 goto error;
502 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
503 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
504 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
506 isl_dim_free(left);
507 isl_dim_free(right);
509 return dim;
510 error:
511 isl_dim_free(left);
512 isl_dim_free(right);
513 return NULL;
516 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
518 struct isl_dim *dim;
520 if (!left || !right)
521 goto error;
523 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
524 goto error);
526 dim = isl_dim_alloc(left->ctx, left->nparam,
527 left->n_in + right->n_in, left->n_out + right->n_out);
528 if (!dim)
529 goto error;
531 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
532 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
533 dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
534 dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
535 dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
537 isl_dim_free(left);
538 isl_dim_free(right);
540 return dim;
541 error:
542 isl_dim_free(left);
543 isl_dim_free(right);
544 return NULL;
547 struct isl_dim *isl_dim_map(struct isl_dim *dim)
549 struct isl_name **names = NULL;
551 if (!dim)
552 return NULL;
553 isl_assert(dim->ctx, dim->n_in == 0, goto error);
554 if (dim->n_out == 0)
555 return dim;
556 dim = isl_dim_cow(dim);
557 if (!dim)
558 return NULL;
559 if (dim->names) {
560 names = isl_calloc_array(dim->ctx, struct isl_name *,
561 dim->nparam + dim->n_out + dim->n_out);
562 if (!names)
563 goto error;
564 get_names(dim, isl_dim_param, 0, dim->nparam, names);
565 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
567 dim->n_in = dim->n_out;
568 if (names) {
569 free(dim->names);
570 dim->names = names;
571 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
572 dim = copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
574 return dim;
575 error:
576 isl_dim_free(dim);
577 return NULL;
580 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
581 unsigned first, unsigned n, struct isl_name **names)
583 int i;
585 for (i = 0; i < n ; ++i)
586 dim = set_name(dim, type, first+i, names[i]);
588 return dim;
591 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
593 unsigned t;
594 struct isl_name **names = NULL;
596 if (!dim)
597 return NULL;
598 if (match(dim, isl_dim_in, dim, isl_dim_out))
599 return dim;
601 dim = isl_dim_cow(dim);
602 if (!dim)
603 return NULL;
605 if (dim->names) {
606 names = isl_alloc_array(dim->ctx, struct isl_name *,
607 dim->n_in + dim->n_out);
608 if (!names)
609 goto error;
610 get_names(dim, isl_dim_in, 0, dim->n_in, names);
611 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
614 t = dim->n_in;
615 dim->n_in = dim->n_out;
616 dim->n_out = t;
618 if (dim->names) {
619 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
620 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
621 free(names);
624 return dim;
625 error:
626 free(names);
627 isl_dim_free(dim);
628 return NULL;
631 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
632 unsigned first, unsigned num)
634 int i;
636 if (!dim)
637 return NULL;
639 if (n == 0)
640 return dim;
642 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
643 dim = isl_dim_cow(dim);
644 if (!dim)
645 goto error;
646 if (dim->names) {
647 dim = extend_names(dim);
648 if (!dim)
649 goto error;
650 for (i = 0; i < num; ++i)
651 isl_name_free(dim->ctx, get_name(dim, type, first+i));
652 for (i = first+num; i < n(dim, type); ++i)
653 set_name(dim, type, i - num, get_name(dim, type, i));
654 switch (type) {
655 case isl_dim_param:
656 get_names(dim, isl_dim_in, 0, dim->n_in,
657 dim->names + offset(dim, isl_dim_in) - num);
658 case isl_dim_in:
659 get_names(dim, isl_dim_out, 0, dim->n_out,
660 dim->names + offset(dim, isl_dim_out) - num);
661 case isl_dim_out:
664 dim->n_name -= num;
666 switch (type) {
667 case isl_dim_param: dim->nparam -= num; break;
668 case isl_dim_in: dim->n_in -= num; break;
669 case isl_dim_out: dim->n_out -= num; break;
671 return dim;
672 error:
673 isl_dim_free(dim);
674 return NULL;
677 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
678 unsigned first, unsigned n)
680 return isl_dim_drop(dim, isl_dim_in, first, n);
683 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
684 unsigned first, unsigned n)
686 return isl_dim_drop(dim, isl_dim_out, first, n);
689 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
691 if (!dim)
692 return NULL;
693 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
694 return isl_dim_reverse(dim);
697 struct isl_dim *isl_dim_range(struct isl_dim *dim)
699 if (!dim)
700 return NULL;
701 return isl_dim_drop_inputs(dim, 0, dim->n_in);
704 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
706 int i;
708 if (!dim)
709 return NULL;
710 if (n_div == 0 &&
711 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
712 return dim;
713 dim = isl_dim_cow(dim);
714 if (!dim)
715 return NULL;
716 dim->n_out += dim->nparam + dim->n_in + n_div;
717 dim->nparam = 0;
718 dim->n_in = 0;
720 for (i = 0; i < dim->n_name; ++i)
721 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
722 dim->n_name = 0;
724 return dim;
727 unsigned isl_dim_total(struct isl_dim *dim)
729 return dim->nparam + dim->n_in + dim->n_out;
732 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
734 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
735 n(dim1, isl_dim_in) == n(dim2, isl_dim_in) &&
736 n(dim1, isl_dim_out) == n(dim2, isl_dim_out);
739 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
741 return dim1->nparam == dim2->nparam &&
742 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;