You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2679 lines
78KB

  1. /* Polynomial integer classes.
  2. Copyright (C) 2014-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file provides a representation of sizes and offsets whose exact
  16. values depend on certain runtime properties. The motivating example
  17. is the Arm SVE ISA, in which the number of vector elements is only
  18. known at runtime. See doc/poly-int.texi for more details.
  19. Tests for poly-int.h are located in testsuite/gcc.dg/plugin,
  20. since they are too expensive (in terms of binary size) to be
  21. included as selftests. */
  22. #ifndef HAVE_POLY_INT_H
  23. #define HAVE_POLY_INT_H
  24. template<unsigned int N, typename T> struct poly_int_pod;
  25. template<unsigned int N, typename T> class poly_int;
  26. /* poly_coeff_traiits<T> describes the properties of a poly_int
  27. coefficient type T:
  28. - poly_coeff_traits<T1>::rank is less than poly_coeff_traits<T2>::rank
  29. if T1 can promote to T2. For C-like types the rank is:
  30. (2 * number of bytes) + (unsigned ? 1 : 0)
  31. wide_ints don't have a normal rank and so use a value of INT_MAX.
  32. Any fixed-width integer should be promoted to wide_int if possible
  33. and lead to an error otherwise.
  34. - poly_coeff_traits<T>::int_type is the type to which an integer
  35. literal should be cast before comparing it with T.
  36. - poly_coeff_traits<T>::precision is the number of bits that T can hold.
  37. - poly_coeff_traits<T>::signedness is:
  38. 0 if T is unsigned
  39. 1 if T is signed
  40. -1 if T has no inherent sign (as for wide_int).
  41. - poly_coeff_traits<T>::max_value, if defined, is the maximum value of T.
  42. - poly_coeff_traits<T>::result is a type that can hold results of
  43. operations on T. This is different from T itself in cases where T
  44. is the result of an accessor like wi::to_offset. */
  45. template<typename T, wi::precision_type = wi::int_traits<T>::precision_type>
  46. struct poly_coeff_traits;
  47. template<typename T>
  48. struct poly_coeff_traits<T, wi::FLEXIBLE_PRECISION>
  49. {
  50. typedef T result;
  51. typedef T int_type;
  52. static const int signedness = (T (0) >= T (-1));
  53. static const int precision = sizeof (T) * CHAR_BIT;
  54. static const T max_value = (signedness
  55. ? ((T (1) << (precision - 2))
  56. + ((T (1) << (precision - 2)) - 1))
  57. : T (-1));
  58. static const int rank = sizeof (T) * 2 + !signedness;
  59. };
  60. template<typename T>
  61. struct poly_coeff_traits<T, wi::VAR_PRECISION>
  62. {
  63. typedef T result;
  64. typedef int int_type;
  65. static const int signedness = -1;
  66. static const int precision = WIDE_INT_MAX_PRECISION;
  67. static const int rank = INT_MAX;
  68. };
  69. template<typename T>
  70. struct poly_coeff_traits<T, wi::CONST_PRECISION>
  71. {
  72. typedef WI_UNARY_RESULT (T) result;
  73. typedef int int_type;
  74. /* These types are always signed. */
  75. static const int signedness = 1;
  76. static const int precision = wi::int_traits<T>::precision;
  77. static const int rank = precision * 2 / CHAR_BIT;
  78. };
  79. /* Information about a pair of coefficient types. */
  80. template<typename T1, typename T2>
  81. struct poly_coeff_pair_traits
  82. {
  83. /* True if T1 can represent all the values of T2.
  84. Either:
  85. - T1 should be a type with the same signedness as T2 and no less
  86. precision. This allows things like int16_t -> int16_t and
  87. uint32_t -> uint64_t.
  88. - T1 should be signed, T2 should be unsigned, and T1 should be
  89. wider than T2. This allows things like uint16_t -> int32_t.
  90. This rules out cases in which T1 has less precision than T2 or where
  91. the conversion would reinterpret the top bit. E.g. int16_t -> uint32_t
  92. can be dangerous and should have an explicit cast if deliberate. */
  93. static const bool lossless_p = (poly_coeff_traits<T1>::signedness
  94. == poly_coeff_traits<T2>::signedness
  95. ? (poly_coeff_traits<T1>::precision
  96. >= poly_coeff_traits<T2>::precision)
  97. : (poly_coeff_traits<T1>::signedness == 1
  98. && poly_coeff_traits<T2>::signedness == 0
  99. && (poly_coeff_traits<T1>::precision
  100. > poly_coeff_traits<T2>::precision)));
  101. /* 0 if T1 op T2 should promote to HOST_WIDE_INT,
  102. 1 if T1 op T2 should promote to unsigned HOST_WIDE_INT,
  103. 2 if T1 op T2 should use wide-int rules. */
  104. #define RANK(X) poly_coeff_traits<X>::rank
  105. static const int result_kind
  106. = ((RANK (T1) <= RANK (HOST_WIDE_INT)
  107. && RANK (T2) <= RANK (HOST_WIDE_INT))
  108. ? 0
  109. : (RANK (T1) <= RANK (unsigned HOST_WIDE_INT)
  110. && RANK (T2) <= RANK (unsigned HOST_WIDE_INT))
  111. ? 1 : 2);
  112. #undef RANK
  113. };
  114. /* SFINAE class that makes T3 available as "type" if T2 can represent all the
  115. values in T1. */
  116. template<typename T1, typename T2, typename T3,
  117. bool lossless_p = poly_coeff_pair_traits<T1, T2>::lossless_p>
  118. struct if_lossless;
  119. template<typename T1, typename T2, typename T3>
  120. struct if_lossless<T1, T2, T3, true>
  121. {
  122. typedef T3 type;
  123. };
  124. /* poly_int_traits<T> describes an integer type T that might be polynomial
  125. or non-polynomial:
  126. - poly_int_traits<T>::is_poly is true if T is a poly_int-based type
  127. and false otherwise.
  128. - poly_int_traits<T>::num_coeffs gives the number of coefficients in T
  129. if T is a poly_int and 1 otherwise.
  130. - poly_int_traits<T>::coeff_type gives the coefficent type of T if T
  131. is a poly_int and T itself otherwise
  132. - poly_int_traits<T>::int_type is a shorthand for
  133. typename poly_coeff_traits<coeff_type>::int_type. */
  134. template<typename T>
  135. struct poly_int_traits
  136. {
  137. static const bool is_poly = false;
  138. static const unsigned int num_coeffs = 1;
  139. typedef T coeff_type;
  140. typedef typename poly_coeff_traits<T>::int_type int_type;
  141. };
  142. template<unsigned int N, typename C>
  143. struct poly_int_traits<poly_int_pod<N, C> >
  144. {
  145. static const bool is_poly = true;
  146. static const unsigned int num_coeffs = N;
  147. typedef C coeff_type;
  148. typedef typename poly_coeff_traits<C>::int_type int_type;
  149. };
  150. template<unsigned int N, typename C>
  151. struct poly_int_traits<poly_int<N, C> > : poly_int_traits<poly_int_pod<N, C> >
  152. {
  153. };
  154. /* SFINAE class that makes T2 available as "type" if T1 is a non-polynomial
  155. type. */
  156. template<typename T1, typename T2 = T1,
  157. bool is_poly = poly_int_traits<T1>::is_poly>
  158. struct if_nonpoly {};
  159. template<typename T1, typename T2>
  160. struct if_nonpoly<T1, T2, false>
  161. {
  162. typedef T2 type;
  163. };
  164. /* SFINAE class that makes T3 available as "type" if both T1 and T2 are
  165. non-polynomial types. */
  166. template<typename T1, typename T2, typename T3,
  167. bool is_poly1 = poly_int_traits<T1>::is_poly,
  168. bool is_poly2 = poly_int_traits<T2>::is_poly>
  169. struct if_nonpoly2 {};
  170. template<typename T1, typename T2, typename T3>
  171. struct if_nonpoly2<T1, T2, T3, false, false>
  172. {
  173. typedef T3 type;
  174. };
  175. /* SFINAE class that makes T2 available as "type" if T1 is a polynomial
  176. type. */
  177. template<typename T1, typename T2 = T1,
  178. bool is_poly = poly_int_traits<T1>::is_poly>
  179. struct if_poly {};
  180. template<typename T1, typename T2>
  181. struct if_poly<T1, T2, true>
  182. {
  183. typedef T2 type;
  184. };
  185. /* poly_result<T1, T2> describes the result of an operation on two
  186. types T1 and T2, where at least one of the types is polynomial:
  187. - poly_result<T1, T2>::type gives the result type for the operation.
  188. The intention is to provide normal C-like rules for integer ranks,
  189. except that everything smaller than HOST_WIDE_INT promotes to
  190. HOST_WIDE_INT.
  191. - poly_result<T1, T2>::cast is the type to which an operand of type
  192. T1 should be cast before doing the operation, to ensure that
  193. the operation is done at the right precision. Casting to
  194. poly_result<T1, T2>::type would also work, but casting to this
  195. type is more efficient. */
  196. template<typename T1, typename T2 = T1,
  197. int result_kind = poly_coeff_pair_traits<T1, T2>::result_kind>
  198. struct poly_result;
  199. /* Promote pair to HOST_WIDE_INT. */
  200. template<typename T1, typename T2>
  201. struct poly_result<T1, T2, 0>
  202. {
  203. typedef HOST_WIDE_INT type;
  204. /* T1 and T2 are primitive types, so cast values to T before operating
  205. on them. */
  206. typedef type cast;
  207. };
  208. /* Promote pair to unsigned HOST_WIDE_INT. */
  209. template<typename T1, typename T2>
  210. struct poly_result<T1, T2, 1>
  211. {
  212. typedef unsigned HOST_WIDE_INT type;
  213. /* T1 and T2 are primitive types, so cast values to T before operating
  214. on them. */
  215. typedef type cast;
  216. };
  217. /* Use normal wide-int rules. */
  218. template<typename T1, typename T2>
  219. struct poly_result<T1, T2, 2>
  220. {
  221. typedef WI_BINARY_RESULT (T1, T2) type;
  222. /* Don't cast values before operating on them; leave the wi:: routines
  223. to handle promotion as necessary. */
  224. typedef const T1 &cast;
  225. };
  226. /* The coefficient type for the result of a binary operation on two
  227. poly_ints, the first of which has coefficients of type C1 and the
  228. second of which has coefficients of type C2. */
  229. #define POLY_POLY_COEFF(C1, C2) typename poly_result<C1, C2>::type
  230. /* Enforce that T2 is non-polynomial and provide the cofficient type of
  231. the result of a binary operation in which the first operand is a
  232. poly_int with coefficients of type C1 and the second operand is
  233. a constant of type T2. */
  234. #define POLY_CONST_COEFF(C1, T2) \
  235. POLY_POLY_COEFF (C1, typename if_nonpoly<T2>::type)
  236. /* Likewise in reverse. */
  237. #define CONST_POLY_COEFF(T1, C2) \
  238. POLY_POLY_COEFF (typename if_nonpoly<T1>::type, C2)
  239. /* The result type for a binary operation on poly_int<N, C1> and
  240. poly_int<N, C2>. */
  241. #define POLY_POLY_RESULT(N, C1, C2) poly_int<N, POLY_POLY_COEFF (C1, C2)>
  242. /* Enforce that T2 is non-polynomial and provide the result type
  243. for a binary operation on poly_int<N, C1> and T2. */
  244. #define POLY_CONST_RESULT(N, C1, T2) poly_int<N, POLY_CONST_COEFF (C1, T2)>
  245. /* Enforce that T1 is non-polynomial and provide the result type
  246. for a binary operation on T1 and poly_int<N, C2>. */
  247. #define CONST_POLY_RESULT(N, T1, C2) poly_int<N, CONST_POLY_COEFF (T1, C2)>
  248. /* Enforce that T1 and T2 are non-polynomial and provide the result type
  249. for a binary operation on T1 and T2. */
  250. #define CONST_CONST_RESULT(N, T1, T2) \
  251. POLY_POLY_COEFF (typename if_nonpoly<T1>::type, \
  252. typename if_nonpoly<T2>::type)
  253. /* The type to which a coefficient of type C1 should be cast before
  254. using it in a binary operation with a coefficient of type C2. */
  255. #define POLY_CAST(C1, C2) typename poly_result<C1, C2>::cast
  256. /* Provide the coefficient type for the result of T1 op T2, where T1
  257. and T2 can be polynomial or non-polynomial. */
  258. #define POLY_BINARY_COEFF(T1, T2) \
  259. typename poly_result<typename poly_int_traits<T1>::coeff_type, \
  260. typename poly_int_traits<T2>::coeff_type>::type
  261. /* The type to which an integer constant should be cast before
  262. comparing it with T. */
  263. #define POLY_INT_TYPE(T) typename poly_int_traits<T>::int_type
  264. /* RES is a poly_int result that has coefficients of type C and that
  265. is being built up a coefficient at a time. Set coefficient number I
  266. to VALUE in the most efficient way possible.
  267. For primitive C it is better to assign directly, since it avoids
  268. any further calls and so is more efficient when the compiler is
  269. built at -O0. But for wide-int based C it is better to construct
  270. the value in-place. This means that calls out to a wide-int.cc
  271. routine can take the address of RES rather than the address of
  272. a temporary.
  273. The dummy comparison against a null C * is just a way of checking
  274. that C gives the right type. */
  275. #define POLY_SET_COEFF(C, RES, I, VALUE) \
  276. ((void) (&(RES).coeffs[0] == (C *) 0), \
  277. wi::int_traits<C>::precision_type == wi::FLEXIBLE_PRECISION \
  278. ? (void) ((RES).coeffs[I] = VALUE) \
  279. : (void) ((RES).coeffs[I].~C (), new (&(RES).coeffs[I]) C (VALUE)))
  280. /* A base POD class for polynomial integers. The polynomial has N
  281. coefficients of type C. */
  282. template<unsigned int N, typename C>
  283. struct poly_int_pod
  284. {
  285. public:
  286. template<typename Ca>
  287. poly_int_pod &operator = (const poly_int_pod<N, Ca> &);
  288. template<typename Ca>
  289. typename if_nonpoly<Ca, poly_int_pod>::type &operator = (const Ca &);
  290. template<typename Ca>
  291. poly_int_pod &operator += (const poly_int_pod<N, Ca> &);
  292. template<typename Ca>
  293. typename if_nonpoly<Ca, poly_int_pod>::type &operator += (const Ca &);
  294. template<typename Ca>
  295. poly_int_pod &operator -= (const poly_int_pod<N, Ca> &);
  296. template<typename Ca>
  297. typename if_nonpoly<Ca, poly_int_pod>::type &operator -= (const Ca &);
  298. template<typename Ca>
  299. typename if_nonpoly<Ca, poly_int_pod>::type &operator *= (const Ca &);
  300. poly_int_pod &operator <<= (unsigned int);
  301. bool is_constant () const;
  302. template<typename T>
  303. typename if_lossless<T, C, bool>::type is_constant (T *) const;
  304. C to_constant () const;
  305. template<typename Ca>
  306. static poly_int<N, C> from (const poly_int_pod<N, Ca> &, unsigned int,
  307. signop);
  308. template<typename Ca>
  309. static poly_int<N, C> from (const poly_int_pod<N, Ca> &, signop);
  310. bool to_shwi (poly_int_pod<N, HOST_WIDE_INT> *) const;
  311. bool to_uhwi (poly_int_pod<N, unsigned HOST_WIDE_INT> *) const;
  312. poly_int<N, HOST_WIDE_INT> force_shwi () const;
  313. poly_int<N, unsigned HOST_WIDE_INT> force_uhwi () const;
  314. #if POLY_INT_CONVERSION
  315. operator C () const;
  316. #endif
  317. C coeffs[N];
  318. };
  319. template<unsigned int N, typename C>
  320. template<typename Ca>
  321. inline poly_int_pod<N, C>&
  322. poly_int_pod<N, C>::operator = (const poly_int_pod<N, Ca> &a)
  323. {
  324. for (unsigned int i = 0; i < N; i++)
  325. POLY_SET_COEFF (C, *this, i, a.coeffs[i]);
  326. return *this;
  327. }
  328. template<unsigned int N, typename C>
  329. template<typename Ca>
  330. inline typename if_nonpoly<Ca, poly_int_pod<N, C> >::type &
  331. poly_int_pod<N, C>::operator = (const Ca &a)
  332. {
  333. POLY_SET_COEFF (C, *this, 0, a);
  334. if (N >= 2)
  335. for (unsigned int i = 1; i < N; i++)
  336. POLY_SET_COEFF (C, *this, i, wi::ints_for<C>::zero (this->coeffs[0]));
  337. return *this;
  338. }
  339. template<unsigned int N, typename C>
  340. template<typename Ca>
  341. inline poly_int_pod<N, C>&
  342. poly_int_pod<N, C>::operator += (const poly_int_pod<N, Ca> &a)
  343. {
  344. for (unsigned int i = 0; i < N; i++)
  345. this->coeffs[i] += a.coeffs[i];
  346. return *this;
  347. }
  348. template<unsigned int N, typename C>
  349. template<typename Ca>
  350. inline typename if_nonpoly<Ca, poly_int_pod<N, C> >::type &
  351. poly_int_pod<N, C>::operator += (const Ca &a)
  352. {
  353. this->coeffs[0] += a;
  354. return *this;
  355. }
  356. template<unsigned int N, typename C>
  357. template<typename Ca>
  358. inline poly_int_pod<N, C>&
  359. poly_int_pod<N, C>::operator -= (const poly_int_pod<N, Ca> &a)
  360. {
  361. for (unsigned int i = 0; i < N; i++)
  362. this->coeffs[i] -= a.coeffs[i];
  363. return *this;
  364. }
  365. template<unsigned int N, typename C>
  366. template<typename Ca>
  367. inline typename if_nonpoly<Ca, poly_int_pod<N, C> >::type &
  368. poly_int_pod<N, C>::operator -= (const Ca &a)
  369. {
  370. this->coeffs[0] -= a;
  371. return *this;
  372. }
  373. template<unsigned int N, typename C>
  374. template<typename Ca>
  375. inline typename if_nonpoly<Ca, poly_int_pod<N, C> >::type &
  376. poly_int_pod<N, C>::operator *= (const Ca &a)
  377. {
  378. for (unsigned int i = 0; i < N; i++)
  379. this->coeffs[i] *= a;
  380. return *this;
  381. }
  382. template<unsigned int N, typename C>
  383. inline poly_int_pod<N, C>&
  384. poly_int_pod<N, C>::operator <<= (unsigned int a)
  385. {
  386. for (unsigned int i = 0; i < N; i++)
  387. this->coeffs[i] <<= a;
  388. return *this;
  389. }
  390. /* Return true if the polynomial value is a compile-time constant. */
  391. template<unsigned int N, typename C>
  392. inline bool
  393. poly_int_pod<N, C>::is_constant () const
  394. {
  395. if (N >= 2)
  396. for (unsigned int i = 1; i < N; i++)
  397. if (this->coeffs[i] != 0)
  398. return false;
  399. return true;
  400. }
  401. /* Return true if the polynomial value is a compile-time constant,
  402. storing its value in CONST_VALUE if so. */
  403. template<unsigned int N, typename C>
  404. template<typename T>
  405. inline typename if_lossless<T, C, bool>::type
  406. poly_int_pod<N, C>::is_constant (T *const_value) const
  407. {
  408. if (is_constant ())
  409. {
  410. *const_value = this->coeffs[0];
  411. return true;
  412. }
  413. return false;
  414. }
  415. /* Return the value of a polynomial that is already known to be a
  416. compile-time constant.
  417. NOTE: When using this function, please add a comment above the call
  418. explaining why we know the value is constant in that context. */
  419. template<unsigned int N, typename C>
  420. inline C
  421. poly_int_pod<N, C>::to_constant () const
  422. {
  423. gcc_checking_assert (is_constant ());
  424. return this->coeffs[0];
  425. }
  426. /* Convert X to a wide_int-based polynomial in which each coefficient
  427. has BITSIZE bits. If X's coefficients are smaller than BITSIZE,
  428. extend them according to SGN. */
  429. template<unsigned int N, typename C>
  430. template<typename Ca>
  431. inline poly_int<N, C>
  432. poly_int_pod<N, C>::from (const poly_int_pod<N, Ca> &a,
  433. unsigned int bitsize, signop sgn)
  434. {
  435. poly_int<N, C> r;
  436. for (unsigned int i = 0; i < N; i++)
  437. POLY_SET_COEFF (C, r, i, C::from (a.coeffs[i], bitsize, sgn));
  438. return r;
  439. }
  440. /* Convert X to a fixed_wide_int-based polynomial, extending according
  441. to SGN. */
  442. template<unsigned int N, typename C>
  443. template<typename Ca>
  444. inline poly_int<N, C>
  445. poly_int_pod<N, C>::from (const poly_int_pod<N, Ca> &a, signop sgn)
  446. {
  447. poly_int<N, C> r;
  448. for (unsigned int i = 0; i < N; i++)
  449. POLY_SET_COEFF (C, r, i, C::from (a.coeffs[i], sgn));
  450. return r;
  451. }
  452. /* Return true if the coefficients of this generic_wide_int-based
  453. polynomial can be represented as signed HOST_WIDE_INTs without loss
  454. of precision. Store the HOST_WIDE_INT representation in *R if so. */
  455. template<unsigned int N, typename C>
  456. inline bool
  457. poly_int_pod<N, C>::to_shwi (poly_int_pod<N, HOST_WIDE_INT> *r) const
  458. {
  459. for (unsigned int i = 0; i < N; i++)
  460. if (!wi::fits_shwi_p (this->coeffs[i]))
  461. return false;
  462. for (unsigned int i = 0; i < N; i++)
  463. r->coeffs[i] = this->coeffs[i].to_shwi ();
  464. return true;
  465. }
  466. /* Return true if the coefficients of this generic_wide_int-based
  467. polynomial can be represented as unsigned HOST_WIDE_INTs without
  468. loss of precision. Store the unsigned HOST_WIDE_INT representation
  469. in *R if so. */
  470. template<unsigned int N, typename C>
  471. inline bool
  472. poly_int_pod<N, C>::to_uhwi (poly_int_pod<N, unsigned HOST_WIDE_INT> *r) const
  473. {
  474. for (unsigned int i = 0; i < N; i++)
  475. if (!wi::fits_uhwi_p (this->coeffs[i]))
  476. return false;
  477. for (unsigned int i = 0; i < N; i++)
  478. r->coeffs[i] = this->coeffs[i].to_uhwi ();
  479. return true;
  480. }
  481. /* Force a generic_wide_int-based constant to HOST_WIDE_INT precision,
  482. truncating if necessary. */
  483. template<unsigned int N, typename C>
  484. inline poly_int<N, HOST_WIDE_INT>
  485. poly_int_pod<N, C>::force_shwi () const
  486. {
  487. poly_int_pod<N, HOST_WIDE_INT> r;
  488. for (unsigned int i = 0; i < N; i++)
  489. r.coeffs[i] = this->coeffs[i].to_shwi ();
  490. return r;
  491. }
  492. /* Force a generic_wide_int-based constant to unsigned HOST_WIDE_INT precision,
  493. truncating if necessary. */
  494. template<unsigned int N, typename C>
  495. inline poly_int<N, unsigned HOST_WIDE_INT>
  496. poly_int_pod<N, C>::force_uhwi () const
  497. {
  498. poly_int_pod<N, unsigned HOST_WIDE_INT> r;
  499. for (unsigned int i = 0; i < N; i++)
  500. r.coeffs[i] = this->coeffs[i].to_uhwi ();
  501. return r;
  502. }
  503. #if POLY_INT_CONVERSION
  504. /* Provide a conversion operator to constants. */
  505. template<unsigned int N, typename C>
  506. inline
  507. poly_int_pod<N, C>::operator C () const
  508. {
  509. gcc_checking_assert (this->is_constant ());
  510. return this->coeffs[0];
  511. }
  512. #endif
  513. /* The main class for polynomial integers. The class provides
  514. constructors that are necessarily missing from the POD base. */
  515. template<unsigned int N, typename C>
  516. class poly_int : public poly_int_pod<N, C>
  517. {
  518. public:
  519. poly_int () {}
  520. template<typename Ca>
  521. poly_int (const poly_int<N, Ca> &);
  522. template<typename Ca>
  523. poly_int (const poly_int_pod<N, Ca> &);
  524. template<typename C0>
  525. poly_int (const C0 &);
  526. template<typename C0, typename C1>
  527. poly_int (const C0 &, const C1 &);
  528. template<typename Ca>
  529. poly_int &operator = (const poly_int_pod<N, Ca> &);
  530. template<typename Ca>
  531. typename if_nonpoly<Ca, poly_int>::type &operator = (const Ca &);
  532. template<typename Ca>
  533. poly_int &operator += (const poly_int_pod<N, Ca> &);
  534. template<typename Ca>
  535. typename if_nonpoly<Ca, poly_int>::type &operator += (const Ca &);
  536. template<typename Ca>
  537. poly_int &operator -= (const poly_int_pod<N, Ca> &);
  538. template<typename Ca>
  539. typename if_nonpoly<Ca, poly_int>::type &operator -= (const Ca &);
  540. template<typename Ca>
  541. typename if_nonpoly<Ca, poly_int>::type &operator *= (const Ca &);
  542. poly_int &operator <<= (unsigned int);
  543. };
  544. template<unsigned int N, typename C>
  545. template<typename Ca>
  546. inline
  547. poly_int<N, C>::poly_int (const poly_int<N, Ca> &a)
  548. {
  549. for (unsigned int i = 0; i < N; i++)
  550. POLY_SET_COEFF (C, *this, i, a.coeffs[i]);
  551. }
  552. template<unsigned int N, typename C>
  553. template<typename Ca>
  554. inline
  555. poly_int<N, C>::poly_int (const poly_int_pod<N, Ca> &a)
  556. {
  557. for (unsigned int i = 0; i < N; i++)
  558. POLY_SET_COEFF (C, *this, i, a.coeffs[i]);
  559. }
  560. template<unsigned int N, typename C>
  561. template<typename C0>
  562. inline
  563. poly_int<N, C>::poly_int (const C0 &c0)
  564. {
  565. POLY_SET_COEFF (C, *this, 0, c0);
  566. for (unsigned int i = 1; i < N; i++)
  567. POLY_SET_COEFF (C, *this, i, wi::ints_for<C>::zero (this->coeffs[0]));
  568. }
  569. template<unsigned int N, typename C>
  570. template<typename C0, typename C1>
  571. inline
  572. poly_int<N, C>::poly_int (const C0 &c0, const C1 &c1)
  573. {
  574. STATIC_ASSERT (N >= 2);
  575. POLY_SET_COEFF (C, *this, 0, c0);
  576. POLY_SET_COEFF (C, *this, 1, c1);
  577. for (unsigned int i = 2; i < N; i++)
  578. POLY_SET_COEFF (C, *this, i, wi::ints_for<C>::zero (this->coeffs[0]));
  579. }
  580. template<unsigned int N, typename C>
  581. template<typename Ca>
  582. inline poly_int<N, C>&
  583. poly_int<N, C>::operator = (const poly_int_pod<N, Ca> &a)
  584. {
  585. for (unsigned int i = 0; i < N; i++)
  586. this->coeffs[i] = a.coeffs[i];
  587. return *this;
  588. }
  589. template<unsigned int N, typename C>
  590. template<typename Ca>
  591. inline typename if_nonpoly<Ca, poly_int<N, C> >::type &
  592. poly_int<N, C>::operator = (const Ca &a)
  593. {
  594. this->coeffs[0] = a;
  595. if (N >= 2)
  596. for (unsigned int i = 1; i < N; i++)
  597. this->coeffs[i] = wi::ints_for<C>::zero (this->coeffs[0]);
  598. return *this;
  599. }
  600. template<unsigned int N, typename C>
  601. template<typename Ca>
  602. inline poly_int<N, C>&
  603. poly_int<N, C>::operator += (const poly_int_pod<N, Ca> &a)
  604. {
  605. for (unsigned int i = 0; i < N; i++)
  606. this->coeffs[i] += a.coeffs[i];
  607. return *this;
  608. }
  609. template<unsigned int N, typename C>
  610. template<typename Ca>
  611. inline typename if_nonpoly<Ca, poly_int<N, C> >::type &
  612. poly_int<N, C>::operator += (const Ca &a)
  613. {
  614. this->coeffs[0] += a;
  615. return *this;
  616. }
  617. template<unsigned int N, typename C>
  618. template<typename Ca>
  619. inline poly_int<N, C>&
  620. poly_int<N, C>::operator -= (const poly_int_pod<N, Ca> &a)
  621. {
  622. for (unsigned int i = 0; i < N; i++)
  623. this->coeffs[i] -= a.coeffs[i];
  624. return *this;
  625. }
  626. template<unsigned int N, typename C>
  627. template<typename Ca>
  628. inline typename if_nonpoly<Ca, poly_int<N, C> >::type &
  629. poly_int<N, C>::operator -= (const Ca &a)
  630. {
  631. this->coeffs[0] -= a;
  632. return *this;
  633. }
  634. template<unsigned int N, typename C>
  635. template<typename Ca>
  636. inline typename if_nonpoly<Ca, poly_int<N, C> >::type &
  637. poly_int<N, C>::operator *= (const Ca &a)
  638. {
  639. for (unsigned int i = 0; i < N; i++)
  640. this->coeffs[i] *= a;
  641. return *this;
  642. }
  643. template<unsigned int N, typename C>
  644. inline poly_int<N, C>&
  645. poly_int<N, C>::operator <<= (unsigned int a)
  646. {
  647. for (unsigned int i = 0; i < N; i++)
  648. this->coeffs[i] <<= a;
  649. return *this;
  650. }
  651. /* Return true if every coefficient of A is in the inclusive range [B, C]. */
  652. template<typename Ca, typename Cb, typename Cc>
  653. inline typename if_nonpoly<Ca, bool>::type
  654. coeffs_in_range_p (const Ca &a, const Cb &b, const Cc &c)
  655. {
  656. return a >= b && a <= c;
  657. }
  658. template<unsigned int N, typename Ca, typename Cb, typename Cc>
  659. inline typename if_nonpoly<Ca, bool>::type
  660. coeffs_in_range_p (const poly_int_pod<N, Ca> &a, const Cb &b, const Cc &c)
  661. {
  662. for (unsigned int i = 0; i < N; i++)
  663. if (a.coeffs[i] < b || a.coeffs[i] > c)
  664. return false;
  665. return true;
  666. }
  667. namespace wi {
  668. /* Poly version of wi::shwi, with the same interface. */
  669. template<unsigned int N>
  670. inline poly_int<N, hwi_with_prec>
  671. shwi (const poly_int_pod<N, HOST_WIDE_INT> &a, unsigned int precision)
  672. {
  673. poly_int<N, hwi_with_prec> r;
  674. for (unsigned int i = 0; i < N; i++)
  675. POLY_SET_COEFF (hwi_with_prec, r, i, wi::shwi (a.coeffs[i], precision));
  676. return r;
  677. }
  678. /* Poly version of wi::uhwi, with the same interface. */
  679. template<unsigned int N>
  680. inline poly_int<N, hwi_with_prec>
  681. uhwi (const poly_int_pod<N, unsigned HOST_WIDE_INT> &a, unsigned int precision)
  682. {
  683. poly_int<N, hwi_with_prec> r;
  684. for (unsigned int i = 0; i < N; i++)
  685. POLY_SET_COEFF (hwi_with_prec, r, i, wi::uhwi (a.coeffs[i], precision));
  686. return r;
  687. }
  688. /* Poly version of wi::sext, with the same interface. */
  689. template<unsigned int N, typename Ca>
  690. inline POLY_POLY_RESULT (N, Ca, Ca)
  691. sext (const poly_int_pod<N, Ca> &a, unsigned int precision)
  692. {
  693. typedef POLY_POLY_COEFF (Ca, Ca) C;
  694. poly_int<N, C> r;
  695. for (unsigned int i = 0; i < N; i++)
  696. POLY_SET_COEFF (C, r, i, wi::sext (a.coeffs[i], precision));
  697. return r;
  698. }
  699. /* Poly version of wi::zext, with the same interface. */
  700. template<unsigned int N, typename Ca>
  701. inline POLY_POLY_RESULT (N, Ca, Ca)
  702. zext (const poly_int_pod<N, Ca> &a, unsigned int precision)
  703. {
  704. typedef POLY_POLY_COEFF (Ca, Ca) C;
  705. poly_int<N, C> r;
  706. for (unsigned int i = 0; i < N; i++)
  707. POLY_SET_COEFF (C, r, i, wi::zext (a.coeffs[i], precision));
  708. return r;
  709. }
  710. }
  711. template<unsigned int N, typename Ca, typename Cb>
  712. inline POLY_POLY_RESULT (N, Ca, Cb)
  713. operator + (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  714. {
  715. typedef POLY_CAST (Ca, Cb) NCa;
  716. typedef POLY_POLY_COEFF (Ca, Cb) C;
  717. poly_int<N, C> r;
  718. for (unsigned int i = 0; i < N; i++)
  719. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]) + b.coeffs[i]);
  720. return r;
  721. }
  722. template<unsigned int N, typename Ca, typename Cb>
  723. inline POLY_CONST_RESULT (N, Ca, Cb)
  724. operator + (const poly_int_pod<N, Ca> &a, const Cb &b)
  725. {
  726. typedef POLY_CAST (Ca, Cb) NCa;
  727. typedef POLY_CONST_COEFF (Ca, Cb) C;
  728. poly_int<N, C> r;
  729. POLY_SET_COEFF (C, r, 0, NCa (a.coeffs[0]) + b);
  730. if (N >= 2)
  731. for (unsigned int i = 1; i < N; i++)
  732. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]));
  733. return r;
  734. }
  735. template<unsigned int N, typename Ca, typename Cb>
  736. inline CONST_POLY_RESULT (N, Ca, Cb)
  737. operator + (const Ca &a, const poly_int_pod<N, Cb> &b)
  738. {
  739. typedef POLY_CAST (Cb, Ca) NCb;
  740. typedef CONST_POLY_COEFF (Ca, Cb) C;
  741. poly_int<N, C> r;
  742. POLY_SET_COEFF (C, r, 0, a + NCb (b.coeffs[0]));
  743. if (N >= 2)
  744. for (unsigned int i = 1; i < N; i++)
  745. POLY_SET_COEFF (C, r, i, NCb (b.coeffs[i]));
  746. return r;
  747. }
  748. namespace wi {
  749. /* Poly versions of wi::add, with the same interface. */
  750. template<unsigned int N, typename Ca, typename Cb>
  751. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  752. add (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  753. {
  754. typedef WI_BINARY_RESULT (Ca, Cb) C;
  755. poly_int<N, C> r;
  756. for (unsigned int i = 0; i < N; i++)
  757. POLY_SET_COEFF (C, r, i, wi::add (a.coeffs[i], b.coeffs[i]));
  758. return r;
  759. }
  760. template<unsigned int N, typename Ca, typename Cb>
  761. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  762. add (const poly_int_pod<N, Ca> &a, const Cb &b)
  763. {
  764. typedef WI_BINARY_RESULT (Ca, Cb) C;
  765. poly_int<N, C> r;
  766. POLY_SET_COEFF (C, r, 0, wi::add (a.coeffs[0], b));
  767. for (unsigned int i = 1; i < N; i++)
  768. POLY_SET_COEFF (C, r, i, wi::add (a.coeffs[i],
  769. wi::ints_for<Cb>::zero (b)));
  770. return r;
  771. }
  772. template<unsigned int N, typename Ca, typename Cb>
  773. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  774. add (const Ca &a, const poly_int_pod<N, Cb> &b)
  775. {
  776. typedef WI_BINARY_RESULT (Ca, Cb) C;
  777. poly_int<N, C> r;
  778. POLY_SET_COEFF (C, r, 0, wi::add (a, b.coeffs[0]));
  779. for (unsigned int i = 1; i < N; i++)
  780. POLY_SET_COEFF (C, r, i, wi::add (wi::ints_for<Ca>::zero (a),
  781. b.coeffs[i]));
  782. return r;
  783. }
  784. template<unsigned int N, typename Ca, typename Cb>
  785. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  786. add (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
  787. signop sgn, wi::overflow_type *overflow)
  788. {
  789. typedef WI_BINARY_RESULT (Ca, Cb) C;
  790. poly_int<N, C> r;
  791. POLY_SET_COEFF (C, r, 0, wi::add (a.coeffs[0], b.coeffs[0], sgn, overflow));
  792. for (unsigned int i = 1; i < N; i++)
  793. {
  794. wi::overflow_type suboverflow;
  795. POLY_SET_COEFF (C, r, i, wi::add (a.coeffs[i], b.coeffs[i], sgn,
  796. &suboverflow));
  797. wi::accumulate_overflow (*overflow, suboverflow);
  798. }
  799. return r;
  800. }
  801. }
  802. template<unsigned int N, typename Ca, typename Cb>
  803. inline POLY_POLY_RESULT (N, Ca, Cb)
  804. operator - (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  805. {
  806. typedef POLY_CAST (Ca, Cb) NCa;
  807. typedef POLY_POLY_COEFF (Ca, Cb) C;
  808. poly_int<N, C> r;
  809. for (unsigned int i = 0; i < N; i++)
  810. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]) - b.coeffs[i]);
  811. return r;
  812. }
  813. template<unsigned int N, typename Ca, typename Cb>
  814. inline POLY_CONST_RESULT (N, Ca, Cb)
  815. operator - (const poly_int_pod<N, Ca> &a, const Cb &b)
  816. {
  817. typedef POLY_CAST (Ca, Cb) NCa;
  818. typedef POLY_CONST_COEFF (Ca, Cb) C;
  819. poly_int<N, C> r;
  820. POLY_SET_COEFF (C, r, 0, NCa (a.coeffs[0]) - b);
  821. if (N >= 2)
  822. for (unsigned int i = 1; i < N; i++)
  823. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]));
  824. return r;
  825. }
  826. template<unsigned int N, typename Ca, typename Cb>
  827. inline CONST_POLY_RESULT (N, Ca, Cb)
  828. operator - (const Ca &a, const poly_int_pod<N, Cb> &b)
  829. {
  830. typedef POLY_CAST (Cb, Ca) NCb;
  831. typedef CONST_POLY_COEFF (Ca, Cb) C;
  832. poly_int<N, C> r;
  833. POLY_SET_COEFF (C, r, 0, a - NCb (b.coeffs[0]));
  834. if (N >= 2)
  835. for (unsigned int i = 1; i < N; i++)
  836. POLY_SET_COEFF (C, r, i, -NCb (b.coeffs[i]));
  837. return r;
  838. }
  839. namespace wi {
  840. /* Poly versions of wi::sub, with the same interface. */
  841. template<unsigned int N, typename Ca, typename Cb>
  842. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  843. sub (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  844. {
  845. typedef WI_BINARY_RESULT (Ca, Cb) C;
  846. poly_int<N, C> r;
  847. for (unsigned int i = 0; i < N; i++)
  848. POLY_SET_COEFF (C, r, i, wi::sub (a.coeffs[i], b.coeffs[i]));
  849. return r;
  850. }
  851. template<unsigned int N, typename Ca, typename Cb>
  852. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  853. sub (const poly_int_pod<N, Ca> &a, const Cb &b)
  854. {
  855. typedef WI_BINARY_RESULT (Ca, Cb) C;
  856. poly_int<N, C> r;
  857. POLY_SET_COEFF (C, r, 0, wi::sub (a.coeffs[0], b));
  858. for (unsigned int i = 1; i < N; i++)
  859. POLY_SET_COEFF (C, r, i, wi::sub (a.coeffs[i],
  860. wi::ints_for<Cb>::zero (b)));
  861. return r;
  862. }
  863. template<unsigned int N, typename Ca, typename Cb>
  864. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  865. sub (const Ca &a, const poly_int_pod<N, Cb> &b)
  866. {
  867. typedef WI_BINARY_RESULT (Ca, Cb) C;
  868. poly_int<N, C> r;
  869. POLY_SET_COEFF (C, r, 0, wi::sub (a, b.coeffs[0]));
  870. for (unsigned int i = 1; i < N; i++)
  871. POLY_SET_COEFF (C, r, i, wi::sub (wi::ints_for<Ca>::zero (a),
  872. b.coeffs[i]));
  873. return r;
  874. }
  875. template<unsigned int N, typename Ca, typename Cb>
  876. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  877. sub (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
  878. signop sgn, wi::overflow_type *overflow)
  879. {
  880. typedef WI_BINARY_RESULT (Ca, Cb) C;
  881. poly_int<N, C> r;
  882. POLY_SET_COEFF (C, r, 0, wi::sub (a.coeffs[0], b.coeffs[0], sgn, overflow));
  883. for (unsigned int i = 1; i < N; i++)
  884. {
  885. wi::overflow_type suboverflow;
  886. POLY_SET_COEFF (C, r, i, wi::sub (a.coeffs[i], b.coeffs[i], sgn,
  887. &suboverflow));
  888. wi::accumulate_overflow (*overflow, suboverflow);
  889. }
  890. return r;
  891. }
  892. }
  893. template<unsigned int N, typename Ca>
  894. inline POLY_POLY_RESULT (N, Ca, Ca)
  895. operator - (const poly_int_pod<N, Ca> &a)
  896. {
  897. typedef POLY_CAST (Ca, Ca) NCa;
  898. typedef POLY_POLY_COEFF (Ca, Ca) C;
  899. poly_int<N, C> r;
  900. for (unsigned int i = 0; i < N; i++)
  901. POLY_SET_COEFF (C, r, i, -NCa (a.coeffs[i]));
  902. return r;
  903. }
  904. namespace wi {
  905. /* Poly version of wi::neg, with the same interface. */
  906. template<unsigned int N, typename Ca>
  907. inline poly_int<N, WI_UNARY_RESULT (Ca)>
  908. neg (const poly_int_pod<N, Ca> &a)
  909. {
  910. typedef WI_UNARY_RESULT (Ca) C;
  911. poly_int<N, C> r;
  912. for (unsigned int i = 0; i < N; i++)
  913. POLY_SET_COEFF (C, r, i, wi::neg (a.coeffs[i]));
  914. return r;
  915. }
  916. template<unsigned int N, typename Ca>
  917. inline poly_int<N, WI_UNARY_RESULT (Ca)>
  918. neg (const poly_int_pod<N, Ca> &a, wi::overflow_type *overflow)
  919. {
  920. typedef WI_UNARY_RESULT (Ca) C;
  921. poly_int<N, C> r;
  922. POLY_SET_COEFF (C, r, 0, wi::neg (a.coeffs[0], overflow));
  923. for (unsigned int i = 1; i < N; i++)
  924. {
  925. wi::overflow_type suboverflow;
  926. POLY_SET_COEFF (C, r, i, wi::neg (a.coeffs[i], &suboverflow));
  927. wi::accumulate_overflow (*overflow, suboverflow);
  928. }
  929. return r;
  930. }
  931. }
  932. template<unsigned int N, typename Ca>
  933. inline POLY_POLY_RESULT (N, Ca, Ca)
  934. operator ~ (const poly_int_pod<N, Ca> &a)
  935. {
  936. if (N >= 2)
  937. return -1 - a;
  938. return ~a.coeffs[0];
  939. }
  940. template<unsigned int N, typename Ca, typename Cb>
  941. inline POLY_CONST_RESULT (N, Ca, Cb)
  942. operator * (const poly_int_pod<N, Ca> &a, const Cb &b)
  943. {
  944. typedef POLY_CAST (Ca, Cb) NCa;
  945. typedef POLY_CONST_COEFF (Ca, Cb) C;
  946. poly_int<N, C> r;
  947. for (unsigned int i = 0; i < N; i++)
  948. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]) * b);
  949. return r;
  950. }
  951. template<unsigned int N, typename Ca, typename Cb>
  952. inline CONST_POLY_RESULT (N, Ca, Cb)
  953. operator * (const Ca &a, const poly_int_pod<N, Cb> &b)
  954. {
  955. typedef POLY_CAST (Ca, Cb) NCa;
  956. typedef CONST_POLY_COEFF (Ca, Cb) C;
  957. poly_int<N, C> r;
  958. for (unsigned int i = 0; i < N; i++)
  959. POLY_SET_COEFF (C, r, i, NCa (a) * b.coeffs[i]);
  960. return r;
  961. }
  962. namespace wi {
  963. /* Poly versions of wi::mul, with the same interface. */
  964. template<unsigned int N, typename Ca, typename Cb>
  965. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  966. mul (const poly_int_pod<N, Ca> &a, const Cb &b)
  967. {
  968. typedef WI_BINARY_RESULT (Ca, Cb) C;
  969. poly_int<N, C> r;
  970. for (unsigned int i = 0; i < N; i++)
  971. POLY_SET_COEFF (C, r, i, wi::mul (a.coeffs[i], b));
  972. return r;
  973. }
  974. template<unsigned int N, typename Ca, typename Cb>
  975. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  976. mul (const Ca &a, const poly_int_pod<N, Cb> &b)
  977. {
  978. typedef WI_BINARY_RESULT (Ca, Cb) C;
  979. poly_int<N, C> r;
  980. for (unsigned int i = 0; i < N; i++)
  981. POLY_SET_COEFF (C, r, i, wi::mul (a, b.coeffs[i]));
  982. return r;
  983. }
  984. template<unsigned int N, typename Ca, typename Cb>
  985. inline poly_int<N, WI_BINARY_RESULT (Ca, Cb)>
  986. mul (const poly_int_pod<N, Ca> &a, const Cb &b,
  987. signop sgn, wi::overflow_type *overflow)
  988. {
  989. typedef WI_BINARY_RESULT (Ca, Cb) C;
  990. poly_int<N, C> r;
  991. POLY_SET_COEFF (C, r, 0, wi::mul (a.coeffs[0], b, sgn, overflow));
  992. for (unsigned int i = 1; i < N; i++)
  993. {
  994. wi::overflow_type suboverflow;
  995. POLY_SET_COEFF (C, r, i, wi::mul (a.coeffs[i], b, sgn, &suboverflow));
  996. wi::accumulate_overflow (*overflow, suboverflow);
  997. }
  998. return r;
  999. }
  1000. }
  1001. template<unsigned int N, typename Ca, typename Cb>
  1002. inline POLY_POLY_RESULT (N, Ca, Ca)
  1003. operator << (const poly_int_pod<N, Ca> &a, const Cb &b)
  1004. {
  1005. typedef POLY_CAST (Ca, Ca) NCa;
  1006. typedef POLY_POLY_COEFF (Ca, Ca) C;
  1007. poly_int<N, C> r;
  1008. for (unsigned int i = 0; i < N; i++)
  1009. POLY_SET_COEFF (C, r, i, NCa (a.coeffs[i]) << b);
  1010. return r;
  1011. }
  1012. namespace wi {
  1013. /* Poly version of wi::lshift, with the same interface. */
  1014. template<unsigned int N, typename Ca, typename Cb>
  1015. inline poly_int<N, WI_BINARY_RESULT (Ca, Ca)>
  1016. lshift (const poly_int_pod<N, Ca> &a, const Cb &b)
  1017. {
  1018. typedef WI_BINARY_RESULT (Ca, Ca) C;
  1019. poly_int<N, C> r;
  1020. for (unsigned int i = 0; i < N; i++)
  1021. POLY_SET_COEFF (C, r, i, wi::lshift (a.coeffs[i], b));
  1022. return r;
  1023. }
  1024. }
  1025. /* Return true if a0 + a1 * x might equal b0 + b1 * x for some nonnegative
  1026. integer x. */
  1027. template<typename Ca, typename Cb>
  1028. inline bool
  1029. maybe_eq_2 (const Ca &a0, const Ca &a1, const Cb &b0, const Cb &b1)
  1030. {
  1031. if (a1 != b1)
  1032. /* a0 + a1 * x == b0 + b1 * x
  1033. ==> (a1 - b1) * x == b0 - a0
  1034. ==> x == (b0 - a0) / (a1 - b1)
  1035. We need to test whether that's a valid value of x.
  1036. (b0 - a0) and (a1 - b1) must not have opposite signs
  1037. and the result must be integral. */
  1038. return (a1 < b1
  1039. ? b0 <= a0 && (a0 - b0) % (b1 - a1) == 0
  1040. : b0 >= a0 && (b0 - a0) % (a1 - b1) == 0);
  1041. return a0 == b0;
  1042. }
  1043. /* Return true if a0 + a1 * x might equal b for some nonnegative
  1044. integer x. */
  1045. template<typename Ca, typename Cb>
  1046. inline bool
  1047. maybe_eq_2 (const Ca &a0, const Ca &a1, const Cb &b)
  1048. {
  1049. if (a1 != 0)
  1050. /* a0 + a1 * x == b
  1051. ==> x == (b - a0) / a1
  1052. We need to test whether that's a valid value of x.
  1053. (b - a0) and a1 must not have opposite signs and the
  1054. result must be integral. */
  1055. return (a1 < 0
  1056. ? b <= a0 && (a0 - b) % a1 == 0
  1057. : b >= a0 && (b - a0) % a1 == 0);
  1058. return a0 == b;
  1059. }
  1060. /* Return true if A might equal B for some indeterminate values. */
  1061. template<unsigned int N, typename Ca, typename Cb>
  1062. inline bool
  1063. maybe_eq (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1064. {
  1065. STATIC_ASSERT (N <= 2);
  1066. if (N == 2)
  1067. return maybe_eq_2 (a.coeffs[0], a.coeffs[1], b.coeffs[0], b.coeffs[1]);
  1068. return a.coeffs[0] == b.coeffs[0];
  1069. }
  1070. template<unsigned int N, typename Ca, typename Cb>
  1071. inline typename if_nonpoly<Cb, bool>::type
  1072. maybe_eq (const poly_int_pod<N, Ca> &a, const Cb &b)
  1073. {
  1074. STATIC_ASSERT (N <= 2);
  1075. if (N == 2)
  1076. return maybe_eq_2 (a.coeffs[0], a.coeffs[1], b);
  1077. return a.coeffs[0] == b;
  1078. }
  1079. template<unsigned int N, typename Ca, typename Cb>
  1080. inline typename if_nonpoly<Ca, bool>::type
  1081. maybe_eq (const Ca &a, const poly_int_pod<N, Cb> &b)
  1082. {
  1083. STATIC_ASSERT (N <= 2);
  1084. if (N == 2)
  1085. return maybe_eq_2 (b.coeffs[0], b.coeffs[1], a);
  1086. return a == b.coeffs[0];
  1087. }
  1088. template<typename Ca, typename Cb>
  1089. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1090. maybe_eq (const Ca &a, const Cb &b)
  1091. {
  1092. return a == b;
  1093. }
  1094. /* Return true if A might not equal B for some indeterminate values. */
  1095. template<unsigned int N, typename Ca, typename Cb>
  1096. inline bool
  1097. maybe_ne (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1098. {
  1099. if (N >= 2)
  1100. for (unsigned int i = 1; i < N; i++)
  1101. if (a.coeffs[i] != b.coeffs[i])
  1102. return true;
  1103. return a.coeffs[0] != b.coeffs[0];
  1104. }
  1105. template<unsigned int N, typename Ca, typename Cb>
  1106. inline typename if_nonpoly<Cb, bool>::type
  1107. maybe_ne (const poly_int_pod<N, Ca> &a, const Cb &b)
  1108. {
  1109. if (N >= 2)
  1110. for (unsigned int i = 1; i < N; i++)
  1111. if (a.coeffs[i] != 0)
  1112. return true;
  1113. return a.coeffs[0] != b;
  1114. }
  1115. template<unsigned int N, typename Ca, typename Cb>
  1116. inline typename if_nonpoly<Ca, bool>::type
  1117. maybe_ne (const Ca &a, const poly_int_pod<N, Cb> &b)
  1118. {
  1119. if (N >= 2)
  1120. for (unsigned int i = 1; i < N; i++)
  1121. if (b.coeffs[i] != 0)
  1122. return true;
  1123. return a != b.coeffs[0];
  1124. }
  1125. template<typename Ca, typename Cb>
  1126. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1127. maybe_ne (const Ca &a, const Cb &b)
  1128. {
  1129. return a != b;
  1130. }
  1131. /* Return true if A is known to be equal to B. */
  1132. #define known_eq(A, B) (!maybe_ne (A, B))
  1133. /* Return true if A is known to be unequal to B. */
  1134. #define known_ne(A, B) (!maybe_eq (A, B))
  1135. /* Return true if A might be less than or equal to B for some
  1136. indeterminate values. */
  1137. template<unsigned int N, typename Ca, typename Cb>
  1138. inline bool
  1139. maybe_le (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1140. {
  1141. if (N >= 2)
  1142. for (unsigned int i = 1; i < N; i++)
  1143. if (a.coeffs[i] < b.coeffs[i])
  1144. return true;
  1145. return a.coeffs[0] <= b.coeffs[0];
  1146. }
  1147. template<unsigned int N, typename Ca, typename Cb>
  1148. inline typename if_nonpoly<Cb, bool>::type
  1149. maybe_le (const poly_int_pod<N, Ca> &a, const Cb &b)
  1150. {
  1151. if (N >= 2)
  1152. for (unsigned int i = 1; i < N; i++)
  1153. if (a.coeffs[i] < 0)
  1154. return true;
  1155. return a.coeffs[0] <= b;
  1156. }
  1157. template<unsigned int N, typename Ca, typename Cb>
  1158. inline typename if_nonpoly<Ca, bool>::type
  1159. maybe_le (const Ca &a, const poly_int_pod<N, Cb> &b)
  1160. {
  1161. if (N >= 2)
  1162. for (unsigned int i = 1; i < N; i++)
  1163. if (b.coeffs[i] > 0)
  1164. return true;
  1165. return a <= b.coeffs[0];
  1166. }
  1167. template<typename Ca, typename Cb>
  1168. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1169. maybe_le (const Ca &a, const Cb &b)
  1170. {
  1171. return a <= b;
  1172. }
  1173. /* Return true if A might be less than B for some indeterminate values. */
  1174. template<unsigned int N, typename Ca, typename Cb>
  1175. inline bool
  1176. maybe_lt (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1177. {
  1178. if (N >= 2)
  1179. for (unsigned int i = 1; i < N; i++)
  1180. if (a.coeffs[i] < b.coeffs[i])
  1181. return true;
  1182. return a.coeffs[0] < b.coeffs[0];
  1183. }
  1184. template<unsigned int N, typename Ca, typename Cb>
  1185. inline typename if_nonpoly<Cb, bool>::type
  1186. maybe_lt (const poly_int_pod<N, Ca> &a, const Cb &b)
  1187. {
  1188. if (N >= 2)
  1189. for (unsigned int i = 1; i < N; i++)
  1190. if (a.coeffs[i] < 0)
  1191. return true;
  1192. return a.coeffs[0] < b;
  1193. }
  1194. template<unsigned int N, typename Ca, typename Cb>
  1195. inline typename if_nonpoly<Ca, bool>::type
  1196. maybe_lt (const Ca &a, const poly_int_pod<N, Cb> &b)
  1197. {
  1198. if (N >= 2)
  1199. for (unsigned int i = 1; i < N; i++)
  1200. if (b.coeffs[i] > 0)
  1201. return true;
  1202. return a < b.coeffs[0];
  1203. }
  1204. template<typename Ca, typename Cb>
  1205. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1206. maybe_lt (const Ca &a, const Cb &b)
  1207. {
  1208. return a < b;
  1209. }
  1210. /* Return true if A may be greater than or equal to B. */
  1211. #define maybe_ge(A, B) maybe_le (B, A)
  1212. /* Return true if A may be greater than B. */
  1213. #define maybe_gt(A, B) maybe_lt (B, A)
  1214. /* Return true if A is known to be less than or equal to B. */
  1215. #define known_le(A, B) (!maybe_gt (A, B))
  1216. /* Return true if A is known to be less than B. */
  1217. #define known_lt(A, B) (!maybe_ge (A, B))
  1218. /* Return true if A is known to be greater than B. */
  1219. #define known_gt(A, B) (!maybe_le (A, B))
  1220. /* Return true if A is known to be greater than or equal to B. */
  1221. #define known_ge(A, B) (!maybe_lt (A, B))
  1222. /* Return true if A and B are ordered by the partial ordering known_le. */
  1223. template<typename T1, typename T2>
  1224. inline bool
  1225. ordered_p (const T1 &a, const T2 &b)
  1226. {
  1227. return ((poly_int_traits<T1>::num_coeffs == 1
  1228. && poly_int_traits<T2>::num_coeffs == 1)
  1229. || known_le (a, b)
  1230. || known_le (b, a));
  1231. }
  1232. /* Assert that A and B are known to be ordered and return the minimum
  1233. of the two.
  1234. NOTE: When using this function, please add a comment above the call
  1235. explaining why we know the values are ordered in that context. */
  1236. template<unsigned int N, typename Ca, typename Cb>
  1237. inline POLY_POLY_RESULT (N, Ca, Cb)
  1238. ordered_min (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1239. {
  1240. if (known_le (a, b))
  1241. return a;
  1242. else
  1243. {
  1244. if (N > 1)
  1245. gcc_checking_assert (known_le (b, a));
  1246. return b;
  1247. }
  1248. }
  1249. template<unsigned int N, typename Ca, typename Cb>
  1250. inline CONST_POLY_RESULT (N, Ca, Cb)
  1251. ordered_min (const Ca &a, const poly_int_pod<N, Cb> &b)
  1252. {
  1253. if (known_le (a, b))
  1254. return a;
  1255. else
  1256. {
  1257. if (N > 1)
  1258. gcc_checking_assert (known_le (b, a));
  1259. return b;
  1260. }
  1261. }
  1262. template<unsigned int N, typename Ca, typename Cb>
  1263. inline POLY_CONST_RESULT (N, Ca, Cb)
  1264. ordered_min (const poly_int_pod<N, Ca> &a, const Cb &b)
  1265. {
  1266. if (known_le (a, b))
  1267. return a;
  1268. else
  1269. {
  1270. if (N > 1)
  1271. gcc_checking_assert (known_le (b, a));
  1272. return b;
  1273. }
  1274. }
  1275. /* Assert that A and B are known to be ordered and return the maximum
  1276. of the two.
  1277. NOTE: When using this function, please add a comment above the call
  1278. explaining why we know the values are ordered in that context. */
  1279. template<unsigned int N, typename Ca, typename Cb>
  1280. inline POLY_POLY_RESULT (N, Ca, Cb)
  1281. ordered_max (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1282. {
  1283. if (known_le (a, b))
  1284. return b;
  1285. else
  1286. {
  1287. if (N > 1)
  1288. gcc_checking_assert (known_le (b, a));
  1289. return a;
  1290. }
  1291. }
  1292. template<unsigned int N, typename Ca, typename Cb>
  1293. inline CONST_POLY_RESULT (N, Ca, Cb)
  1294. ordered_max (const Ca &a, const poly_int_pod<N, Cb> &b)
  1295. {
  1296. if (known_le (a, b))
  1297. return b;
  1298. else
  1299. {
  1300. if (N > 1)
  1301. gcc_checking_assert (known_le (b, a));
  1302. return a;
  1303. }
  1304. }
  1305. template<unsigned int N, typename Ca, typename Cb>
  1306. inline POLY_CONST_RESULT (N, Ca, Cb)
  1307. ordered_max (const poly_int_pod<N, Ca> &a, const Cb &b)
  1308. {
  1309. if (known_le (a, b))
  1310. return b;
  1311. else
  1312. {
  1313. if (N > 1)
  1314. gcc_checking_assert (known_le (b, a));
  1315. return a;
  1316. }
  1317. }
  1318. /* Return a constant lower bound on the value of A, which is known
  1319. to be nonnegative. */
  1320. template<unsigned int N, typename Ca>
  1321. inline Ca
  1322. constant_lower_bound (const poly_int_pod<N, Ca> &a)
  1323. {
  1324. gcc_checking_assert (known_ge (a, POLY_INT_TYPE (Ca) (0)));
  1325. return a.coeffs[0];
  1326. }
  1327. /* Return the constant lower bound of A, given that it is no less than B. */
  1328. template<unsigned int N, typename Ca, typename Cb>
  1329. inline POLY_CONST_COEFF (Ca, Cb)
  1330. constant_lower_bound_with_limit (const poly_int_pod<N, Ca> &a, const Cb &b)
  1331. {
  1332. if (known_ge (a, b))
  1333. return a.coeffs[0];
  1334. return b;
  1335. }
  1336. /* Return the constant upper bound of A, given that it is no greater
  1337. than B. */
  1338. template<unsigned int N, typename Ca, typename Cb>
  1339. inline POLY_CONST_COEFF (Ca, Cb)
  1340. constant_upper_bound_with_limit (const poly_int_pod<N, Ca> &a, const Cb &b)
  1341. {
  1342. if (known_le (a, b))
  1343. return a.coeffs[0];
  1344. return b;
  1345. }
  1346. /* Return a value that is known to be no greater than A and B. This
  1347. will be the greatest lower bound for some indeterminate values but
  1348. not necessarily for all. */
  1349. template<unsigned int N, typename Ca, typename Cb>
  1350. inline POLY_CONST_RESULT (N, Ca, Cb)
  1351. lower_bound (const poly_int_pod<N, Ca> &a, const Cb &b)
  1352. {
  1353. typedef POLY_CAST (Ca, Cb) NCa;
  1354. typedef POLY_CAST (Cb, Ca) NCb;
  1355. typedef POLY_INT_TYPE (Cb) ICb;
  1356. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1357. poly_int<N, C> r;
  1358. POLY_SET_COEFF (C, r, 0, MIN (NCa (a.coeffs[0]), NCb (b)));
  1359. if (N >= 2)
  1360. for (unsigned int i = 1; i < N; i++)
  1361. POLY_SET_COEFF (C, r, i, MIN (NCa (a.coeffs[i]), ICb (0)));
  1362. return r;
  1363. }
  1364. template<unsigned int N, typename Ca, typename Cb>
  1365. inline CONST_POLY_RESULT (N, Ca, Cb)
  1366. lower_bound (const Ca &a, const poly_int_pod<N, Cb> &b)
  1367. {
  1368. return lower_bound (b, a);
  1369. }
  1370. template<unsigned int N, typename Ca, typename Cb>
  1371. inline POLY_POLY_RESULT (N, Ca, Cb)
  1372. lower_bound (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1373. {
  1374. typedef POLY_CAST (Ca, Cb) NCa;
  1375. typedef POLY_CAST (Cb, Ca) NCb;
  1376. typedef POLY_POLY_COEFF (Ca, Cb) C;
  1377. poly_int<N, C> r;
  1378. for (unsigned int i = 0; i < N; i++)
  1379. POLY_SET_COEFF (C, r, i, MIN (NCa (a.coeffs[i]), NCb (b.coeffs[i])));
  1380. return r;
  1381. }
  1382. template<typename Ca, typename Cb>
  1383. inline CONST_CONST_RESULT (N, Ca, Cb)
  1384. lower_bound (const Ca &a, const Cb &b)
  1385. {
  1386. return a < b ? a : b;
  1387. }
  1388. /* Return a value that is known to be no less than A and B. This will
  1389. be the least upper bound for some indeterminate values but not
  1390. necessarily for all. */
  1391. template<unsigned int N, typename Ca, typename Cb>
  1392. inline POLY_CONST_RESULT (N, Ca, Cb)
  1393. upper_bound (const poly_int_pod<N, Ca> &a, const Cb &b)
  1394. {
  1395. typedef POLY_CAST (Ca, Cb) NCa;
  1396. typedef POLY_CAST (Cb, Ca) NCb;
  1397. typedef POLY_INT_TYPE (Cb) ICb;
  1398. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1399. poly_int<N, C> r;
  1400. POLY_SET_COEFF (C, r, 0, MAX (NCa (a.coeffs[0]), NCb (b)));
  1401. if (N >= 2)
  1402. for (unsigned int i = 1; i < N; i++)
  1403. POLY_SET_COEFF (C, r, i, MAX (NCa (a.coeffs[i]), ICb (0)));
  1404. return r;
  1405. }
  1406. template<unsigned int N, typename Ca, typename Cb>
  1407. inline CONST_POLY_RESULT (N, Ca, Cb)
  1408. upper_bound (const Ca &a, const poly_int_pod<N, Cb> &b)
  1409. {
  1410. return upper_bound (b, a);
  1411. }
  1412. template<unsigned int N, typename Ca, typename Cb>
  1413. inline POLY_POLY_RESULT (N, Ca, Cb)
  1414. upper_bound (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1415. {
  1416. typedef POLY_CAST (Ca, Cb) NCa;
  1417. typedef POLY_CAST (Cb, Ca) NCb;
  1418. typedef POLY_POLY_COEFF (Ca, Cb) C;
  1419. poly_int<N, C> r;
  1420. for (unsigned int i = 0; i < N; i++)
  1421. POLY_SET_COEFF (C, r, i, MAX (NCa (a.coeffs[i]), NCb (b.coeffs[i])));
  1422. return r;
  1423. }
  1424. /* Return the greatest common divisor of all nonzero coefficients, or zero
  1425. if all coefficients are zero. */
  1426. template<unsigned int N, typename Ca>
  1427. inline POLY_BINARY_COEFF (Ca, Ca)
  1428. coeff_gcd (const poly_int_pod<N, Ca> &a)
  1429. {
  1430. /* Find the first nonzero coefficient, stopping at 0 whatever happens. */
  1431. unsigned int i;
  1432. for (i = N - 1; i > 0; --i)
  1433. if (a.coeffs[i] != 0)
  1434. break;
  1435. typedef POLY_BINARY_COEFF (Ca, Ca) C;
  1436. C r = a.coeffs[i];
  1437. for (unsigned int j = 0; j < i; ++j)
  1438. if (a.coeffs[j] != 0)
  1439. r = gcd (r, C (a.coeffs[j]));
  1440. return r;
  1441. }
  1442. /* Return a value that is a multiple of both A and B. This will be the
  1443. least common multiple for some indeterminate values but necessarily
  1444. for all. */
  1445. template<unsigned int N, typename Ca, typename Cb>
  1446. POLY_CONST_RESULT (N, Ca, Cb)
  1447. common_multiple (const poly_int_pod<N, Ca> &a, Cb b)
  1448. {
  1449. POLY_BINARY_COEFF (Ca, Ca) xgcd = coeff_gcd (a);
  1450. return a * (least_common_multiple (xgcd, b) / xgcd);
  1451. }
  1452. template<unsigned int N, typename Ca, typename Cb>
  1453. inline CONST_POLY_RESULT (N, Ca, Cb)
  1454. common_multiple (const Ca &a, const poly_int_pod<N, Cb> &b)
  1455. {
  1456. return common_multiple (b, a);
  1457. }
  1458. /* Return a value that is a multiple of both A and B, asserting that
  1459. such a value exists. The result will be the least common multiple
  1460. for some indeterminate values but necessarily for all.
  1461. NOTE: When using this function, please add a comment above the call
  1462. explaining why we know the values have a common multiple (which might
  1463. for example be because we know A / B is rational). */
  1464. template<unsigned int N, typename Ca, typename Cb>
  1465. POLY_POLY_RESULT (N, Ca, Cb)
  1466. force_common_multiple (const poly_int_pod<N, Ca> &a,
  1467. const poly_int_pod<N, Cb> &b)
  1468. {
  1469. if (b.is_constant ())
  1470. return common_multiple (a, b.coeffs[0]);
  1471. if (a.is_constant ())
  1472. return common_multiple (a.coeffs[0], b);
  1473. typedef POLY_CAST (Ca, Cb) NCa;
  1474. typedef POLY_CAST (Cb, Ca) NCb;
  1475. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1476. typedef POLY_INT_TYPE (Ca) ICa;
  1477. for (unsigned int i = 1; i < N; ++i)
  1478. if (a.coeffs[i] != ICa (0))
  1479. {
  1480. C lcm = least_common_multiple (NCa (a.coeffs[i]), NCb (b.coeffs[i]));
  1481. C amul = lcm / a.coeffs[i];
  1482. C bmul = lcm / b.coeffs[i];
  1483. for (unsigned int j = 0; j < N; ++j)
  1484. gcc_checking_assert (a.coeffs[j] * amul == b.coeffs[j] * bmul);
  1485. return a * amul;
  1486. }
  1487. gcc_unreachable ();
  1488. }
  1489. /* Compare A and B for sorting purposes, returning -1 if A should come
  1490. before B, 0 if A and B are identical, and 1 if A should come after B.
  1491. This is a lexicographical compare of the coefficients in reverse order.
  1492. A consequence of this is that all constant sizes come before all
  1493. non-constant ones, regardless of magnitude (since a size is never
  1494. negative). This is what most callers want. For example, when laying
  1495. data out on the stack, it's better to keep all the constant-sized
  1496. data together so that it can be accessed as a constant offset from a
  1497. single base. */
  1498. template<unsigned int N, typename Ca, typename Cb>
  1499. inline int
  1500. compare_sizes_for_sort (const poly_int_pod<N, Ca> &a,
  1501. const poly_int_pod<N, Cb> &b)
  1502. {
  1503. for (unsigned int i = N; i-- > 0; )
  1504. if (a.coeffs[i] != b.coeffs[i])
  1505. return a.coeffs[i] < b.coeffs[i] ? -1 : 1;
  1506. return 0;
  1507. }
  1508. /* Return true if we can calculate VALUE & (ALIGN - 1) at compile time. */
  1509. template<unsigned int N, typename Ca, typename Cb>
  1510. inline bool
  1511. can_align_p (const poly_int_pod<N, Ca> &value, Cb align)
  1512. {
  1513. for (unsigned int i = 1; i < N; i++)
  1514. if ((value.coeffs[i] & (align - 1)) != 0)
  1515. return false;
  1516. return true;
  1517. }
  1518. /* Return true if we can align VALUE up to the smallest multiple of
  1519. ALIGN that is >= VALUE. Store the aligned value in *ALIGNED if so. */
  1520. template<unsigned int N, typename Ca, typename Cb>
  1521. inline bool
  1522. can_align_up (const poly_int_pod<N, Ca> &value, Cb align,
  1523. poly_int_pod<N, Ca> *aligned)
  1524. {
  1525. if (!can_align_p (value, align))
  1526. return false;
  1527. *aligned = value + (-value.coeffs[0] & (align - 1));
  1528. return true;
  1529. }
  1530. /* Return true if we can align VALUE down to the largest multiple of
  1531. ALIGN that is <= VALUE. Store the aligned value in *ALIGNED if so. */
  1532. template<unsigned int N, typename Ca, typename Cb>
  1533. inline bool
  1534. can_align_down (const poly_int_pod<N, Ca> &value, Cb align,
  1535. poly_int_pod<N, Ca> *aligned)
  1536. {
  1537. if (!can_align_p (value, align))
  1538. return false;
  1539. *aligned = value - (value.coeffs[0] & (align - 1));
  1540. return true;
  1541. }
  1542. /* Return true if we can align A and B up to the smallest multiples of
  1543. ALIGN that are >= A and B respectively, and if doing so gives the
  1544. same value. */
  1545. template<unsigned int N, typename Ca, typename Cb, typename Cc>
  1546. inline bool
  1547. known_equal_after_align_up (const poly_int_pod<N, Ca> &a,
  1548. const poly_int_pod<N, Cb> &b,
  1549. Cc align)
  1550. {
  1551. poly_int<N, Ca> aligned_a;
  1552. poly_int<N, Cb> aligned_b;
  1553. return (can_align_up (a, align, &aligned_a)
  1554. && can_align_up (b, align, &aligned_b)
  1555. && known_eq (aligned_a, aligned_b));
  1556. }
  1557. /* Return true if we can align A and B down to the largest multiples of
  1558. ALIGN that are <= A and B respectively, and if doing so gives the
  1559. same value. */
  1560. template<unsigned int N, typename Ca, typename Cb, typename Cc>
  1561. inline bool
  1562. known_equal_after_align_down (const poly_int_pod<N, Ca> &a,
  1563. const poly_int_pod<N, Cb> &b,
  1564. Cc align)
  1565. {
  1566. poly_int<N, Ca> aligned_a;
  1567. poly_int<N, Cb> aligned_b;
  1568. return (can_align_down (a, align, &aligned_a)
  1569. && can_align_down (b, align, &aligned_b)
  1570. && known_eq (aligned_a, aligned_b));
  1571. }
  1572. /* Assert that we can align VALUE to ALIGN at compile time and return
  1573. the smallest multiple of ALIGN that is >= VALUE.
  1574. NOTE: When using this function, please add a comment above the call
  1575. explaining why we know the non-constant coefficients must already
  1576. be a multiple of ALIGN. */
  1577. template<unsigned int N, typename Ca, typename Cb>
  1578. inline poly_int<N, Ca>
  1579. force_align_up (const poly_int_pod<N, Ca> &value, Cb align)
  1580. {
  1581. gcc_checking_assert (can_align_p (value, align));
  1582. return value + (-value.coeffs[0] & (align - 1));
  1583. }
  1584. /* Assert that we can align VALUE to ALIGN at compile time and return
  1585. the largest multiple of ALIGN that is <= VALUE.
  1586. NOTE: When using this function, please add a comment above the call
  1587. explaining why we know the non-constant coefficients must already
  1588. be a multiple of ALIGN. */
  1589. template<unsigned int N, typename Ca, typename Cb>
  1590. inline poly_int<N, Ca>
  1591. force_align_down (const poly_int_pod<N, Ca> &value, Cb align)
  1592. {
  1593. gcc_checking_assert (can_align_p (value, align));
  1594. return value - (value.coeffs[0] & (align - 1));
  1595. }
  1596. /* Return a value <= VALUE that is a multiple of ALIGN. It will be the
  1597. greatest such value for some indeterminate values but not necessarily
  1598. for all. */
  1599. template<unsigned int N, typename Ca, typename Cb>
  1600. inline poly_int<N, Ca>
  1601. aligned_lower_bound (const poly_int_pod<N, Ca> &value, Cb align)
  1602. {
  1603. poly_int<N, Ca> r;
  1604. for (unsigned int i = 0; i < N; i++)
  1605. /* This form copes correctly with more type combinations than
  1606. value.coeffs[i] & -align would. */
  1607. POLY_SET_COEFF (Ca, r, i, (value.coeffs[i]
  1608. - (value.coeffs[i] & (align - 1))));
  1609. return r;
  1610. }
  1611. /* Return a value >= VALUE that is a multiple of ALIGN. It will be the
  1612. least such value for some indeterminate values but not necessarily
  1613. for all. */
  1614. template<unsigned int N, typename Ca, typename Cb>
  1615. inline poly_int<N, Ca>
  1616. aligned_upper_bound (const poly_int_pod<N, Ca> &value, Cb align)
  1617. {
  1618. poly_int<N, Ca> r;
  1619. for (unsigned int i = 0; i < N; i++)
  1620. POLY_SET_COEFF (Ca, r, i, (value.coeffs[i]
  1621. + (-value.coeffs[i] & (align - 1))));
  1622. return r;
  1623. }
  1624. /* Assert that we can align VALUE to ALIGN at compile time. Align VALUE
  1625. down to the largest multiple of ALIGN that is <= VALUE, then divide by
  1626. ALIGN.
  1627. NOTE: When using this function, please add a comment above the call
  1628. explaining why we know the non-constant coefficients must already
  1629. be a multiple of ALIGN. */
  1630. template<unsigned int N, typename Ca, typename Cb>
  1631. inline poly_int<N, Ca>
  1632. force_align_down_and_div (const poly_int_pod<N, Ca> &value, Cb align)
  1633. {
  1634. gcc_checking_assert (can_align_p (value, align));
  1635. poly_int<N, Ca> r;
  1636. POLY_SET_COEFF (Ca, r, 0, ((value.coeffs[0]
  1637. - (value.coeffs[0] & (align - 1)))
  1638. / align));
  1639. if (N >= 2)
  1640. for (unsigned int i = 1; i < N; i++)
  1641. POLY_SET_COEFF (Ca, r, i, value.coeffs[i] / align);
  1642. return r;
  1643. }
  1644. /* Assert that we can align VALUE to ALIGN at compile time. Align VALUE
  1645. up to the smallest multiple of ALIGN that is >= VALUE, then divide by
  1646. ALIGN.
  1647. NOTE: When using this function, please add a comment above the call
  1648. explaining why we know the non-constant coefficients must already
  1649. be a multiple of ALIGN. */
  1650. template<unsigned int N, typename Ca, typename Cb>
  1651. inline poly_int<N, Ca>
  1652. force_align_up_and_div (const poly_int_pod<N, Ca> &value, Cb align)
  1653. {
  1654. gcc_checking_assert (can_align_p (value, align));
  1655. poly_int<N, Ca> r;
  1656. POLY_SET_COEFF (Ca, r, 0, ((value.coeffs[0]
  1657. + (-value.coeffs[0] & (align - 1)))
  1658. / align));
  1659. if (N >= 2)
  1660. for (unsigned int i = 1; i < N; i++)
  1661. POLY_SET_COEFF (Ca, r, i, value.coeffs[i] / align);
  1662. return r;
  1663. }
  1664. /* Return true if we know at compile time the difference between VALUE
  1665. and the equal or preceding multiple of ALIGN. Store the value in
  1666. *MISALIGN if so. */
  1667. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1668. inline bool
  1669. known_misalignment (const poly_int_pod<N, Ca> &value, Cb align, Cm *misalign)
  1670. {
  1671. gcc_checking_assert (align != 0);
  1672. if (!can_align_p (value, align))
  1673. return false;
  1674. *misalign = value.coeffs[0] & (align - 1);
  1675. return true;
  1676. }
  1677. /* Return X & (Y - 1), asserting that this value is known. Please add
  1678. an a comment above callers to this function to explain why the condition
  1679. is known to hold. */
  1680. template<unsigned int N, typename Ca, typename Cb>
  1681. inline POLY_BINARY_COEFF (Ca, Ca)
  1682. force_get_misalignment (const poly_int_pod<N, Ca> &a, Cb align)
  1683. {
  1684. gcc_checking_assert (can_align_p (a, align));
  1685. return a.coeffs[0] & (align - 1);
  1686. }
  1687. /* Return the maximum alignment that A is known to have. Return 0
  1688. if A is known to be zero. */
  1689. template<unsigned int N, typename Ca>
  1690. inline POLY_BINARY_COEFF (Ca, Ca)
  1691. known_alignment (const poly_int_pod<N, Ca> &a)
  1692. {
  1693. typedef POLY_BINARY_COEFF (Ca, Ca) C;
  1694. C r = a.coeffs[0];
  1695. for (unsigned int i = 1; i < N; ++i)
  1696. r |= a.coeffs[i];
  1697. return r & -r;
  1698. }
  1699. /* Return true if we can compute A | B at compile time, storing the
  1700. result in RES if so. */
  1701. template<unsigned int N, typename Ca, typename Cb, typename Cr>
  1702. inline typename if_nonpoly<Cb, bool>::type
  1703. can_ior_p (const poly_int_pod<N, Ca> &a, Cb b, Cr *result)
  1704. {
  1705. /* Coefficients 1 and above must be a multiple of something greater
  1706. than B. */
  1707. typedef POLY_INT_TYPE (Ca) int_type;
  1708. if (N >= 2)
  1709. for (unsigned int i = 1; i < N; i++)
  1710. if ((-(a.coeffs[i] & -a.coeffs[i]) & b) != int_type (0))
  1711. return false;
  1712. *result = a;
  1713. result->coeffs[0] |= b;
  1714. return true;
  1715. }
  1716. /* Return true if A is a constant multiple of B, storing the
  1717. multiple in *MULTIPLE if so. */
  1718. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1719. inline typename if_nonpoly<Cb, bool>::type
  1720. constant_multiple_p (const poly_int_pod<N, Ca> &a, Cb b, Cm *multiple)
  1721. {
  1722. typedef POLY_CAST (Ca, Cb) NCa;
  1723. typedef POLY_CAST (Cb, Ca) NCb;
  1724. /* Do the modulus before the constant check, to catch divide by
  1725. zero errors. */
  1726. if (NCa (a.coeffs[0]) % NCb (b) != 0 || !a.is_constant ())
  1727. return false;
  1728. *multiple = NCa (a.coeffs[0]) / NCb (b);
  1729. return true;
  1730. }
  1731. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1732. inline typename if_nonpoly<Ca, bool>::type
  1733. constant_multiple_p (Ca a, const poly_int_pod<N, Cb> &b, Cm *multiple)
  1734. {
  1735. typedef POLY_CAST (Ca, Cb) NCa;
  1736. typedef POLY_CAST (Cb, Ca) NCb;
  1737. typedef POLY_INT_TYPE (Ca) int_type;
  1738. /* Do the modulus before the constant check, to catch divide by
  1739. zero errors. */
  1740. if (NCa (a) % NCb (b.coeffs[0]) != 0
  1741. || (a != int_type (0) && !b.is_constant ()))
  1742. return false;
  1743. *multiple = NCa (a) / NCb (b.coeffs[0]);
  1744. return true;
  1745. }
  1746. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1747. inline bool
  1748. constant_multiple_p (const poly_int_pod<N, Ca> &a,
  1749. const poly_int_pod<N, Cb> &b, Cm *multiple)
  1750. {
  1751. typedef POLY_CAST (Ca, Cb) NCa;
  1752. typedef POLY_CAST (Cb, Ca) NCb;
  1753. typedef POLY_INT_TYPE (Ca) ICa;
  1754. typedef POLY_INT_TYPE (Cb) ICb;
  1755. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1756. if (NCa (a.coeffs[0]) % NCb (b.coeffs[0]) != 0)
  1757. return false;
  1758. C r = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1759. for (unsigned int i = 1; i < N; ++i)
  1760. if (b.coeffs[i] == ICb (0)
  1761. ? a.coeffs[i] != ICa (0)
  1762. : (NCa (a.coeffs[i]) % NCb (b.coeffs[i]) != 0
  1763. || NCa (a.coeffs[i]) / NCb (b.coeffs[i]) != r))
  1764. return false;
  1765. *multiple = r;
  1766. return true;
  1767. }
  1768. /* Return true if A is a multiple of B. */
  1769. template<typename Ca, typename Cb>
  1770. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1771. multiple_p (Ca a, Cb b)
  1772. {
  1773. return a % b == 0;
  1774. }
  1775. /* Return true if A is a (polynomial) multiple of B. */
  1776. template<unsigned int N, typename Ca, typename Cb>
  1777. inline typename if_nonpoly<Cb, bool>::type
  1778. multiple_p (const poly_int_pod<N, Ca> &a, Cb b)
  1779. {
  1780. for (unsigned int i = 0; i < N; ++i)
  1781. if (a.coeffs[i] % b != 0)
  1782. return false;
  1783. return true;
  1784. }
  1785. /* Return true if A is a (constant) multiple of B. */
  1786. template<unsigned int N, typename Ca, typename Cb>
  1787. inline typename if_nonpoly<Ca, bool>::type
  1788. multiple_p (Ca a, const poly_int_pod<N, Cb> &b)
  1789. {
  1790. typedef POLY_INT_TYPE (Ca) int_type;
  1791. /* Do the modulus before the constant check, to catch divide by
  1792. potential zeros. */
  1793. return a % b.coeffs[0] == 0 && (a == int_type (0) || b.is_constant ());
  1794. }
  1795. /* Return true if A is a (polynomial) multiple of B. This handles cases
  1796. where either B is constant or the multiple is constant. */
  1797. template<unsigned int N, typename Ca, typename Cb>
  1798. inline bool
  1799. multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1800. {
  1801. if (b.is_constant ())
  1802. return multiple_p (a, b.coeffs[0]);
  1803. POLY_BINARY_COEFF (Ca, Ca) tmp;
  1804. return constant_multiple_p (a, b, &tmp);
  1805. }
  1806. /* Return true if A is a (constant) multiple of B, storing the
  1807. multiple in *MULTIPLE if so. */
  1808. template<typename Ca, typename Cb, typename Cm>
  1809. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1810. multiple_p (Ca a, Cb b, Cm *multiple)
  1811. {
  1812. if (a % b != 0)
  1813. return false;
  1814. *multiple = a / b;
  1815. return true;
  1816. }
  1817. /* Return true if A is a (polynomial) multiple of B, storing the
  1818. multiple in *MULTIPLE if so. */
  1819. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1820. inline typename if_nonpoly<Cb, bool>::type
  1821. multiple_p (const poly_int_pod<N, Ca> &a, Cb b, poly_int_pod<N, Cm> *multiple)
  1822. {
  1823. if (!multiple_p (a, b))
  1824. return false;
  1825. for (unsigned int i = 0; i < N; ++i)
  1826. multiple->coeffs[i] = a.coeffs[i] / b;
  1827. return true;
  1828. }
  1829. /* Return true if B is a constant and A is a (constant) multiple of B,
  1830. storing the multiple in *MULTIPLE if so. */
  1831. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1832. inline typename if_nonpoly<Ca, bool>::type
  1833. multiple_p (Ca a, const poly_int_pod<N, Cb> &b, Cm *multiple)
  1834. {
  1835. typedef POLY_CAST (Ca, Cb) NCa;
  1836. /* Do the modulus before the constant check, to catch divide by
  1837. potential zeros. */
  1838. if (a % b.coeffs[0] != 0 || (NCa (a) != 0 && !b.is_constant ()))
  1839. return false;
  1840. *multiple = a / b.coeffs[0];
  1841. return true;
  1842. }
  1843. /* Return true if A is a (polynomial) multiple of B, storing the
  1844. multiple in *MULTIPLE if so. This handles cases where either
  1845. B is constant or the multiple is constant. */
  1846. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1847. inline bool
  1848. multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
  1849. poly_int_pod<N, Cm> *multiple)
  1850. {
  1851. if (b.is_constant ())
  1852. return multiple_p (a, b.coeffs[0], multiple);
  1853. return constant_multiple_p (a, b, multiple);
  1854. }
  1855. /* Return A / B, given that A is known to be a multiple of B. */
  1856. template<unsigned int N, typename Ca, typename Cb>
  1857. inline POLY_CONST_RESULT (N, Ca, Cb)
  1858. exact_div (const poly_int_pod<N, Ca> &a, Cb b)
  1859. {
  1860. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1861. poly_int<N, C> r;
  1862. for (unsigned int i = 0; i < N; i++)
  1863. {
  1864. gcc_checking_assert (a.coeffs[i] % b == 0);
  1865. POLY_SET_COEFF (C, r, i, a.coeffs[i] / b);
  1866. }
  1867. return r;
  1868. }
  1869. /* Return A / B, given that A is known to be a multiple of B. */
  1870. template<unsigned int N, typename Ca, typename Cb>
  1871. inline POLY_POLY_RESULT (N, Ca, Cb)
  1872. exact_div (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1873. {
  1874. if (b.is_constant ())
  1875. return exact_div (a, b.coeffs[0]);
  1876. typedef POLY_CAST (Ca, Cb) NCa;
  1877. typedef POLY_CAST (Cb, Ca) NCb;
  1878. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1879. typedef POLY_INT_TYPE (Cb) int_type;
  1880. gcc_checking_assert (a.coeffs[0] % b.coeffs[0] == 0);
  1881. C r = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1882. for (unsigned int i = 1; i < N; ++i)
  1883. gcc_checking_assert (b.coeffs[i] == int_type (0)
  1884. ? a.coeffs[i] == int_type (0)
  1885. : (a.coeffs[i] % b.coeffs[i] == 0
  1886. && NCa (a.coeffs[i]) / NCb (b.coeffs[i]) == r));
  1887. return r;
  1888. }
  1889. /* Return true if there is some constant Q and polynomial r such that:
  1890. (1) a = b * Q + r
  1891. (2) |b * Q| <= |a|
  1892. (3) |r| < |b|
  1893. Store the value Q in *QUOTIENT if so. */
  1894. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  1895. inline typename if_nonpoly2<Cb, Cq, bool>::type
  1896. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b, Cq *quotient)
  1897. {
  1898. typedef POLY_CAST (Ca, Cb) NCa;
  1899. typedef POLY_CAST (Cb, Ca) NCb;
  1900. /* Do the division before the constant check, to catch divide by
  1901. zero errors. */
  1902. Cq q = NCa (a.coeffs[0]) / NCb (b);
  1903. if (!a.is_constant ())
  1904. return false;
  1905. *quotient = q;
  1906. return true;
  1907. }
  1908. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  1909. inline typename if_nonpoly<Cq, bool>::type
  1910. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  1911. const poly_int_pod<N, Cb> &b,
  1912. Cq *quotient)
  1913. {
  1914. /* We can calculate Q from the case in which the indeterminates
  1915. are zero. */
  1916. typedef POLY_CAST (Ca, Cb) NCa;
  1917. typedef POLY_CAST (Cb, Ca) NCb;
  1918. typedef POLY_INT_TYPE (Ca) ICa;
  1919. typedef POLY_INT_TYPE (Cb) ICb;
  1920. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1921. C q = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1922. /* Check the other coefficients and record whether the division is exact.
  1923. The only difficult case is when it isn't. If we require a and b to
  1924. ordered wrt zero, there can be no two coefficients of the same value
  1925. that have opposite signs. This means that:
  1926. |a| = |a0| + |a1 * x1| + |a2 * x2| + ...
  1927. |b| = |b0| + |b1 * x1| + |b2 * x2| + ...
  1928. The Q we've just calculated guarantees:
  1929. |b0 * Q| <= |a0|
  1930. |a0 - b0 * Q| < |b0|
  1931. and so:
  1932. (2) |b * Q| <= |a|
  1933. is satisfied if:
  1934. |bi * xi * Q| <= |ai * xi|
  1935. for each i in [1, N]. This is trivially true when xi is zero.
  1936. When it isn't we need:
  1937. (2') |bi * Q| <= |ai|
  1938. r is calculated as:
  1939. r = r0 + r1 * x1 + r2 * x2 + ...
  1940. where ri = ai - bi * Q
  1941. Restricting to ordered a and b also guarantees that no two ris
  1942. have opposite signs, so we have:
  1943. |r| = |r0| + |r1 * x1| + |r2 * x2| + ...
  1944. We know from the calculation of Q that |r0| < |b0|, so:
  1945. (3) |r| < |b|
  1946. is satisfied if:
  1947. (3') |ai - bi * Q| <= |bi|
  1948. for each i in [1, N]. */
  1949. bool rem_p = NCa (a.coeffs[0]) % NCb (b.coeffs[0]) != 0;
  1950. for (unsigned int i = 1; i < N; ++i)
  1951. {
  1952. if (b.coeffs[i] == ICb (0))
  1953. {
  1954. /* For bi == 0 we simply need: (3') |ai| == 0. */
  1955. if (a.coeffs[i] != ICa (0))
  1956. return false;
  1957. }
  1958. else
  1959. {
  1960. if (q == 0)
  1961. {
  1962. /* For Q == 0 we simply need: (3') |ai| <= |bi|. */
  1963. if (a.coeffs[i] != ICa (0))
  1964. {
  1965. /* Use negative absolute to avoid overflow, i.e.
  1966. -|ai| >= -|bi|. */
  1967. C neg_abs_a = (a.coeffs[i] < 0 ? a.coeffs[i] : -a.coeffs[i]);
  1968. C neg_abs_b = (b.coeffs[i] < 0 ? b.coeffs[i] : -b.coeffs[i]);
  1969. if (neg_abs_a < neg_abs_b)
  1970. return false;
  1971. rem_p = true;
  1972. }
  1973. }
  1974. else
  1975. {
  1976. /* Otherwise just check for the case in which ai / bi == Q. */
  1977. if (NCa (a.coeffs[i]) / NCb (b.coeffs[i]) != q)
  1978. return false;
  1979. if (NCa (a.coeffs[i]) % NCb (b.coeffs[i]) != 0)
  1980. rem_p = true;
  1981. }
  1982. }
  1983. }
  1984. /* If the division isn't exact, require both values to be ordered wrt 0,
  1985. so that we can guarantee conditions (2) and (3) for all indeterminate
  1986. values. */
  1987. if (rem_p && (!ordered_p (a, ICa (0)) || !ordered_p (b, ICb (0))))
  1988. return false;
  1989. *quotient = q;
  1990. return true;
  1991. }
  1992. /* Likewise, but also store r in *REMAINDER. */
  1993. template<unsigned int N, typename Ca, typename Cb, typename Cq, typename Cr>
  1994. inline typename if_nonpoly<Cq, bool>::type
  1995. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  1996. const poly_int_pod<N, Cb> &b,
  1997. Cq *quotient, Cr *remainder)
  1998. {
  1999. if (!can_div_trunc_p (a, b, quotient))
  2000. return false;
  2001. *remainder = a - *quotient * b;
  2002. return true;
  2003. }
  2004. /* Return true if there is some polynomial q and constant R such that:
  2005. (1) a = B * q + R
  2006. (2) |B * q| <= |a|
  2007. (3) |R| < |B|
  2008. Store the value q in *QUOTIENT if so. */
  2009. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  2010. inline typename if_nonpoly<Cb, bool>::type
  2011. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b,
  2012. poly_int_pod<N, Cq> *quotient)
  2013. {
  2014. /* The remainder must be constant. */
  2015. for (unsigned int i = 1; i < N; ++i)
  2016. if (a.coeffs[i] % b != 0)
  2017. return false;
  2018. for (unsigned int i = 0; i < N; ++i)
  2019. quotient->coeffs[i] = a.coeffs[i] / b;
  2020. return true;
  2021. }
  2022. /* Likewise, but also store R in *REMAINDER. */
  2023. template<unsigned int N, typename Ca, typename Cb, typename Cq, typename Cr>
  2024. inline typename if_nonpoly<Cb, bool>::type
  2025. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b,
  2026. poly_int_pod<N, Cq> *quotient, Cr *remainder)
  2027. {
  2028. if (!can_div_trunc_p (a, b, quotient))
  2029. return false;
  2030. *remainder = a.coeffs[0] % b;
  2031. return true;
  2032. }
  2033. /* Return true if we can compute A / B at compile time, rounding towards zero.
  2034. Store the result in QUOTIENT if so.
  2035. This handles cases in which either B is constant or the result is
  2036. constant. */
  2037. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  2038. inline bool
  2039. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  2040. const poly_int_pod<N, Cb> &b,
  2041. poly_int_pod<N, Cq> *quotient)
  2042. {
  2043. if (b.is_constant ())
  2044. return can_div_trunc_p (a, b.coeffs[0], quotient);
  2045. if (!can_div_trunc_p (a, b, &quotient->coeffs[0]))
  2046. return false;
  2047. for (unsigned int i = 1; i < N; ++i)
  2048. quotient->coeffs[i] = 0;
  2049. return true;
  2050. }
  2051. /* Return true if there is some constant Q and polynomial r such that:
  2052. (1) a = b * Q + r
  2053. (2) |a| <= |b * Q|
  2054. (3) |r| < |b|
  2055. Store the value Q in *QUOTIENT if so. */
  2056. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  2057. inline typename if_nonpoly<Cq, bool>::type
  2058. can_div_away_from_zero_p (const poly_int_pod<N, Ca> &a,
  2059. const poly_int_pod<N, Cb> &b,
  2060. Cq *quotient)
  2061. {
  2062. if (!can_div_trunc_p (a, b, quotient))
  2063. return false;
  2064. if (maybe_ne (*quotient * b, a))
  2065. *quotient += (*quotient < 0 ? -1 : 1);
  2066. return true;
  2067. }
  2068. /* Use print_dec to print VALUE to FILE, where SGN is the sign
  2069. of the values. */
  2070. template<unsigned int N, typename C>
  2071. void
  2072. print_dec (const poly_int_pod<N, C> &value, FILE *file, signop sgn)
  2073. {
  2074. if (value.is_constant ())
  2075. print_dec (value.coeffs[0], file, sgn);
  2076. else
  2077. {
  2078. fprintf (file, "[");
  2079. for (unsigned int i = 0; i < N; ++i)
  2080. {
  2081. print_dec (value.coeffs[i], file, sgn);
  2082. fputc (i == N - 1 ? ']' : ',', file);
  2083. }
  2084. }
  2085. }
  2086. /* Likewise without the signop argument, for coefficients that have an
  2087. inherent signedness. */
  2088. template<unsigned int N, typename C>
  2089. void
  2090. print_dec (const poly_int_pod<N, C> &value, FILE *file)
  2091. {
  2092. STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
  2093. print_dec (value, file,
  2094. poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED);
  2095. }
  2096. /* Use print_hex to print VALUE to FILE. */
  2097. template<unsigned int N, typename C>
  2098. void
  2099. print_hex (const poly_int_pod<N, C> &value, FILE *file)
  2100. {
  2101. if (value.is_constant ())
  2102. print_hex (value.coeffs[0], file);
  2103. else
  2104. {
  2105. fprintf (file, "[");
  2106. for (unsigned int i = 0; i < N; ++i)
  2107. {
  2108. print_hex (value.coeffs[i], file);
  2109. fputc (i == N - 1 ? ']' : ',', file);
  2110. }
  2111. }
  2112. }
  2113. /* Helper for calculating the distance between two points P1 and P2,
  2114. in cases where known_le (P1, P2). T1 and T2 are the types of the
  2115. two positions, in either order. The coefficients of P2 - P1 have
  2116. type unsigned HOST_WIDE_INT if the coefficients of both T1 and T2
  2117. have C++ primitive type, otherwise P2 - P1 has its usual
  2118. wide-int-based type.
  2119. The actual subtraction should look something like this:
  2120. typedef poly_span_traits<T1, T2> span_traits;
  2121. span_traits::cast (P2) - span_traits::cast (P1)
  2122. Applying the cast before the subtraction avoids undefined overflow
  2123. for signed T1 and T2.
  2124. The implementation of the cast tries to avoid unnecessary arithmetic
  2125. or copying. */
  2126. template<typename T1, typename T2,
  2127. typename Res = POLY_BINARY_COEFF (POLY_BINARY_COEFF (T1, T2),
  2128. unsigned HOST_WIDE_INT)>
  2129. struct poly_span_traits
  2130. {
  2131. template<typename T>
  2132. static const T &cast (const T &x) { return x; }
  2133. };
  2134. template<typename T1, typename T2>
  2135. struct poly_span_traits<T1, T2, unsigned HOST_WIDE_INT>
  2136. {
  2137. template<typename T>
  2138. static typename if_nonpoly<T, unsigned HOST_WIDE_INT>::type
  2139. cast (const T &x) { return x; }
  2140. template<unsigned int N, typename T>
  2141. static poly_int<N, unsigned HOST_WIDE_INT>
  2142. cast (const poly_int_pod<N, T> &x) { return x; }
  2143. };
  2144. /* Return true if SIZE represents a known size, assuming that all-ones
  2145. indicates an unknown size. */
  2146. template<typename T>
  2147. inline bool
  2148. known_size_p (const T &a)
  2149. {
  2150. return maybe_ne (a, POLY_INT_TYPE (T) (-1));
  2151. }
  2152. /* Return true if range [POS, POS + SIZE) might include VAL.
  2153. SIZE can be the special value -1, in which case the range is
  2154. open-ended. */
  2155. template<typename T1, typename T2, typename T3>
  2156. inline bool
  2157. maybe_in_range_p (const T1 &val, const T2 &pos, const T3 &size)
  2158. {
  2159. typedef poly_span_traits<T1, T2> start_span;
  2160. typedef poly_span_traits<T3, T3> size_span;
  2161. if (known_lt (val, pos))
  2162. return false;
  2163. if (!known_size_p (size))
  2164. return true;
  2165. if ((poly_int_traits<T1>::num_coeffs > 1
  2166. || poly_int_traits<T2>::num_coeffs > 1)
  2167. && maybe_lt (val, pos))
  2168. /* In this case we don't know whether VAL >= POS is true at compile
  2169. time, so we can't prove that VAL >= POS + SIZE. */
  2170. return true;
  2171. return maybe_lt (start_span::cast (val) - start_span::cast (pos),
  2172. size_span::cast (size));
  2173. }
  2174. /* Return true if range [POS, POS + SIZE) is known to include VAL.
  2175. SIZE can be the special value -1, in which case the range is
  2176. open-ended. */
  2177. template<typename T1, typename T2, typename T3>
  2178. inline bool
  2179. known_in_range_p (const T1 &val, const T2 &pos, const T3 &size)
  2180. {
  2181. typedef poly_span_traits<T1, T2> start_span;
  2182. typedef poly_span_traits<T3, T3> size_span;
  2183. return (known_size_p (size)
  2184. && known_ge (val, pos)
  2185. && known_lt (start_span::cast (val) - start_span::cast (pos),
  2186. size_span::cast (size)));
  2187. }
  2188. /* Return true if the two ranges [POS1, POS1 + SIZE1) and [POS2, POS2 + SIZE2)
  2189. might overlap. SIZE1 and/or SIZE2 can be the special value -1, in which
  2190. case the range is open-ended. */
  2191. template<typename T1, typename T2, typename T3, typename T4>
  2192. inline bool
  2193. ranges_maybe_overlap_p (const T1 &pos1, const T2 &size1,
  2194. const T3 &pos2, const T4 &size2)
  2195. {
  2196. if (maybe_in_range_p (pos2, pos1, size1))
  2197. return maybe_ne (size2, POLY_INT_TYPE (T4) (0));
  2198. if (maybe_in_range_p (pos1, pos2, size2))
  2199. return maybe_ne (size1, POLY_INT_TYPE (T2) (0));
  2200. return false;
  2201. }
  2202. /* Return true if the two ranges [POS1, POS1 + SIZE1) and [POS2, POS2 + SIZE2)
  2203. are known to overlap. SIZE1 and/or SIZE2 can be the special value -1,
  2204. in which case the range is open-ended. */
  2205. template<typename T1, typename T2, typename T3, typename T4>
  2206. inline bool
  2207. ranges_known_overlap_p (const T1 &pos1, const T2 &size1,
  2208. const T3 &pos2, const T4 &size2)
  2209. {
  2210. typedef poly_span_traits<T1, T3> start_span;
  2211. typedef poly_span_traits<T2, T2> size1_span;
  2212. typedef poly_span_traits<T4, T4> size2_span;
  2213. /* known_gt (POS1 + SIZE1, POS2) [infinite precision]
  2214. --> known_gt (SIZE1, POS2 - POS1) [infinite precision]
  2215. --> known_gt (SIZE1, POS2 - lower_bound (POS1, POS2)) [infinite precision]
  2216. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ always nonnegative
  2217. --> known_gt (SIZE1, span1::cast (POS2 - lower_bound (POS1, POS2))).
  2218. Using the saturating subtraction enforces that SIZE1 must be
  2219. nonzero, since known_gt (0, x) is false for all nonnegative x.
  2220. If POS2.coeff[I] < POS1.coeff[I] for some I > 0, increasing
  2221. indeterminate number I makes the unsaturated condition easier to
  2222. satisfy, so using a saturated coefficient of zero tests the case in
  2223. which the indeterminate is zero (the minimum value). */
  2224. return (known_size_p (size1)
  2225. && known_size_p (size2)
  2226. && known_lt (start_span::cast (pos2)
  2227. - start_span::cast (lower_bound (pos1, pos2)),
  2228. size1_span::cast (size1))
  2229. && known_lt (start_span::cast (pos1)
  2230. - start_span::cast (lower_bound (pos1, pos2)),
  2231. size2_span::cast (size2)));
  2232. }
  2233. /* Return true if range [POS1, POS1 + SIZE1) is known to be a subrange of
  2234. [POS2, POS2 + SIZE2). SIZE1 and/or SIZE2 can be the special value -1,
  2235. in which case the range is open-ended. */
  2236. template<typename T1, typename T2, typename T3, typename T4>
  2237. inline bool
  2238. known_subrange_p (const T1 &pos1, const T2 &size1,
  2239. const T3 &pos2, const T4 &size2)
  2240. {
  2241. typedef typename poly_int_traits<T2>::coeff_type C2;
  2242. typedef poly_span_traits<T1, T3> start_span;
  2243. typedef poly_span_traits<T2, T4> size_span;
  2244. return (known_gt (size1, POLY_INT_TYPE (T2) (0))
  2245. && (poly_coeff_traits<C2>::signedness > 0
  2246. || known_size_p (size1))
  2247. && known_size_p (size2)
  2248. && known_ge (pos1, pos2)
  2249. && known_le (size1, size2)
  2250. && known_le (start_span::cast (pos1) - start_span::cast (pos2),
  2251. size_span::cast (size2) - size_span::cast (size1)));
  2252. }
  2253. /* Return true if the endpoint of the range [POS, POS + SIZE) can be
  2254. stored in a T, or if SIZE is the special value -1, which makes the
  2255. range open-ended. */
  2256. template<typename T>
  2257. inline typename if_nonpoly<T, bool>::type
  2258. endpoint_representable_p (const T &pos, const T &size)
  2259. {
  2260. return (!known_size_p (size)
  2261. || pos <= poly_coeff_traits<T>::max_value - size);
  2262. }
  2263. template<unsigned int N, typename C>
  2264. inline bool
  2265. endpoint_representable_p (const poly_int_pod<N, C> &pos,
  2266. const poly_int_pod<N, C> &size)
  2267. {
  2268. if (known_size_p (size))
  2269. for (unsigned int i = 0; i < N; ++i)
  2270. if (pos.coeffs[i] > poly_coeff_traits<C>::max_value - size.coeffs[i])
  2271. return false;
  2272. return true;
  2273. }
  2274. template<unsigned int N, typename C>
  2275. void
  2276. gt_ggc_mx (poly_int_pod<N, C> *)
  2277. {
  2278. }
  2279. template<unsigned int N, typename C>
  2280. void
  2281. gt_pch_nx (poly_int_pod<N, C> *)
  2282. {
  2283. }
  2284. template<unsigned int N, typename C>
  2285. void
  2286. gt_pch_nx (poly_int_pod<N, C> *, void (*) (void *, void *), void *)
  2287. {
  2288. }
  2289. #undef POLY_SET_COEFF
  2290. #undef POLY_INT_TYPE
  2291. #undef POLY_BINARY_COEFF
  2292. #undef CONST_CONST_RESULT
  2293. #undef POLY_CONST_RESULT
  2294. #undef CONST_POLY_RESULT
  2295. #undef POLY_POLY_RESULT
  2296. #undef POLY_CONST_COEFF
  2297. #undef CONST_POLY_COEFF
  2298. #undef POLY_POLY_COEFF
  2299. #endif