isl_seq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright 2008-2009 Katholieke Universiteit Leuven
  3. * Copyright 2011 INRIA Saclay
  4. *
  5. * Use of this software is governed by the MIT license
  6. *
  7. * Written by Sven Verdoolaege, K.U.Leuven, Departement
  8. * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  9. */
  10. #include <isl_ctx_private.h>
  11. #include <isl_seq.h>
  12. void isl_seq_clr(isl_int *p, unsigned len)
  13. {
  14. int i;
  15. for (i = 0; i < len; ++i)
  16. isl_int_set_si(p[i], 0);
  17. }
  18. void isl_seq_set_si(isl_int *p, int v, unsigned len)
  19. {
  20. int i;
  21. for (i = 0; i < len; ++i)
  22. isl_int_set_si(p[i], v);
  23. }
  24. void isl_seq_set(isl_int *p, isl_int v, unsigned len)
  25. {
  26. int i;
  27. for (i = 0; i < len; ++i)
  28. isl_int_set(p[i], v);
  29. }
  30. void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
  31. {
  32. int i;
  33. for (i = 0; i < len; ++i)
  34. isl_int_neg(dst[i], src[i]);
  35. }
  36. void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
  37. {
  38. int i;
  39. for (i = 0; i < len; ++i)
  40. isl_int_set(dst[i], src[i]);
  41. }
  42. void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
  43. {
  44. int i;
  45. for (i = 0; i < len; ++i)
  46. isl_int_submul(dst[i], f, src[i]);
  47. }
  48. void isl_seq_addmul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
  49. {
  50. int i;
  51. for (i = 0; i < len; ++i)
  52. isl_int_addmul(dst[i], f, src[i]);
  53. }
  54. void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
  55. {
  56. int i;
  57. for (i = 0; i < len; ++i)
  58. isl_int_swap_or_set(dst[i], src[i]);
  59. }
  60. void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
  61. {
  62. int i;
  63. for (i = 0; i < len; ++i)
  64. isl_int_mul(dst[i], src[i], m);
  65. }
  66. void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
  67. {
  68. int i;
  69. for (i = 0; i < len; ++i)
  70. isl_int_divexact(dst[i], src[i], m);
  71. }
  72. void isl_seq_cdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
  73. {
  74. int i;
  75. for (i = 0; i < len; ++i)
  76. isl_int_cdiv_q(dst[i], src[i], m);
  77. }
  78. void isl_seq_fdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
  79. {
  80. int i;
  81. for (i = 0; i < len; ++i)
  82. isl_int_fdiv_q(dst[i], src[i], m);
  83. }
  84. void isl_seq_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
  85. {
  86. int i;
  87. for (i = 0; i < len; ++i)
  88. isl_int_fdiv_r(dst[i], src[i], m);
  89. }
  90. void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
  91. isl_int m2, isl_int *src2, unsigned len)
  92. {
  93. int i;
  94. isl_int tmp;
  95. if (dst == src1 && isl_int_is_one(m1)) {
  96. if (isl_int_is_zero(m2))
  97. return;
  98. for (i = 0; i < len; ++i)
  99. isl_int_addmul(src1[i], m2, src2[i]);
  100. return;
  101. }
  102. isl_int_init(tmp);
  103. for (i = 0; i < len; ++i) {
  104. isl_int_mul(tmp, m1, src1[i]);
  105. isl_int_addmul(tmp, m2, src2[i]);
  106. isl_int_set(dst[i], tmp);
  107. }
  108. isl_int_clear(tmp);
  109. }
  110. /* Eliminate element "pos" from "dst" using "src".
  111. * In particular, let d = dst[pos] and s = src[pos], then
  112. * dst is replaced by (|s| dst - sgn(s)d src)/gcd(s,d),
  113. * such that dst[pos] is zero after the elimination.
  114. * If "m" is not NULL, then *m is multiplied by |s|/gcd(s,d).
  115. * That is, it is multiplied by the same factor as "dst".
  116. */
  117. void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
  118. isl_int *m)
  119. {
  120. isl_int a;
  121. isl_int b;
  122. if (isl_int_is_zero(dst[pos]))
  123. return;
  124. isl_int_init(a);
  125. isl_int_init(b);
  126. isl_int_gcd(a, src[pos], dst[pos]);
  127. isl_int_divexact(b, dst[pos], a);
  128. if (isl_int_is_pos(src[pos]))
  129. isl_int_neg(b, b);
  130. isl_int_divexact(a, src[pos], a);
  131. isl_int_abs(a, a);
  132. isl_seq_combine(dst, a, dst, b, src, len);
  133. if (m)
  134. isl_int_mul(*m, *m, a);
  135. isl_int_clear(a);
  136. isl_int_clear(b);
  137. }
  138. int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
  139. {
  140. int i;
  141. for (i = 0; i < len; ++i)
  142. if (isl_int_ne(p1[i], p2[i]))
  143. return 0;
  144. return 1;
  145. }
  146. int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
  147. {
  148. int i;
  149. int cmp;
  150. for (i = 0; i < len; ++i)
  151. if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
  152. return cmp;
  153. return 0;
  154. }
  155. int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
  156. {
  157. int i;
  158. for (i = 0; i < len; ++i) {
  159. if (isl_int_abs_ne(p1[i], p2[i]))
  160. return 0;
  161. if (isl_int_is_zero(p1[i]))
  162. continue;
  163. if (isl_int_eq(p1[i], p2[i]))
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. int isl_seq_first_non_zero(isl_int *p, unsigned len)
  169. {
  170. int i;
  171. for (i = 0; i < len; ++i)
  172. if (!isl_int_is_zero(p[i]))
  173. return i;
  174. return -1;
  175. }
  176. int isl_seq_last_non_zero(isl_int *p, unsigned len)
  177. {
  178. int i;
  179. for (i = len - 1; i >= 0; --i)
  180. if (!isl_int_is_zero(p[i]))
  181. return i;
  182. return -1;
  183. }
  184. void isl_seq_abs_max(isl_int *p, unsigned len, isl_int *max)
  185. {
  186. int i;
  187. isl_int_set_si(*max, 0);
  188. for (i = 0; i < len; ++i)
  189. if (isl_int_abs_gt(p[i], *max))
  190. isl_int_abs(*max, p[i]);
  191. }
  192. int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
  193. {
  194. int i, min = isl_seq_first_non_zero(p, len);
  195. if (min < 0)
  196. return -1;
  197. for (i = min + 1; i < len; ++i) {
  198. if (isl_int_is_zero(p[i]))
  199. continue;
  200. if (isl_int_abs_lt(p[i], p[min]))
  201. min = i;
  202. }
  203. return min;
  204. }
  205. void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
  206. {
  207. int i, min = isl_seq_abs_min_non_zero(p, len);
  208. if (min < 0) {
  209. isl_int_set_si(*gcd, 0);
  210. return;
  211. }
  212. isl_int_abs(*gcd, p[min]);
  213. for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
  214. if (i == min)
  215. continue;
  216. if (isl_int_is_zero(p[i]))
  217. continue;
  218. isl_int_gcd(*gcd, *gcd, p[i]);
  219. }
  220. }
  221. void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
  222. {
  223. if (len == 0)
  224. return;
  225. isl_seq_gcd(p, len, &ctx->normalize_gcd);
  226. if (!isl_int_is_zero(ctx->normalize_gcd) &&
  227. !isl_int_is_one(ctx->normalize_gcd))
  228. isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
  229. }
  230. void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
  231. {
  232. int i;
  233. if (len == 0) {
  234. isl_int_set_si(*lcm, 1);
  235. return;
  236. }
  237. isl_int_set(*lcm, p[0]);
  238. for (i = 1; i < len; ++i)
  239. isl_int_lcm(*lcm, *lcm, p[i]);
  240. }
  241. void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
  242. isl_int *prod)
  243. {
  244. int i;
  245. if (len == 0) {
  246. isl_int_set_si(*prod, 0);
  247. return;
  248. }
  249. isl_int_mul(*prod, p1[0], p2[0]);
  250. for (i = 1; i < len; ++i)
  251. isl_int_addmul(*prod, p1[i], p2[i]);
  252. }
  253. uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
  254. {
  255. int i;
  256. for (i = 0; i < len; ++i) {
  257. if (isl_int_is_zero(p[i]))
  258. continue;
  259. hash *= 16777619;
  260. hash ^= (i & 0xFF);
  261. hash = isl_int_hash(p[i], hash);
  262. }
  263. return hash;
  264. }
  265. /* Given two affine expressions "p" of length p_len (including the
  266. * denominator and the constant term) and "subs" of length subs_len,
  267. * plug in "subs" for the variable at position "pos".
  268. * The variables of "subs" and "p" are assumed to match up to subs_len,
  269. * but "p" may have additional variables.
  270. * "v" is an initialized isl_int that can be used internally.
  271. *
  272. * In particular, if "p" represents the expression
  273. *
  274. * (a i + g)/m
  275. *
  276. * with i the variable at position "pos" and "subs" represents the expression
  277. *
  278. * f/d
  279. *
  280. * then the result represents the expression
  281. *
  282. * (a f + d g)/(m d)
  283. *
  284. */
  285. void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
  286. int p_len, int subs_len, isl_int v)
  287. {
  288. isl_int_set(v, p[1 + pos]);
  289. isl_int_set_si(p[1 + pos], 0);
  290. isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
  291. isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
  292. isl_int_mul(p[0], p[0], subs[0]);
  293. }
  294. uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
  295. {
  296. uint32_t hash = isl_hash_init();
  297. return isl_seq_hash(p, len, hash);
  298. }
  299. uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
  300. {
  301. uint32_t hash;
  302. hash = isl_seq_get_hash(p, len);
  303. return isl_hash_bits(hash, bits);
  304. }
  305. void isl_seq_dump(isl_int *p, unsigned len)
  306. {
  307. int i;
  308. for (i = 0; i < len; ++i) {
  309. if (i)
  310. fprintf(stderr, " ");
  311. isl_int_print(stderr, p[i], 0);
  312. }
  313. fprintf(stderr, "\n");
  314. }