isl_val.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * Copyright 2013 Ecole Normale Superieure
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege,
  7. * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  8. */
  9. #include <isl_int.h>
  10. #include <isl_ctx_private.h>
  11. #include <isl_val_private.h>
  12. #undef EL_BASE
  13. #define EL_BASE val
  14. #include <isl_list_templ.c>
  15. #include <isl_list_read_templ.c>
  16. /* Allocate an isl_val object with indeterminate value.
  17. */
  18. __isl_give isl_val *isl_val_alloc(isl_ctx *ctx)
  19. {
  20. isl_val *v;
  21. v = isl_alloc_type(ctx, struct isl_val);
  22. if (!v)
  23. return NULL;
  24. v->ctx = ctx;
  25. isl_ctx_ref(ctx);
  26. v->ref = 1;
  27. isl_int_init(v->n);
  28. isl_int_init(v->d);
  29. return v;
  30. }
  31. /* Return a reference to an isl_val representing zero.
  32. */
  33. __isl_give isl_val *isl_val_zero(isl_ctx *ctx)
  34. {
  35. return isl_val_int_from_si(ctx, 0);
  36. }
  37. /* Return a reference to an isl_val representing one.
  38. */
  39. __isl_give isl_val *isl_val_one(isl_ctx *ctx)
  40. {
  41. return isl_val_int_from_si(ctx, 1);
  42. }
  43. /* Return a reference to an isl_val representing negative one.
  44. */
  45. __isl_give isl_val *isl_val_negone(isl_ctx *ctx)
  46. {
  47. return isl_val_int_from_si(ctx, -1);
  48. }
  49. /* Return a reference to an isl_val representing NaN.
  50. */
  51. __isl_give isl_val *isl_val_nan(isl_ctx *ctx)
  52. {
  53. isl_val *v;
  54. v = isl_val_alloc(ctx);
  55. if (!v)
  56. return NULL;
  57. isl_int_set_si(v->n, 0);
  58. isl_int_set_si(v->d, 0);
  59. return v;
  60. }
  61. /* Change "v" into a NaN.
  62. */
  63. __isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
  64. {
  65. if (!v)
  66. return NULL;
  67. if (isl_val_is_nan(v))
  68. return v;
  69. v = isl_val_cow(v);
  70. if (!v)
  71. return NULL;
  72. isl_int_set_si(v->n, 0);
  73. isl_int_set_si(v->d, 0);
  74. return v;
  75. }
  76. /* Return a reference to an isl_val representing +infinity.
  77. */
  78. __isl_give isl_val *isl_val_infty(isl_ctx *ctx)
  79. {
  80. isl_val *v;
  81. v = isl_val_alloc(ctx);
  82. if (!v)
  83. return NULL;
  84. isl_int_set_si(v->n, 1);
  85. isl_int_set_si(v->d, 0);
  86. return v;
  87. }
  88. /* Return a reference to an isl_val representing -infinity.
  89. */
  90. __isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
  91. {
  92. isl_val *v;
  93. v = isl_val_alloc(ctx);
  94. if (!v)
  95. return NULL;
  96. isl_int_set_si(v->n, -1);
  97. isl_int_set_si(v->d, 0);
  98. return v;
  99. }
  100. /* Return a reference to an isl_val representing the integer "i".
  101. */
  102. __isl_give isl_val *isl_val_int_from_si(isl_ctx *ctx, long i)
  103. {
  104. isl_val *v;
  105. v = isl_val_alloc(ctx);
  106. if (!v)
  107. return NULL;
  108. isl_int_set_si(v->n, i);
  109. isl_int_set_si(v->d, 1);
  110. return v;
  111. }
  112. /* Change the value of "v" to be equal to the integer "i".
  113. */
  114. __isl_give isl_val *isl_val_set_si(__isl_take isl_val *v, long i)
  115. {
  116. if (!v)
  117. return NULL;
  118. if (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
  119. return v;
  120. v = isl_val_cow(v);
  121. if (!v)
  122. return NULL;
  123. isl_int_set_si(v->n, i);
  124. isl_int_set_si(v->d, 1);
  125. return v;
  126. }
  127. /* Change the value of "v" to be equal to zero.
  128. */
  129. __isl_give isl_val *isl_val_set_zero(__isl_take isl_val *v)
  130. {
  131. return isl_val_set_si(v, 0);
  132. }
  133. /* Return a reference to an isl_val representing the unsigned integer "u".
  134. */
  135. __isl_give isl_val *isl_val_int_from_ui(isl_ctx *ctx, unsigned long u)
  136. {
  137. isl_val *v;
  138. v = isl_val_alloc(ctx);
  139. if (!v)
  140. return NULL;
  141. isl_int_set_ui(v->n, u);
  142. isl_int_set_si(v->d, 1);
  143. return v;
  144. }
  145. /* Return a reference to an isl_val representing the integer "n".
  146. */
  147. __isl_give isl_val *isl_val_int_from_isl_int(isl_ctx *ctx, isl_int n)
  148. {
  149. isl_val *v;
  150. v = isl_val_alloc(ctx);
  151. if (!v)
  152. return NULL;
  153. isl_int_set(v->n, n);
  154. isl_int_set_si(v->d, 1);
  155. return v;
  156. }
  157. /* Return a reference to an isl_val representing the rational value "n"/"d".
  158. * Normalizing the isl_val (if needed) is left to the caller.
  159. */
  160. __isl_give isl_val *isl_val_rat_from_isl_int(isl_ctx *ctx,
  161. isl_int n, isl_int d)
  162. {
  163. isl_val *v;
  164. v = isl_val_alloc(ctx);
  165. if (!v)
  166. return NULL;
  167. isl_int_set(v->n, n);
  168. isl_int_set(v->d, d);
  169. return v;
  170. }
  171. /* Return a new reference to "v".
  172. */
  173. __isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
  174. {
  175. if (!v)
  176. return NULL;
  177. v->ref++;
  178. return v;
  179. }
  180. /* Return a fresh copy of "val".
  181. */
  182. __isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
  183. {
  184. isl_val *dup;
  185. if (!val)
  186. return NULL;
  187. dup = isl_val_alloc(isl_val_get_ctx(val));
  188. if (!dup)
  189. return NULL;
  190. isl_int_set(dup->n, val->n);
  191. isl_int_set(dup->d, val->d);
  192. return dup;
  193. }
  194. /* Return an isl_val that is equal to "val" and that has only
  195. * a single reference.
  196. */
  197. __isl_give isl_val *isl_val_cow(__isl_take isl_val *val)
  198. {
  199. if (!val)
  200. return NULL;
  201. if (val->ref == 1)
  202. return val;
  203. val->ref--;
  204. return isl_val_dup(val);
  205. }
  206. /* Free "v" and return NULL.
  207. */
  208. __isl_null isl_val *isl_val_free(__isl_take isl_val *v)
  209. {
  210. if (!v)
  211. return NULL;
  212. if (--v->ref > 0)
  213. return NULL;
  214. isl_ctx_deref(v->ctx);
  215. isl_int_clear(v->n);
  216. isl_int_clear(v->d);
  217. free(v);
  218. return NULL;
  219. }
  220. /* Extract the numerator of a rational value "v" as an integer.
  221. *
  222. * If "v" is not a rational value, then the result is undefined.
  223. */
  224. long isl_val_get_num_si(__isl_keep isl_val *v)
  225. {
  226. if (!v)
  227. return 0;
  228. if (!isl_val_is_rat(v))
  229. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  230. "expecting rational value", return 0);
  231. if (!isl_int_fits_slong(v->n))
  232. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  233. "numerator too large", return 0);
  234. return isl_int_get_si(v->n);
  235. }
  236. /* Extract the numerator of a rational value "v" as an isl_int.
  237. *
  238. * If "v" is not a rational value, then the result is undefined.
  239. */
  240. isl_stat isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
  241. {
  242. if (!v)
  243. return isl_stat_error;
  244. if (!isl_val_is_rat(v))
  245. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  246. "expecting rational value", return isl_stat_error);
  247. isl_int_set(*n, v->n);
  248. return isl_stat_ok;
  249. }
  250. /* Extract the denominator of a rational value "v" as an integer.
  251. *
  252. * If "v" is not a rational value, then the result is undefined.
  253. */
  254. long isl_val_get_den_si(__isl_keep isl_val *v)
  255. {
  256. if (!v)
  257. return 0;
  258. if (!isl_val_is_rat(v))
  259. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  260. "expecting rational value", return 0);
  261. if (!isl_int_fits_slong(v->d))
  262. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  263. "denominator too large", return 0);
  264. return isl_int_get_si(v->d);
  265. }
  266. /* Extract the denominator of a rational value "v" as an isl_val.
  267. *
  268. * If "v" is not a rational value, then the result is undefined.
  269. */
  270. __isl_give isl_val *isl_val_get_den_val(__isl_keep isl_val *v)
  271. {
  272. if (!v)
  273. return NULL;
  274. if (!isl_val_is_rat(v))
  275. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  276. "expecting rational value", return NULL);
  277. return isl_val_int_from_isl_int(isl_val_get_ctx(v), v->d);
  278. }
  279. /* Return an approximation of "v" as a double.
  280. */
  281. double isl_val_get_d(__isl_keep isl_val *v)
  282. {
  283. if (!v)
  284. return 0;
  285. if (!isl_val_is_rat(v))
  286. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  287. "expecting rational value", return 0);
  288. return isl_int_get_d(v->n) / isl_int_get_d(v->d);
  289. }
  290. /* Return the isl_ctx to which "val" belongs.
  291. */
  292. isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
  293. {
  294. return val ? val->ctx : NULL;
  295. }
  296. /* Return a hash value that digests "val".
  297. */
  298. uint32_t isl_val_get_hash(__isl_keep isl_val *val)
  299. {
  300. uint32_t hash;
  301. if (!val)
  302. return 0;
  303. hash = isl_hash_init();
  304. hash = isl_int_hash(val->n, hash);
  305. hash = isl_int_hash(val->d, hash);
  306. return hash;
  307. }
  308. /* Normalize "v".
  309. *
  310. * In particular, make sure that the denominator of a rational value
  311. * is positive and the numerator and denominator do not have any
  312. * common divisors.
  313. *
  314. * This function should not be called by an external user
  315. * since it will only be given normalized values.
  316. */
  317. __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
  318. {
  319. isl_ctx *ctx;
  320. if (!v)
  321. return NULL;
  322. if (isl_val_is_int(v))
  323. return v;
  324. if (!isl_val_is_rat(v))
  325. return v;
  326. if (isl_int_is_neg(v->d)) {
  327. isl_int_neg(v->d, v->d);
  328. isl_int_neg(v->n, v->n);
  329. }
  330. ctx = isl_val_get_ctx(v);
  331. isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
  332. if (isl_int_is_one(ctx->normalize_gcd))
  333. return v;
  334. isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
  335. isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
  336. return v;
  337. }
  338. /* Return the opposite of "v".
  339. */
  340. __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
  341. {
  342. if (!v)
  343. return NULL;
  344. if (isl_val_is_nan(v))
  345. return v;
  346. if (isl_val_is_zero(v))
  347. return v;
  348. v = isl_val_cow(v);
  349. if (!v)
  350. return NULL;
  351. isl_int_neg(v->n, v->n);
  352. return v;
  353. }
  354. /* Return the inverse of "v".
  355. */
  356. __isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
  357. {
  358. if (!v)
  359. return NULL;
  360. if (isl_val_is_nan(v))
  361. return v;
  362. if (isl_val_is_zero(v)) {
  363. isl_ctx *ctx = isl_val_get_ctx(v);
  364. isl_val_free(v);
  365. return isl_val_nan(ctx);
  366. }
  367. if (isl_val_is_infty(v) || isl_val_is_neginfty(v)) {
  368. isl_ctx *ctx = isl_val_get_ctx(v);
  369. isl_val_free(v);
  370. return isl_val_zero(ctx);
  371. }
  372. v = isl_val_cow(v);
  373. if (!v)
  374. return NULL;
  375. isl_int_swap(v->n, v->d);
  376. return isl_val_normalize(v);
  377. }
  378. /* Return the absolute value of "v".
  379. */
  380. __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
  381. {
  382. if (!v)
  383. return NULL;
  384. if (isl_val_is_nan(v))
  385. return v;
  386. if (isl_val_is_nonneg(v))
  387. return v;
  388. return isl_val_neg(v);
  389. }
  390. /* Return the "floor" (greatest integer part) of "v".
  391. * That is, return the result of rounding towards -infinity.
  392. */
  393. __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
  394. {
  395. if (!v)
  396. return NULL;
  397. if (isl_val_is_int(v))
  398. return v;
  399. if (!isl_val_is_rat(v))
  400. return v;
  401. v = isl_val_cow(v);
  402. if (!v)
  403. return NULL;
  404. isl_int_fdiv_q(v->n, v->n, v->d);
  405. isl_int_set_si(v->d, 1);
  406. return v;
  407. }
  408. /* Return the "ceiling" of "v".
  409. * That is, return the result of rounding towards +infinity.
  410. */
  411. __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
  412. {
  413. if (!v)
  414. return NULL;
  415. if (isl_val_is_int(v))
  416. return v;
  417. if (!isl_val_is_rat(v))
  418. return v;
  419. v = isl_val_cow(v);
  420. if (!v)
  421. return NULL;
  422. isl_int_cdiv_q(v->n, v->n, v->d);
  423. isl_int_set_si(v->d, 1);
  424. return v;
  425. }
  426. /* Truncate "v".
  427. * That is, return the result of rounding towards zero.
  428. */
  429. __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
  430. {
  431. if (!v)
  432. return NULL;
  433. if (isl_val_is_int(v))
  434. return v;
  435. if (!isl_val_is_rat(v))
  436. return v;
  437. v = isl_val_cow(v);
  438. if (!v)
  439. return NULL;
  440. isl_int_tdiv_q(v->n, v->n, v->d);
  441. isl_int_set_si(v->d, 1);
  442. return v;
  443. }
  444. /* Return 2^v, where v is an integer (that is not too large).
  445. */
  446. __isl_give isl_val *isl_val_pow2(__isl_take isl_val *v)
  447. {
  448. unsigned long exp;
  449. int neg;
  450. v = isl_val_cow(v);
  451. if (!v)
  452. return NULL;
  453. if (!isl_val_is_int(v))
  454. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  455. "can only compute integer powers",
  456. return isl_val_free(v));
  457. neg = isl_val_is_neg(v);
  458. if (neg)
  459. isl_int_neg(v->n, v->n);
  460. if (!isl_int_fits_ulong(v->n))
  461. isl_die(isl_val_get_ctx(v), isl_error_invalid,
  462. "exponent too large", return isl_val_free(v));
  463. exp = isl_int_get_ui(v->n);
  464. if (neg) {
  465. isl_int_mul_2exp(v->d, v->d, exp);
  466. isl_int_set_si(v->n, 1);
  467. } else {
  468. isl_int_mul_2exp(v->n, v->d, exp);
  469. }
  470. return v;
  471. }
  472. /* This is an alternative name for the function above.
  473. */
  474. __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
  475. {
  476. return isl_val_pow2(v);
  477. }
  478. /* Return the minimum of "v1" and "v2".
  479. */
  480. __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
  481. {
  482. if (!v1 || !v2)
  483. goto error;
  484. if (isl_val_is_nan(v1)) {
  485. isl_val_free(v2);
  486. return v1;
  487. }
  488. if (isl_val_is_nan(v2)) {
  489. isl_val_free(v1);
  490. return v2;
  491. }
  492. if (isl_val_le(v1, v2)) {
  493. isl_val_free(v2);
  494. return v1;
  495. } else {
  496. isl_val_free(v1);
  497. return v2;
  498. }
  499. error:
  500. isl_val_free(v1);
  501. isl_val_free(v2);
  502. return NULL;
  503. }
  504. /* Return the maximum of "v1" and "v2".
  505. */
  506. __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
  507. {
  508. if (!v1 || !v2)
  509. goto error;
  510. if (isl_val_is_nan(v1)) {
  511. isl_val_free(v2);
  512. return v1;
  513. }
  514. if (isl_val_is_nan(v2)) {
  515. isl_val_free(v1);
  516. return v2;
  517. }
  518. if (isl_val_ge(v1, v2)) {
  519. isl_val_free(v2);
  520. return v1;
  521. } else {
  522. isl_val_free(v1);
  523. return v2;
  524. }
  525. error:
  526. isl_val_free(v1);
  527. isl_val_free(v2);
  528. return NULL;
  529. }
  530. /* Return the sum of "v1" and "v2".
  531. */
  532. __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
  533. {
  534. if (!v1 || !v2)
  535. goto error;
  536. if (isl_val_is_nan(v1)) {
  537. isl_val_free(v2);
  538. return v1;
  539. }
  540. if (isl_val_is_nan(v2)) {
  541. isl_val_free(v1);
  542. return v2;
  543. }
  544. if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
  545. (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
  546. isl_val_free(v2);
  547. return isl_val_set_nan(v1);
  548. }
  549. if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
  550. isl_val_free(v2);
  551. return v1;
  552. }
  553. if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
  554. isl_val_free(v1);
  555. return v2;
  556. }
  557. if (isl_val_is_zero(v1)) {
  558. isl_val_free(v1);
  559. return v2;
  560. }
  561. if (isl_val_is_zero(v2)) {
  562. isl_val_free(v2);
  563. return v1;
  564. }
  565. v1 = isl_val_cow(v1);
  566. if (!v1)
  567. goto error;
  568. if (isl_val_is_int(v1) && isl_val_is_int(v2))
  569. isl_int_add(v1->n, v1->n, v2->n);
  570. else {
  571. if (isl_int_eq(v1->d, v2->d))
  572. isl_int_add(v1->n, v1->n, v2->n);
  573. else {
  574. isl_int_mul(v1->n, v1->n, v2->d);
  575. isl_int_addmul(v1->n, v2->n, v1->d);
  576. isl_int_mul(v1->d, v1->d, v2->d);
  577. }
  578. v1 = isl_val_normalize(v1);
  579. }
  580. isl_val_free(v2);
  581. return v1;
  582. error:
  583. isl_val_free(v1);
  584. isl_val_free(v2);
  585. return NULL;
  586. }
  587. /* Return the sum of "v1" and "v2".
  588. */
  589. __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
  590. {
  591. if (!v1)
  592. return NULL;
  593. if (!isl_val_is_rat(v1))
  594. return v1;
  595. if (v2 == 0)
  596. return v1;
  597. v1 = isl_val_cow(v1);
  598. if (!v1)
  599. return NULL;
  600. isl_int_addmul_ui(v1->n, v1->d, v2);
  601. return v1;
  602. }
  603. /* Subtract "v2" from "v1".
  604. */
  605. __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
  606. {
  607. if (!v1 || !v2)
  608. goto error;
  609. if (isl_val_is_nan(v1)) {
  610. isl_val_free(v2);
  611. return v1;
  612. }
  613. if (isl_val_is_nan(v2)) {
  614. isl_val_free(v1);
  615. return v2;
  616. }
  617. if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
  618. (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
  619. isl_val_free(v2);
  620. return isl_val_set_nan(v1);
  621. }
  622. if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
  623. isl_val_free(v2);
  624. return v1;
  625. }
  626. if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
  627. isl_val_free(v1);
  628. return isl_val_neg(v2);
  629. }
  630. if (isl_val_is_zero(v2)) {
  631. isl_val_free(v2);
  632. return v1;
  633. }
  634. if (isl_val_is_zero(v1)) {
  635. isl_val_free(v1);
  636. return isl_val_neg(v2);
  637. }
  638. v1 = isl_val_cow(v1);
  639. if (!v1)
  640. goto error;
  641. if (isl_val_is_int(v1) && isl_val_is_int(v2))
  642. isl_int_sub(v1->n, v1->n, v2->n);
  643. else {
  644. if (isl_int_eq(v1->d, v2->d))
  645. isl_int_sub(v1->n, v1->n, v2->n);
  646. else {
  647. isl_int_mul(v1->n, v1->n, v2->d);
  648. isl_int_submul(v1->n, v2->n, v1->d);
  649. isl_int_mul(v1->d, v1->d, v2->d);
  650. }
  651. v1 = isl_val_normalize(v1);
  652. }
  653. isl_val_free(v2);
  654. return v1;
  655. error:
  656. isl_val_free(v1);
  657. isl_val_free(v2);
  658. return NULL;
  659. }
  660. /* Subtract "v2" from "v1".
  661. */
  662. __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
  663. {
  664. if (!v1)
  665. return NULL;
  666. if (!isl_val_is_rat(v1))
  667. return v1;
  668. if (v2 == 0)
  669. return v1;
  670. v1 = isl_val_cow(v1);
  671. if (!v1)
  672. return NULL;
  673. isl_int_submul_ui(v1->n, v1->d, v2);
  674. return v1;
  675. }
  676. /* Return the product of "v1" and "v2".
  677. */
  678. __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
  679. {
  680. if (!v1 || !v2)
  681. goto error;
  682. if (isl_val_is_nan(v1)) {
  683. isl_val_free(v2);
  684. return v1;
  685. }
  686. if (isl_val_is_nan(v2)) {
  687. isl_val_free(v1);
  688. return v2;
  689. }
  690. if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
  691. (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
  692. isl_val_free(v2);
  693. return isl_val_set_nan(v1);
  694. }
  695. if (isl_val_is_zero(v1)) {
  696. isl_val_free(v2);
  697. return v1;
  698. }
  699. if (isl_val_is_zero(v2)) {
  700. isl_val_free(v1);
  701. return v2;
  702. }
  703. if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
  704. if (isl_val_is_neg(v2))
  705. v1 = isl_val_neg(v1);
  706. isl_val_free(v2);
  707. return v1;
  708. }
  709. if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
  710. if (isl_val_is_neg(v1))
  711. v2 = isl_val_neg(v2);
  712. isl_val_free(v1);
  713. return v2;
  714. }
  715. v1 = isl_val_cow(v1);
  716. if (!v1)
  717. goto error;
  718. if (isl_val_is_int(v1) && isl_val_is_int(v2))
  719. isl_int_mul(v1->n, v1->n, v2->n);
  720. else {
  721. isl_int_mul(v1->n, v1->n, v2->n);
  722. isl_int_mul(v1->d, v1->d, v2->d);
  723. v1 = isl_val_normalize(v1);
  724. }
  725. isl_val_free(v2);
  726. return v1;
  727. error:
  728. isl_val_free(v1);
  729. isl_val_free(v2);
  730. return NULL;
  731. }
  732. /* Return the product of "v1" and "v2".
  733. *
  734. * This is a private copy of isl_val_mul for use in the generic
  735. * isl_multi_*_scale_val instantiated for isl_val.
  736. */
  737. __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
  738. __isl_take isl_val *v2)
  739. {
  740. return isl_val_mul(v1, v2);
  741. }
  742. /* Return the product of "v1" and "v2".
  743. */
  744. __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
  745. {
  746. if (!v1)
  747. return NULL;
  748. if (isl_val_is_nan(v1))
  749. return v1;
  750. if (!isl_val_is_rat(v1)) {
  751. if (v2 == 0)
  752. v1 = isl_val_set_nan(v1);
  753. return v1;
  754. }
  755. if (v2 == 1)
  756. return v1;
  757. v1 = isl_val_cow(v1);
  758. if (!v1)
  759. return NULL;
  760. isl_int_mul_ui(v1->n, v1->n, v2);
  761. return isl_val_normalize(v1);
  762. }
  763. /* Divide "v1" by "v2".
  764. */
  765. __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
  766. {
  767. if (!v1 || !v2)
  768. goto error;
  769. if (isl_val_is_nan(v1)) {
  770. isl_val_free(v2);
  771. return v1;
  772. }
  773. if (isl_val_is_nan(v2)) {
  774. isl_val_free(v1);
  775. return v2;
  776. }
  777. if (isl_val_is_zero(v2) ||
  778. (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
  779. isl_val_free(v2);
  780. return isl_val_set_nan(v1);
  781. }
  782. if (isl_val_is_zero(v1)) {
  783. isl_val_free(v2);
  784. return v1;
  785. }
  786. if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
  787. if (isl_val_is_neg(v2))
  788. v1 = isl_val_neg(v1);
  789. isl_val_free(v2);
  790. return v1;
  791. }
  792. if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
  793. isl_val_free(v2);
  794. return isl_val_set_zero(v1);
  795. }
  796. v1 = isl_val_cow(v1);
  797. if (!v1)
  798. goto error;
  799. if (isl_val_is_int(v2)) {
  800. isl_int_mul(v1->d, v1->d, v2->n);
  801. v1 = isl_val_normalize(v1);
  802. } else {
  803. isl_int_mul(v1->d, v1->d, v2->n);
  804. isl_int_mul(v1->n, v1->n, v2->d);
  805. v1 = isl_val_normalize(v1);
  806. }
  807. isl_val_free(v2);
  808. return v1;
  809. error:
  810. isl_val_free(v1);
  811. isl_val_free(v2);
  812. return NULL;
  813. }
  814. /* Divide "v1" by "v2".
  815. */
  816. __isl_give isl_val *isl_val_div_ui(__isl_take isl_val *v1, unsigned long v2)
  817. {
  818. if (!v1)
  819. return NULL;
  820. if (isl_val_is_nan(v1))
  821. return v1;
  822. if (v2 == 0)
  823. return isl_val_set_nan(v1);
  824. if (v2 == 1)
  825. return v1;
  826. if (isl_val_is_zero(v1))
  827. return v1;
  828. if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1))
  829. return v1;
  830. v1 = isl_val_cow(v1);
  831. if (!v1)
  832. return NULL;
  833. isl_int_mul_ui(v1->d, v1->d, v2);
  834. return isl_val_normalize(v1);
  835. }
  836. /* Divide "v1" by "v2".
  837. *
  838. * This is a private copy of isl_val_div for use in the generic
  839. * isl_multi_*_scale_down_val instantiated for isl_val.
  840. */
  841. __isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
  842. __isl_take isl_val *v2)
  843. {
  844. return isl_val_div(v1, v2);
  845. }
  846. /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
  847. */
  848. isl_bool isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  849. {
  850. if (!v1 || !v2)
  851. return isl_bool_error;
  852. if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
  853. isl_die(isl_val_get_ctx(v1), isl_error_invalid,
  854. "expecting two integers", return isl_bool_error);
  855. return isl_bool_ok(isl_int_is_divisible_by(v1->n, v2->n));
  856. }
  857. /* Given two integer values "v1" and "v2", return the residue of "v1"
  858. * modulo "v2".
  859. */
  860. __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
  861. {
  862. if (!v1 || !v2)
  863. goto error;
  864. if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
  865. isl_die(isl_val_get_ctx(v1), isl_error_invalid,
  866. "expecting two integers", goto error);
  867. if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
  868. isl_val_free(v2);
  869. return v1;
  870. }
  871. v1 = isl_val_cow(v1);
  872. if (!v1)
  873. goto error;
  874. isl_int_fdiv_r(v1->n, v1->n, v2->n);
  875. isl_val_free(v2);
  876. return v1;
  877. error:
  878. isl_val_free(v1);
  879. isl_val_free(v2);
  880. return NULL;
  881. }
  882. /* Given two integer values "v1" and "v2", return the residue of "v1"
  883. * modulo "v2".
  884. *
  885. * This is a private copy of isl_val_mod for use in the generic
  886. * isl_multi_*_mod_multi_val instantiated for isl_val.
  887. */
  888. __isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
  889. __isl_take isl_val *v2)
  890. {
  891. return isl_val_mod(v1, v2);
  892. }
  893. /* Given two integer values, return their greatest common divisor.
  894. */
  895. __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
  896. {
  897. if (!v1 || !v2)
  898. goto error;
  899. if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
  900. isl_die(isl_val_get_ctx(v1), isl_error_invalid,
  901. "expecting two integers", goto error);
  902. if (isl_val_eq(v1, v2)) {
  903. isl_val_free(v2);
  904. return v1;
  905. }
  906. if (isl_val_is_one(v1)) {
  907. isl_val_free(v2);
  908. return v1;
  909. }
  910. if (isl_val_is_one(v2)) {
  911. isl_val_free(v1);
  912. return v2;
  913. }
  914. v1 = isl_val_cow(v1);
  915. if (!v1)
  916. goto error;
  917. isl_int_gcd(v1->n, v1->n, v2->n);
  918. isl_val_free(v2);
  919. return v1;
  920. error:
  921. isl_val_free(v1);
  922. isl_val_free(v2);
  923. return NULL;
  924. }
  925. /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
  926. */
  927. static void isl_int_gcdext(isl_int *g, isl_int *x, isl_int *y,
  928. isl_int a, isl_int b)
  929. {
  930. isl_int d, tmp;
  931. isl_int a_copy, b_copy;
  932. isl_int_init(a_copy);
  933. isl_int_init(b_copy);
  934. isl_int_init(d);
  935. isl_int_init(tmp);
  936. isl_int_set(a_copy, a);
  937. isl_int_set(b_copy, b);
  938. isl_int_abs(*g, a_copy);
  939. isl_int_abs(d, b_copy);
  940. isl_int_set_si(*x, 1);
  941. isl_int_set_si(*y, 0);
  942. while (isl_int_is_pos(d)) {
  943. isl_int_fdiv_q(tmp, *g, d);
  944. isl_int_submul(*x, tmp, *y);
  945. isl_int_submul(*g, tmp, d);
  946. isl_int_swap(*g, d);
  947. isl_int_swap(*x, *y);
  948. }
  949. if (isl_int_is_zero(a_copy))
  950. isl_int_set_si(*x, 0);
  951. else if (isl_int_is_neg(a_copy))
  952. isl_int_neg(*x, *x);
  953. if (isl_int_is_zero(b_copy))
  954. isl_int_set_si(*y, 0);
  955. else {
  956. isl_int_mul(tmp, a_copy, *x);
  957. isl_int_sub(tmp, *g, tmp);
  958. isl_int_divexact(*y, tmp, b_copy);
  959. }
  960. isl_int_clear(d);
  961. isl_int_clear(tmp);
  962. isl_int_clear(a_copy);
  963. isl_int_clear(b_copy);
  964. }
  965. /* Given two integer values v1 and v2, return their greatest common divisor g,
  966. * as well as two integers x and y such that x * v1 + y * v2 = g.
  967. */
  968. __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
  969. __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
  970. {
  971. isl_ctx *ctx;
  972. isl_val *a = NULL, *b = NULL;
  973. if (!x && !y)
  974. return isl_val_gcd(v1, v2);
  975. if (!v1 || !v2)
  976. goto error;
  977. ctx = isl_val_get_ctx(v1);
  978. if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
  979. isl_die(ctx, isl_error_invalid,
  980. "expecting two integers", goto error);
  981. v1 = isl_val_cow(v1);
  982. a = isl_val_alloc(ctx);
  983. b = isl_val_alloc(ctx);
  984. if (!v1 || !a || !b)
  985. goto error;
  986. isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
  987. if (x) {
  988. isl_int_set_si(a->d, 1);
  989. *x = a;
  990. } else
  991. isl_val_free(a);
  992. if (y) {
  993. isl_int_set_si(b->d, 1);
  994. *y = b;
  995. } else
  996. isl_val_free(b);
  997. isl_val_free(v2);
  998. return v1;
  999. error:
  1000. isl_val_free(v1);
  1001. isl_val_free(v2);
  1002. isl_val_free(a);
  1003. isl_val_free(b);
  1004. if (x)
  1005. *x = NULL;
  1006. if (y)
  1007. *y = NULL;
  1008. return NULL;
  1009. }
  1010. /* Does "v" represent an integer value?
  1011. */
  1012. isl_bool isl_val_is_int(__isl_keep isl_val *v)
  1013. {
  1014. if (!v)
  1015. return isl_bool_error;
  1016. return isl_bool_ok(isl_int_is_one(v->d));
  1017. }
  1018. /* Does "v" represent a rational value?
  1019. */
  1020. isl_bool isl_val_is_rat(__isl_keep isl_val *v)
  1021. {
  1022. if (!v)
  1023. return isl_bool_error;
  1024. return isl_bool_ok(!isl_int_is_zero(v->d));
  1025. }
  1026. /* Does "v" represent NaN?
  1027. */
  1028. isl_bool isl_val_is_nan(__isl_keep isl_val *v)
  1029. {
  1030. if (!v)
  1031. return isl_bool_error;
  1032. return isl_bool_ok(isl_int_is_zero(v->n) && isl_int_is_zero(v->d));
  1033. }
  1034. /* Does "v" represent +infinity?
  1035. */
  1036. isl_bool isl_val_is_infty(__isl_keep isl_val *v)
  1037. {
  1038. if (!v)
  1039. return isl_bool_error;
  1040. return isl_bool_ok(isl_int_is_pos(v->n) && isl_int_is_zero(v->d));
  1041. }
  1042. /* Does "v" represent -infinity?
  1043. */
  1044. isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
  1045. {
  1046. if (!v)
  1047. return isl_bool_error;
  1048. return isl_bool_ok(isl_int_is_neg(v->n) && isl_int_is_zero(v->d));
  1049. }
  1050. /* Does "v" represent the integer zero?
  1051. */
  1052. isl_bool isl_val_is_zero(__isl_keep isl_val *v)
  1053. {
  1054. if (!v)
  1055. return isl_bool_error;
  1056. return isl_bool_ok(isl_int_is_zero(v->n) && !isl_int_is_zero(v->d));
  1057. }
  1058. /* Does "v" represent the integer one?
  1059. */
  1060. isl_bool isl_val_is_one(__isl_keep isl_val *v)
  1061. {
  1062. if (!v)
  1063. return isl_bool_error;
  1064. if (isl_val_is_nan(v))
  1065. return isl_bool_false;
  1066. return isl_bool_ok(isl_int_eq(v->n, v->d));
  1067. }
  1068. /* Does "v" represent the integer negative one?
  1069. */
  1070. isl_bool isl_val_is_negone(__isl_keep isl_val *v)
  1071. {
  1072. if (!v)
  1073. return isl_bool_error;
  1074. return isl_bool_ok(isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d));
  1075. }
  1076. /* Is "v" (strictly) positive?
  1077. */
  1078. isl_bool isl_val_is_pos(__isl_keep isl_val *v)
  1079. {
  1080. if (!v)
  1081. return isl_bool_error;
  1082. return isl_bool_ok(isl_int_is_pos(v->n));
  1083. }
  1084. /* Is "v" (strictly) negative?
  1085. */
  1086. isl_bool isl_val_is_neg(__isl_keep isl_val *v)
  1087. {
  1088. if (!v)
  1089. return isl_bool_error;
  1090. return isl_bool_ok(isl_int_is_neg(v->n));
  1091. }
  1092. /* Is "v" non-negative?
  1093. */
  1094. isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
  1095. {
  1096. if (!v)
  1097. return isl_bool_error;
  1098. if (isl_val_is_nan(v))
  1099. return isl_bool_false;
  1100. return isl_bool_ok(isl_int_is_nonneg(v->n));
  1101. }
  1102. /* Is "v" non-positive?
  1103. */
  1104. isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
  1105. {
  1106. if (!v)
  1107. return isl_bool_error;
  1108. if (isl_val_is_nan(v))
  1109. return isl_bool_false;
  1110. return isl_bool_ok(isl_int_is_nonpos(v->n));
  1111. }
  1112. /* Return the sign of "v".
  1113. *
  1114. * The sign of NaN is undefined.
  1115. */
  1116. int isl_val_sgn(__isl_keep isl_val *v)
  1117. {
  1118. if (!v)
  1119. return 0;
  1120. if (isl_val_is_zero(v))
  1121. return 0;
  1122. if (isl_val_is_pos(v))
  1123. return 1;
  1124. return -1;
  1125. }
  1126. /* Is "v1" (strictly) less than "v2"?
  1127. */
  1128. isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1129. {
  1130. isl_int t;
  1131. isl_bool lt;
  1132. if (!v1 || !v2)
  1133. return isl_bool_error;
  1134. if (isl_val_is_int(v1) && isl_val_is_int(v2))
  1135. return isl_bool_ok(isl_int_lt(v1->n, v2->n));
  1136. if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
  1137. return isl_bool_false;
  1138. if (isl_val_eq(v1, v2))
  1139. return isl_bool_false;
  1140. if (isl_val_is_infty(v2))
  1141. return isl_bool_true;
  1142. if (isl_val_is_infty(v1))
  1143. return isl_bool_false;
  1144. if (isl_val_is_neginfty(v1))
  1145. return isl_bool_true;
  1146. if (isl_val_is_neginfty(v2))
  1147. return isl_bool_false;
  1148. isl_int_init(t);
  1149. isl_int_mul(t, v1->n, v2->d);
  1150. isl_int_submul(t, v2->n, v1->d);
  1151. lt = isl_bool_ok(isl_int_is_neg(t));
  1152. isl_int_clear(t);
  1153. return lt;
  1154. }
  1155. /* Is "v1" (strictly) greater than "v2"?
  1156. */
  1157. isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1158. {
  1159. return isl_val_lt(v2, v1);
  1160. }
  1161. /* Is "v" (strictly) greater than "i"?
  1162. */
  1163. isl_bool isl_val_gt_si(__isl_keep isl_val *v, long i)
  1164. {
  1165. isl_val *vi;
  1166. isl_bool res;
  1167. if (!v)
  1168. return isl_bool_error;
  1169. if (isl_val_is_int(v))
  1170. return isl_bool_ok(isl_int_cmp_si(v->n, i) > 0);
  1171. if (isl_val_is_nan(v))
  1172. return isl_bool_false;
  1173. if (isl_val_is_infty(v))
  1174. return isl_bool_true;
  1175. if (isl_val_is_neginfty(v))
  1176. return isl_bool_false;
  1177. vi = isl_val_int_from_si(isl_val_get_ctx(v), i);
  1178. res = isl_bool_ok(isl_val_gt(v, vi));
  1179. isl_val_free(vi);
  1180. return res;
  1181. }
  1182. /* Is "v1" less than or equal to "v2"?
  1183. */
  1184. isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1185. {
  1186. isl_int t;
  1187. isl_bool le;
  1188. if (!v1 || !v2)
  1189. return isl_bool_error;
  1190. if (isl_val_is_int(v1) && isl_val_is_int(v2))
  1191. return isl_bool_ok(isl_int_le(v1->n, v2->n));
  1192. if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
  1193. return isl_bool_false;
  1194. if (isl_val_eq(v1, v2))
  1195. return isl_bool_true;
  1196. if (isl_val_is_infty(v2))
  1197. return isl_bool_true;
  1198. if (isl_val_is_infty(v1))
  1199. return isl_bool_false;
  1200. if (isl_val_is_neginfty(v1))
  1201. return isl_bool_true;
  1202. if (isl_val_is_neginfty(v2))
  1203. return isl_bool_false;
  1204. isl_int_init(t);
  1205. isl_int_mul(t, v1->n, v2->d);
  1206. isl_int_submul(t, v2->n, v1->d);
  1207. le = isl_bool_ok(isl_int_is_nonpos(t));
  1208. isl_int_clear(t);
  1209. return le;
  1210. }
  1211. /* Is "v1" greater than or equal to "v2"?
  1212. */
  1213. isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1214. {
  1215. return isl_val_le(v2, v1);
  1216. }
  1217. /* How does "v" compare to "i"?
  1218. *
  1219. * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
  1220. *
  1221. * If v is NaN (or NULL), then the result is undefined.
  1222. */
  1223. int isl_val_cmp_si(__isl_keep isl_val *v, long i)
  1224. {
  1225. isl_int t;
  1226. int cmp;
  1227. if (!v)
  1228. return 0;
  1229. if (isl_val_is_int(v))
  1230. return isl_int_cmp_si(v->n, i);
  1231. if (isl_val_is_nan(v))
  1232. return 0;
  1233. if (isl_val_is_infty(v))
  1234. return 1;
  1235. if (isl_val_is_neginfty(v))
  1236. return -1;
  1237. isl_int_init(t);
  1238. isl_int_mul_si(t, v->d, i);
  1239. isl_int_sub(t, v->n, t);
  1240. cmp = isl_int_sgn(t);
  1241. isl_int_clear(t);
  1242. return cmp;
  1243. }
  1244. /* Is "v1" equal to "v2"?
  1245. */
  1246. isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1247. {
  1248. if (!v1 || !v2)
  1249. return isl_bool_error;
  1250. if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
  1251. return isl_bool_false;
  1252. return isl_bool_ok(isl_int_eq(v1->n, v2->n) &&
  1253. isl_int_eq(v1->d, v2->d));
  1254. }
  1255. /* Is "v" equal to "i"?
  1256. */
  1257. isl_bool isl_val_eq_si(__isl_keep isl_val *v, long i)
  1258. {
  1259. if (!v)
  1260. return isl_bool_error;
  1261. if (!isl_val_is_int(v))
  1262. return isl_bool_false;
  1263. return isl_bool_ok(isl_int_cmp_si(v->n, i) == 0);
  1264. }
  1265. /* Is "v1" equal to "v2" in absolute value?
  1266. */
  1267. isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1268. {
  1269. if (!v1 || !v2)
  1270. return isl_bool_error;
  1271. if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
  1272. return isl_bool_false;
  1273. return isl_bool_ok(isl_int_abs_eq(v1->n, v2->n) &&
  1274. isl_int_eq(v1->d, v2->d));
  1275. }
  1276. /* Is "v1" different from "v2"?
  1277. */
  1278. isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
  1279. {
  1280. if (!v1 || !v2)
  1281. return isl_bool_error;
  1282. if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
  1283. return isl_bool_false;
  1284. return isl_bool_ok(isl_int_ne(v1->n, v2->n) ||
  1285. isl_int_ne(v1->d, v2->d));
  1286. }
  1287. /* Print a textual representation of "v" onto "p".
  1288. */
  1289. __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
  1290. __isl_keep isl_val *v)
  1291. {
  1292. int neg;
  1293. if (!p || !v)
  1294. return isl_printer_free(p);
  1295. neg = isl_int_is_neg(v->n);
  1296. if (neg) {
  1297. p = isl_printer_print_str(p, "-");
  1298. isl_int_neg(v->n, v->n);
  1299. }
  1300. if (isl_int_is_zero(v->d)) {
  1301. int sgn = isl_int_sgn(v->n);
  1302. p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
  1303. sgn == 0 ? "NaN" : "infty");
  1304. } else
  1305. p = isl_printer_print_isl_int(p, v->n);
  1306. if (neg)
  1307. isl_int_neg(v->n, v->n);
  1308. if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
  1309. p = isl_printer_print_str(p, "/");
  1310. p = isl_printer_print_isl_int(p, v->d);
  1311. }
  1312. return p;
  1313. }
  1314. /* Is "val1" (obviously) equal to "val2"?
  1315. *
  1316. * This is a private copy of isl_val_eq for use in the generic
  1317. * isl_multi_*_plain_is_equal instantiated for isl_val.
  1318. */
  1319. isl_bool isl_val_plain_is_equal(__isl_keep isl_val *val1,
  1320. __isl_keep isl_val *val2)
  1321. {
  1322. return isl_val_eq(val1, val2);
  1323. }
  1324. /* Does "v" have any non-zero coefficients
  1325. * for any dimension in the given range?
  1326. *
  1327. * This function is only meant to be used in the generic isl_multi_*
  1328. * functions which have to deal with base objects that have an associated
  1329. * space. Since an isl_val does not have any coefficients, this function
  1330. * always returns isl_bool_false.
  1331. */
  1332. isl_bool isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
  1333. unsigned first, unsigned n)
  1334. {
  1335. if (!v)
  1336. return isl_bool_error;
  1337. return isl_bool_false;
  1338. }
  1339. /* Insert "n" dimensions of type "type" at position "first".
  1340. *
  1341. * This function is only meant to be used in the generic isl_multi_*
  1342. * functions which have to deal with base objects that have an associated
  1343. * space. Since an isl_val does not have an associated space, this function
  1344. * does not do anything.
  1345. */
  1346. __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
  1347. enum isl_dim_type type, unsigned first, unsigned n)
  1348. {
  1349. return v;
  1350. }
  1351. /* Change the name of the dimension of type "type" at position "pos" to "s".
  1352. *
  1353. * This function is only meant to be used in the generic isl_multi_*
  1354. * functions which have to deal with base objects that have an associated
  1355. * space. Since an isl_val does not have an associated space, this function
  1356. * does not do anything.
  1357. */
  1358. __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
  1359. enum isl_dim_type type, unsigned pos, const char *s)
  1360. {
  1361. return v;
  1362. }
  1363. /* Return an isl_val that is zero on "ls".
  1364. *
  1365. * This function is only meant to be used in the generic isl_multi_*
  1366. * functions which have to deal with base objects that have an associated
  1367. * space. Since an isl_val does not have an associated space, this function
  1368. * simply returns a zero isl_val in the same context as "ls".
  1369. */
  1370. __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
  1371. {
  1372. isl_ctx *ctx;
  1373. if (!ls)
  1374. return NULL;
  1375. ctx = isl_local_space_get_ctx(ls);
  1376. isl_local_space_free(ls);
  1377. return isl_val_zero(ctx);
  1378. }
  1379. #define isl_val_involves_nan isl_val_is_nan
  1380. #undef BASE
  1381. #define BASE val
  1382. #include <isl_multi_no_domain_templ.c>
  1383. #include <isl_multi_no_explicit_domain.c>
  1384. #include <isl_multi_templ.c>
  1385. #include <isl_multi_arith_templ.c>
  1386. #include <isl_multi_dim_id_templ.c>
  1387. #include <isl_multi_dims.c>
  1388. #include <isl_multi_min_max_templ.c>
  1389. #include <isl_multi_nan_templ.c>
  1390. #include <isl_multi_product_templ.c>
  1391. #include <isl_multi_splice_templ.c>
  1392. #include <isl_multi_tuple_id_templ.c>
  1393. #include <isl_multi_zero_templ.c>
  1394. /* Does "mv" consist of only zeros?
  1395. */
  1396. isl_bool isl_multi_val_is_zero(__isl_keep isl_multi_val *mv)
  1397. {
  1398. return isl_multi_val_every(mv, &isl_val_is_zero);
  1399. }
  1400. /* Apply "fn" to each of the elements of "mv" with as second argument "v".
  1401. */
  1402. static __isl_give isl_multi_val *isl_multi_val_fn_val(
  1403. __isl_take isl_multi_val *mv,
  1404. __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
  1405. __isl_take isl_val *v2),
  1406. __isl_take isl_val *v)
  1407. {
  1408. int i;
  1409. mv = isl_multi_val_cow(mv);
  1410. if (!mv || !v)
  1411. goto error;
  1412. for (i = 0; i < mv->n; ++i) {
  1413. mv->u.p[i] = fn(mv->u.p[i], isl_val_copy(v));
  1414. if (!mv->u.p[i])
  1415. goto error;
  1416. }
  1417. isl_val_free(v);
  1418. return mv;
  1419. error:
  1420. isl_val_free(v);
  1421. isl_multi_val_free(mv);
  1422. return NULL;
  1423. }
  1424. /* Add "v" to each of the elements of "mv".
  1425. */
  1426. __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
  1427. __isl_take isl_val *v)
  1428. {
  1429. if (!v)
  1430. return isl_multi_val_free(mv);
  1431. if (isl_val_is_zero(v)) {
  1432. isl_val_free(v);
  1433. return mv;
  1434. }
  1435. return isl_multi_val_fn_val(mv, &isl_val_add, v);
  1436. }
  1437. /* Reduce the elements of "mv" modulo "v".
  1438. */
  1439. __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
  1440. __isl_take isl_val *v)
  1441. {
  1442. return isl_multi_val_fn_val(mv, &isl_val_mod, v);
  1443. }