isl_point.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * Copyright 2010 INRIA Saclay
  3. * Copyright 2013 Ecole Normale Superieure
  4. * Copyright 2015 Sven Verdoolaege
  5. * Copyright 2019 Cerebras Systems
  6. *
  7. * Use of this software is governed by the MIT license
  8. *
  9. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  10. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  11. * 91893 Orsay, France
  12. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  13. * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
  14. */
  15. #include <isl_map_private.h>
  16. #include <isl_point_private.h>
  17. #include <isl/set.h>
  18. #include <isl/union_set.h>
  19. #include <isl_sample.h>
  20. #include <isl_scan.h>
  21. #include <isl_seq.h>
  22. #include <isl_space_private.h>
  23. #include <isl_local_private.h>
  24. #include <isl_val_private.h>
  25. #include <isl_vec_private.h>
  26. #include <isl_output_private.h>
  27. #include <set_to_map.c>
  28. isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
  29. {
  30. return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
  31. }
  32. /* Return the space of "pnt".
  33. */
  34. __isl_keep isl_space *isl_point_peek_space(__isl_keep isl_point *pnt)
  35. {
  36. return pnt ? pnt->dim : NULL;
  37. }
  38. __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
  39. {
  40. return isl_space_copy(isl_point_peek_space(pnt));
  41. }
  42. #undef TYPE1
  43. #define TYPE1 isl_basic_map
  44. #undef TYPE2
  45. #define TYPE2 isl_point
  46. #undef TYPE_PAIR
  47. #define TYPE_PAIR isl_basic_map_point
  48. static
  49. #include "isl_type_has_equal_space_templ.c"
  50. static
  51. #include "isl_type_check_equal_space_templ.c"
  52. __isl_give isl_point *isl_point_alloc(__isl_take isl_space *space,
  53. __isl_take isl_vec *vec)
  54. {
  55. struct isl_point *pnt;
  56. isl_size dim;
  57. dim = isl_space_dim(space, isl_dim_all);
  58. if (dim < 0 || !vec)
  59. goto error;
  60. if (vec->size > 1 + dim) {
  61. vec = isl_vec_cow(vec);
  62. if (!vec)
  63. goto error;
  64. vec->size = 1 + dim;
  65. }
  66. pnt = isl_alloc_type(space->ctx, struct isl_point);
  67. if (!pnt)
  68. goto error;
  69. pnt->ref = 1;
  70. pnt->dim = space;
  71. pnt->vec = vec;
  72. return pnt;
  73. error:
  74. isl_space_free(space);
  75. isl_vec_free(vec);
  76. return NULL;
  77. }
  78. __isl_give isl_point *isl_point_zero(__isl_take isl_space *space)
  79. {
  80. isl_vec *vec;
  81. isl_size dim;
  82. dim = isl_space_dim(space, isl_dim_all);
  83. if (dim < 0)
  84. goto error;
  85. vec = isl_vec_alloc(space->ctx, 1 + dim);
  86. if (!vec)
  87. goto error;
  88. isl_int_set_si(vec->el[0], 1);
  89. isl_seq_clr(vec->el + 1, vec->size - 1);
  90. return isl_point_alloc(space, vec);
  91. error:
  92. isl_space_free(space);
  93. return NULL;
  94. }
  95. __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
  96. {
  97. struct isl_point *pnt2;
  98. if (!pnt)
  99. return NULL;
  100. pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
  101. return pnt2;
  102. }
  103. __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
  104. {
  105. struct isl_point *pnt2;
  106. if (!pnt)
  107. return NULL;
  108. if (pnt->ref == 1)
  109. return pnt;
  110. pnt2 = isl_point_dup(pnt);
  111. isl_point_free(pnt);
  112. return pnt2;
  113. }
  114. __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
  115. {
  116. if (!pnt)
  117. return NULL;
  118. pnt->ref++;
  119. return pnt;
  120. }
  121. __isl_null isl_point *isl_point_free(__isl_take isl_point *pnt)
  122. {
  123. if (!pnt)
  124. return NULL;
  125. if (--pnt->ref > 0)
  126. return NULL;
  127. isl_space_free(pnt->dim);
  128. isl_vec_free(pnt->vec);
  129. free(pnt);
  130. return NULL;
  131. }
  132. __isl_give isl_point *isl_point_void(__isl_take isl_space *space)
  133. {
  134. if (!space)
  135. return NULL;
  136. return isl_point_alloc(space, isl_vec_alloc(space->ctx, 0));
  137. }
  138. isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
  139. {
  140. if (!pnt)
  141. return isl_bool_error;
  142. return isl_bool_ok(pnt->vec->size == 0);
  143. }
  144. /* Return the space of "pnt".
  145. * This may be either a copy or the space itself
  146. * if there is only one reference to "pnt".
  147. * This allows the space to be modified inplace
  148. * if both the point and its space have only a single reference.
  149. * The caller is not allowed to modify "pnt" between this call and
  150. * a subsequent call to isl_point_restore_space.
  151. * The only exception is that isl_point_free can be called instead.
  152. */
  153. __isl_give isl_space *isl_point_take_space(__isl_keep isl_point *pnt)
  154. {
  155. isl_space *space;
  156. if (!pnt)
  157. return NULL;
  158. if (pnt->ref != 1)
  159. return isl_point_get_space(pnt);
  160. space = pnt->dim;
  161. pnt->dim = NULL;
  162. return space;
  163. }
  164. /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
  165. * due to a preceding call to isl_point_take_space.
  166. * However, in this case, "pnt" only has a single reference and
  167. * then the call to isl_point_cow has no effect.
  168. */
  169. __isl_give isl_point *isl_point_restore_space(__isl_take isl_point *pnt,
  170. __isl_take isl_space *space)
  171. {
  172. if (!pnt || !space)
  173. goto error;
  174. if (pnt->dim == space) {
  175. isl_space_free(space);
  176. return pnt;
  177. }
  178. pnt = isl_point_cow(pnt);
  179. if (!pnt)
  180. goto error;
  181. isl_space_free(pnt->dim);
  182. pnt->dim = space;
  183. return pnt;
  184. error:
  185. isl_point_free(pnt);
  186. isl_space_free(space);
  187. return NULL;
  188. }
  189. /* Return the coordinate vector of "pnt".
  190. */
  191. __isl_keep isl_vec *isl_point_peek_vec(__isl_keep isl_point *pnt)
  192. {
  193. return pnt ? pnt->vec : NULL;
  194. }
  195. /* Return a copy of the coordinate vector of "pnt".
  196. */
  197. __isl_give isl_vec *isl_point_get_vec(__isl_keep isl_point *pnt)
  198. {
  199. return isl_vec_copy(isl_point_peek_vec(pnt));
  200. }
  201. /* Return the coordinate vector of "pnt".
  202. * This may be either a copy or the coordinate vector itself
  203. * if there is only one reference to "pnt".
  204. * This allows the coordinate vector to be modified inplace
  205. * if both the point and its coordinate vector have only a single reference.
  206. * The caller is not allowed to modify "pnt" between this call and
  207. * a subsequent call to isl_point_restore_vec.
  208. * The only exception is that isl_point_free can be called instead.
  209. */
  210. __isl_give isl_vec *isl_point_take_vec(__isl_keep isl_point *pnt)
  211. {
  212. isl_vec *vec;
  213. if (!pnt)
  214. return NULL;
  215. if (pnt->ref != 1)
  216. return isl_point_get_vec(pnt);
  217. vec = pnt->vec;
  218. pnt->vec = NULL;
  219. return vec;
  220. }
  221. /* Set the coordinate vector of "pnt" to "vec",
  222. * where the coordinate vector of "pnt" may be missing
  223. * due to a preceding call to isl_point_take_vec.
  224. * However, in this case, "pnt" only has a single reference and
  225. * then the call to isl_point_cow has no effect.
  226. */
  227. __isl_give isl_point *isl_point_restore_vec(__isl_take isl_point *pnt,
  228. __isl_take isl_vec *vec)
  229. {
  230. if (!pnt || !vec)
  231. goto error;
  232. if (pnt->vec == vec) {
  233. isl_vec_free(vec);
  234. return pnt;
  235. }
  236. pnt = isl_point_cow(pnt);
  237. if (!pnt)
  238. goto error;
  239. isl_vec_free(pnt->vec);
  240. pnt->vec = vec;
  241. return pnt;
  242. error:
  243. isl_point_free(pnt);
  244. isl_vec_free(vec);
  245. return NULL;
  246. }
  247. /* Return the number of variables of the given type.
  248. */
  249. static isl_size isl_point_dim(__isl_keep isl_point *pnt, enum isl_dim_type type)
  250. {
  251. return isl_space_dim(isl_point_peek_space(pnt), type);
  252. }
  253. /* Return the position of the coordinates of the given type
  254. * within the sequence of coordinates of "pnt".
  255. */
  256. static isl_size isl_point_var_offset(__isl_keep isl_point *pnt,
  257. enum isl_dim_type type)
  258. {
  259. return pnt ? isl_space_offset(pnt->dim, type) : isl_size_error;
  260. }
  261. #undef TYPE
  262. #define TYPE isl_point
  263. static
  264. #include "check_type_range_templ.c"
  265. /* Return the value of coordinate "pos" of type "type" of "pnt".
  266. */
  267. __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
  268. enum isl_dim_type type, int pos)
  269. {
  270. isl_ctx *ctx;
  271. isl_val *v;
  272. isl_size off;
  273. if (!pnt)
  274. return NULL;
  275. ctx = isl_point_get_ctx(pnt);
  276. if (isl_point_is_void(pnt))
  277. isl_die(ctx, isl_error_invalid,
  278. "void point does not have coordinates", return NULL);
  279. if (isl_point_check_range(pnt, type, pos, 1) < 0)
  280. return NULL;
  281. off = isl_point_var_offset(pnt, type);
  282. if (off < 0)
  283. return NULL;
  284. pos += off;
  285. v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
  286. pnt->vec->el[0]);
  287. return isl_val_normalize(v);
  288. }
  289. /* Set all entries of "mv" to NaN.
  290. */
  291. static __isl_give isl_multi_val *set_nan(__isl_take isl_multi_val *mv)
  292. {
  293. int i;
  294. isl_size n;
  295. isl_val *v;
  296. n = isl_multi_val_size(mv);
  297. if (n < 0)
  298. return isl_multi_val_free(mv);
  299. v = isl_val_nan(isl_multi_val_get_ctx(mv));
  300. for (i = 0; i < n; ++i)
  301. mv = isl_multi_val_set_at(mv, i, isl_val_copy(v));
  302. isl_val_free(v);
  303. return mv;
  304. }
  305. /* Return the values of the set dimensions of "pnt".
  306. * Return a sequence of NaNs in case of a void point.
  307. */
  308. __isl_give isl_multi_val *isl_point_get_multi_val(__isl_keep isl_point *pnt)
  309. {
  310. int i;
  311. isl_bool is_void;
  312. isl_size n;
  313. isl_multi_val *mv;
  314. is_void = isl_point_is_void(pnt);
  315. if (is_void < 0)
  316. return NULL;
  317. mv = isl_multi_val_alloc(isl_point_get_space(pnt));
  318. if (is_void)
  319. return set_nan(mv);
  320. n = isl_multi_val_size(mv);
  321. if (n < 0)
  322. return isl_multi_val_free(mv);
  323. for (i = 0; i < n; ++i) {
  324. isl_val *v;
  325. v = isl_point_get_coordinate_val(pnt, isl_dim_set, i);
  326. mv = isl_multi_val_set_at(mv, i, v);
  327. }
  328. return mv;
  329. }
  330. /* Replace coordinate "pos" of type "type" of "pnt" by "v".
  331. */
  332. __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
  333. enum isl_dim_type type, int pos, __isl_take isl_val *v)
  334. {
  335. if (!pnt || !v)
  336. goto error;
  337. if (isl_point_is_void(pnt))
  338. isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
  339. "void point does not have coordinates", goto error);
  340. if (isl_point_check_range(pnt, type, pos, 1) < 0)
  341. goto error;
  342. if (!isl_val_is_rat(v))
  343. isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
  344. "expecting rational value", goto error);
  345. pos += isl_space_offset(isl_point_peek_space(pnt), type);
  346. if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
  347. isl_int_eq(pnt->vec->el[0], v->d)) {
  348. isl_val_free(v);
  349. return pnt;
  350. }
  351. pnt = isl_point_cow(pnt);
  352. if (!pnt)
  353. goto error;
  354. pnt->vec = isl_vec_cow(pnt->vec);
  355. if (!pnt->vec)
  356. goto error;
  357. if (isl_int_eq(pnt->vec->el[0], v->d)) {
  358. isl_int_set(pnt->vec->el[1 + pos], v->n);
  359. } else if (isl_int_is_one(v->d)) {
  360. isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
  361. } else {
  362. isl_seq_scale(pnt->vec->el + 1,
  363. pnt->vec->el + 1, v->d, pnt->vec->size - 1);
  364. isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
  365. isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
  366. pnt->vec = isl_vec_normalize(pnt->vec);
  367. if (!pnt->vec)
  368. goto error;
  369. }
  370. isl_val_free(v);
  371. return pnt;
  372. error:
  373. isl_val_free(v);
  374. isl_point_free(pnt);
  375. return NULL;
  376. }
  377. __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
  378. enum isl_dim_type type, int pos, unsigned val)
  379. {
  380. isl_size off;
  381. if (!pnt || isl_point_is_void(pnt))
  382. return pnt;
  383. pnt = isl_point_cow(pnt);
  384. if (!pnt)
  385. return NULL;
  386. pnt->vec = isl_vec_cow(pnt->vec);
  387. if (!pnt->vec)
  388. goto error;
  389. off = isl_point_var_offset(pnt, type);
  390. if (off < 0)
  391. goto error;
  392. pos += off;
  393. isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
  394. return pnt;
  395. error:
  396. isl_point_free(pnt);
  397. return NULL;
  398. }
  399. __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
  400. enum isl_dim_type type, int pos, unsigned val)
  401. {
  402. isl_size off;
  403. if (!pnt || isl_point_is_void(pnt))
  404. return pnt;
  405. pnt = isl_point_cow(pnt);
  406. if (!pnt)
  407. return NULL;
  408. pnt->vec = isl_vec_cow(pnt->vec);
  409. if (!pnt->vec)
  410. goto error;
  411. off = isl_point_var_offset(pnt, type);
  412. if (off < 0)
  413. goto error;
  414. pos += off;
  415. isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
  416. return pnt;
  417. error:
  418. isl_point_free(pnt);
  419. return NULL;
  420. }
  421. struct isl_foreach_point {
  422. struct isl_scan_callback callback;
  423. isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
  424. void *user;
  425. isl_space *dim;
  426. };
  427. static isl_stat foreach_point(struct isl_scan_callback *cb,
  428. __isl_take isl_vec *sample)
  429. {
  430. struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
  431. isl_point *pnt;
  432. pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
  433. return fp->fn(pnt, fp->user);
  434. }
  435. isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
  436. isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
  437. {
  438. struct isl_foreach_point fp = { { &foreach_point }, fn, user };
  439. int i;
  440. if (!set)
  441. return isl_stat_error;
  442. fp.dim = isl_set_get_space(set);
  443. if (!fp.dim)
  444. return isl_stat_error;
  445. set = isl_set_copy(set);
  446. set = isl_set_cow(set);
  447. set = isl_set_make_disjoint(set);
  448. set = isl_set_compute_divs(set);
  449. if (!set)
  450. goto error;
  451. for (i = 0; i < set->n; ++i)
  452. if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
  453. &fp.callback) < 0)
  454. goto error;
  455. isl_set_free(set);
  456. isl_space_free(fp.dim);
  457. return isl_stat_ok;
  458. error:
  459. isl_set_free(set);
  460. isl_space_free(fp.dim);
  461. return isl_stat_error;
  462. }
  463. /* Return 1 if "bmap" contains the point "point".
  464. * "bmap" is assumed to have known divs.
  465. * The point is first extended with the divs and then passed
  466. * to basic_map_contains.
  467. */
  468. isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
  469. __isl_keep isl_point *point)
  470. {
  471. isl_local *local;
  472. isl_vec *vec;
  473. isl_bool contains;
  474. if (isl_basic_map_point_check_equal_space(bmap, point) < 0)
  475. return isl_bool_error;
  476. if (bmap->n_div == 0)
  477. return isl_basic_map_contains(bmap, point->vec);
  478. local = isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap));
  479. vec = isl_point_get_vec(point);
  480. vec = isl_local_extend_point_vec(local, vec);
  481. isl_local_free(local);
  482. contains = isl_basic_map_contains(bmap, vec);
  483. isl_vec_free(vec);
  484. return contains;
  485. }
  486. isl_bool isl_map_contains_point(__isl_keep isl_map *map,
  487. __isl_keep isl_point *point)
  488. {
  489. int i;
  490. isl_bool found = isl_bool_false;
  491. if (!map || !point)
  492. return isl_bool_error;
  493. map = isl_map_copy(map);
  494. map = isl_map_compute_divs(map);
  495. if (!map)
  496. return isl_bool_error;
  497. for (i = 0; i < map->n; ++i) {
  498. found = isl_basic_map_contains_point(map->p[i], point);
  499. if (found < 0)
  500. goto error;
  501. if (found)
  502. break;
  503. }
  504. isl_map_free(map);
  505. return found;
  506. error:
  507. isl_map_free(map);
  508. return isl_bool_error;
  509. }
  510. isl_bool isl_set_contains_point(__isl_keep isl_set *set,
  511. __isl_keep isl_point *point)
  512. {
  513. return isl_map_contains_point(set_to_map(set), point);
  514. }
  515. __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
  516. {
  517. isl_basic_set *bset;
  518. isl_basic_set *model;
  519. if (!pnt)
  520. return NULL;
  521. model = isl_basic_set_empty(isl_space_copy(pnt->dim));
  522. bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
  523. bset = isl_basic_set_from_underlying_set(bset, model);
  524. isl_point_free(pnt);
  525. return bset;
  526. }
  527. __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
  528. {
  529. isl_basic_set *bset;
  530. bset = isl_basic_set_from_point(pnt);
  531. return isl_set_from_basic_set(bset);
  532. }
  533. /* This function performs the same operation as isl_set_from_point,
  534. * but is considered as a function on an isl_point when exported.
  535. */
  536. __isl_give isl_set *isl_point_to_set(__isl_take isl_point *pnt)
  537. {
  538. return isl_set_from_point(pnt);
  539. }
  540. /* Construct a union set, containing the single element "pnt".
  541. * If "pnt" is void, then return an empty union set.
  542. */
  543. __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
  544. {
  545. if (!pnt)
  546. return NULL;
  547. if (isl_point_is_void(pnt)) {
  548. isl_space *space;
  549. space = isl_point_get_space(pnt);
  550. isl_point_free(pnt);
  551. return isl_union_set_empty(space);
  552. }
  553. return isl_union_set_from_set(isl_set_from_point(pnt));
  554. }
  555. __isl_give isl_basic_set *isl_basic_set_box_from_points(
  556. __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
  557. {
  558. isl_basic_set *bset = NULL;
  559. isl_size total;
  560. int i;
  561. int k;
  562. isl_int t;
  563. isl_int_init(t);
  564. if (!pnt1 || !pnt2)
  565. goto error;
  566. isl_assert(pnt1->dim->ctx,
  567. isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
  568. if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
  569. isl_space *space = isl_space_copy(pnt1->dim);
  570. isl_point_free(pnt1);
  571. isl_point_free(pnt2);
  572. isl_int_clear(t);
  573. return isl_basic_set_empty(space);
  574. }
  575. if (isl_point_is_void(pnt1)) {
  576. isl_point_free(pnt1);
  577. isl_int_clear(t);
  578. return isl_basic_set_from_point(pnt2);
  579. }
  580. if (isl_point_is_void(pnt2)) {
  581. isl_point_free(pnt2);
  582. isl_int_clear(t);
  583. return isl_basic_set_from_point(pnt1);
  584. }
  585. total = isl_point_dim(pnt1, isl_dim_all);
  586. if (total < 0)
  587. goto error;
  588. bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
  589. for (i = 0; i < total; ++i) {
  590. isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
  591. isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
  592. k = isl_basic_set_alloc_inequality(bset);
  593. if (k < 0)
  594. goto error;
  595. isl_seq_clr(bset->ineq[k] + 1, total);
  596. if (isl_int_is_pos(t)) {
  597. isl_int_set_si(bset->ineq[k][1 + i], -1);
  598. isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
  599. } else {
  600. isl_int_set_si(bset->ineq[k][1 + i], 1);
  601. isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
  602. }
  603. isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
  604. k = isl_basic_set_alloc_inequality(bset);
  605. if (k < 0)
  606. goto error;
  607. isl_seq_clr(bset->ineq[k] + 1, total);
  608. if (isl_int_is_pos(t)) {
  609. isl_int_set_si(bset->ineq[k][1 + i], 1);
  610. isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
  611. } else {
  612. isl_int_set_si(bset->ineq[k][1 + i], -1);
  613. isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
  614. }
  615. isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
  616. }
  617. bset = isl_basic_set_finalize(bset);
  618. isl_point_free(pnt1);
  619. isl_point_free(pnt2);
  620. isl_int_clear(t);
  621. return bset;
  622. error:
  623. isl_point_free(pnt1);
  624. isl_point_free(pnt2);
  625. isl_int_clear(t);
  626. isl_basic_set_free(bset);
  627. return NULL;
  628. }
  629. __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
  630. __isl_take isl_point *pnt2)
  631. {
  632. isl_basic_set *bset;
  633. bset = isl_basic_set_box_from_points(pnt1, pnt2);
  634. return isl_set_from_basic_set(bset);
  635. }
  636. /* Print the coordinate at position "pos" of the point "pnt".
  637. */
  638. static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
  639. struct isl_print_space_data *data, unsigned pos)
  640. {
  641. isl_point *pnt = data->user;
  642. pos += isl_space_offset(data->space, data->type);
  643. p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
  644. if (!isl_int_is_one(pnt->vec->el[0])) {
  645. p = isl_printer_print_str(p, "/");
  646. p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
  647. }
  648. return p;
  649. }
  650. __isl_give isl_printer *isl_printer_print_point(
  651. __isl_take isl_printer *p, __isl_keep isl_point *pnt)
  652. {
  653. struct isl_print_space_data data = { 0 };
  654. int i;
  655. isl_size nparam;
  656. if (!pnt)
  657. return p;
  658. if (isl_point_is_void(pnt)) {
  659. p = isl_printer_print_str(p, "void");
  660. return p;
  661. }
  662. nparam = isl_point_dim(pnt, isl_dim_param);
  663. if (nparam < 0)
  664. return isl_printer_free(p);
  665. if (nparam > 0) {
  666. p = isl_printer_print_str(p, "[");
  667. for (i = 0; i < nparam; ++i) {
  668. const char *name;
  669. if (i)
  670. p = isl_printer_print_str(p, ", ");
  671. name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
  672. if (name) {
  673. p = isl_printer_print_str(p, name);
  674. p = isl_printer_print_str(p, " = ");
  675. }
  676. p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
  677. if (!isl_int_is_one(pnt->vec->el[0])) {
  678. p = isl_printer_print_str(p, "/");
  679. p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
  680. }
  681. }
  682. p = isl_printer_print_str(p, "]");
  683. p = isl_printer_print_str(p, " -> ");
  684. }
  685. data.print_dim = &print_coordinate;
  686. data.user = pnt;
  687. p = isl_printer_print_str(p, "{ ");
  688. p = isl_print_space(pnt->dim, p, 0, &data);
  689. p = isl_printer_print_str(p, " }");
  690. return p;
  691. }