poly-int.h 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. /* Polynomial integer classes.
  2. Copyright (C) 2014-2019 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> class 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. class 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 a value that is known to be no greater than A and B. This
  1328. will be the greatest lower bound for some indeterminate values but
  1329. not necessarily for all. */
  1330. template<unsigned int N, typename Ca, typename Cb>
  1331. inline POLY_CONST_RESULT (N, Ca, Cb)
  1332. lower_bound (const poly_int_pod<N, Ca> &a, const Cb &b)
  1333. {
  1334. typedef POLY_CAST (Ca, Cb) NCa;
  1335. typedef POLY_CAST (Cb, Ca) NCb;
  1336. typedef POLY_INT_TYPE (Cb) ICb;
  1337. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1338. poly_int<N, C> r;
  1339. POLY_SET_COEFF (C, r, 0, MIN (NCa (a.coeffs[0]), NCb (b)));
  1340. if (N >= 2)
  1341. for (unsigned int i = 1; i < N; i++)
  1342. POLY_SET_COEFF (C, r, i, MIN (NCa (a.coeffs[i]), ICb (0)));
  1343. return r;
  1344. }
  1345. template<unsigned int N, typename Ca, typename Cb>
  1346. inline CONST_POLY_RESULT (N, Ca, Cb)
  1347. lower_bound (const Ca &a, const poly_int_pod<N, Cb> &b)
  1348. {
  1349. return lower_bound (b, a);
  1350. }
  1351. template<unsigned int N, typename Ca, typename Cb>
  1352. inline POLY_POLY_RESULT (N, Ca, Cb)
  1353. lower_bound (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1354. {
  1355. typedef POLY_CAST (Ca, Cb) NCa;
  1356. typedef POLY_CAST (Cb, Ca) NCb;
  1357. typedef POLY_POLY_COEFF (Ca, Cb) C;
  1358. poly_int<N, C> r;
  1359. for (unsigned int i = 0; i < N; i++)
  1360. POLY_SET_COEFF (C, r, i, MIN (NCa (a.coeffs[i]), NCb (b.coeffs[i])));
  1361. return r;
  1362. }
  1363. template<typename Ca, typename Cb>
  1364. inline CONST_CONST_RESULT (N, Ca, Cb)
  1365. lower_bound (const Ca &a, const Cb &b)
  1366. {
  1367. return a < b ? a : b;
  1368. }
  1369. /* Return a value that is known to be no less than A and B. This will
  1370. be the least upper bound for some indeterminate values but not
  1371. necessarily for all. */
  1372. template<unsigned int N, typename Ca, typename Cb>
  1373. inline POLY_CONST_RESULT (N, Ca, Cb)
  1374. upper_bound (const poly_int_pod<N, Ca> &a, const Cb &b)
  1375. {
  1376. typedef POLY_CAST (Ca, Cb) NCa;
  1377. typedef POLY_CAST (Cb, Ca) NCb;
  1378. typedef POLY_INT_TYPE (Cb) ICb;
  1379. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1380. poly_int<N, C> r;
  1381. POLY_SET_COEFF (C, r, 0, MAX (NCa (a.coeffs[0]), NCb (b)));
  1382. if (N >= 2)
  1383. for (unsigned int i = 1; i < N; i++)
  1384. POLY_SET_COEFF (C, r, i, MAX (NCa (a.coeffs[i]), ICb (0)));
  1385. return r;
  1386. }
  1387. template<unsigned int N, typename Ca, typename Cb>
  1388. inline CONST_POLY_RESULT (N, Ca, Cb)
  1389. upper_bound (const Ca &a, const poly_int_pod<N, Cb> &b)
  1390. {
  1391. return upper_bound (b, a);
  1392. }
  1393. template<unsigned int N, typename Ca, typename Cb>
  1394. inline POLY_POLY_RESULT (N, Ca, Cb)
  1395. upper_bound (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1396. {
  1397. typedef POLY_CAST (Ca, Cb) NCa;
  1398. typedef POLY_CAST (Cb, Ca) NCb;
  1399. typedef POLY_POLY_COEFF (Ca, Cb) C;
  1400. poly_int<N, C> r;
  1401. for (unsigned int i = 0; i < N; i++)
  1402. POLY_SET_COEFF (C, r, i, MAX (NCa (a.coeffs[i]), NCb (b.coeffs[i])));
  1403. return r;
  1404. }
  1405. /* Return the greatest common divisor of all nonzero coefficients, or zero
  1406. if all coefficients are zero. */
  1407. template<unsigned int N, typename Ca>
  1408. inline POLY_BINARY_COEFF (Ca, Ca)
  1409. coeff_gcd (const poly_int_pod<N, Ca> &a)
  1410. {
  1411. /* Find the first nonzero coefficient, stopping at 0 whatever happens. */
  1412. unsigned int i;
  1413. for (i = N - 1; i > 0; --i)
  1414. if (a.coeffs[i] != 0)
  1415. break;
  1416. typedef POLY_BINARY_COEFF (Ca, Ca) C;
  1417. C r = a.coeffs[i];
  1418. for (unsigned int j = 0; j < i; ++j)
  1419. if (a.coeffs[j] != 0)
  1420. r = gcd (r, C (a.coeffs[j]));
  1421. return r;
  1422. }
  1423. /* Return a value that is a multiple of both A and B. This will be the
  1424. least common multiple for some indeterminate values but necessarily
  1425. for all. */
  1426. template<unsigned int N, typename Ca, typename Cb>
  1427. POLY_CONST_RESULT (N, Ca, Cb)
  1428. common_multiple (const poly_int_pod<N, Ca> &a, Cb b)
  1429. {
  1430. POLY_BINARY_COEFF (Ca, Ca) xgcd = coeff_gcd (a);
  1431. return a * (least_common_multiple (xgcd, b) / xgcd);
  1432. }
  1433. template<unsigned int N, typename Ca, typename Cb>
  1434. inline CONST_POLY_RESULT (N, Ca, Cb)
  1435. common_multiple (const Ca &a, const poly_int_pod<N, Cb> &b)
  1436. {
  1437. return common_multiple (b, a);
  1438. }
  1439. /* Return a value that is a multiple of both A and B, asserting that
  1440. such a value exists. The result will be the least common multiple
  1441. for some indeterminate values but necessarily for all.
  1442. NOTE: When using this function, please add a comment above the call
  1443. explaining why we know the values have a common multiple (which might
  1444. for example be because we know A / B is rational). */
  1445. template<unsigned int N, typename Ca, typename Cb>
  1446. POLY_POLY_RESULT (N, Ca, Cb)
  1447. force_common_multiple (const poly_int_pod<N, Ca> &a,
  1448. const poly_int_pod<N, Cb> &b)
  1449. {
  1450. if (b.is_constant ())
  1451. return common_multiple (a, b.coeffs[0]);
  1452. if (a.is_constant ())
  1453. return common_multiple (a.coeffs[0], b);
  1454. typedef POLY_CAST (Ca, Cb) NCa;
  1455. typedef POLY_CAST (Cb, Ca) NCb;
  1456. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1457. typedef POLY_INT_TYPE (Ca) ICa;
  1458. for (unsigned int i = 1; i < N; ++i)
  1459. if (a.coeffs[i] != ICa (0))
  1460. {
  1461. C lcm = least_common_multiple (NCa (a.coeffs[i]), NCb (b.coeffs[i]));
  1462. C amul = lcm / a.coeffs[i];
  1463. C bmul = lcm / b.coeffs[i];
  1464. for (unsigned int j = 0; j < N; ++j)
  1465. gcc_checking_assert (a.coeffs[j] * amul == b.coeffs[j] * bmul);
  1466. return a * amul;
  1467. }
  1468. gcc_unreachable ();
  1469. }
  1470. /* Compare A and B for sorting purposes, returning -1 if A should come
  1471. before B, 0 if A and B are identical, and 1 if A should come after B.
  1472. This is a lexicographical compare of the coefficients in reverse order.
  1473. A consequence of this is that all constant sizes come before all
  1474. non-constant ones, regardless of magnitude (since a size is never
  1475. negative). This is what most callers want. For example, when laying
  1476. data out on the stack, it's better to keep all the constant-sized
  1477. data together so that it can be accessed as a constant offset from a
  1478. single base. */
  1479. template<unsigned int N, typename Ca, typename Cb>
  1480. inline int
  1481. compare_sizes_for_sort (const poly_int_pod<N, Ca> &a,
  1482. const poly_int_pod<N, Cb> &b)
  1483. {
  1484. for (unsigned int i = N; i-- > 0; )
  1485. if (a.coeffs[i] != b.coeffs[i])
  1486. return a.coeffs[i] < b.coeffs[i] ? -1 : 1;
  1487. return 0;
  1488. }
  1489. /* Return true if we can calculate VALUE & (ALIGN - 1) at compile time. */
  1490. template<unsigned int N, typename Ca, typename Cb>
  1491. inline bool
  1492. can_align_p (const poly_int_pod<N, Ca> &value, Cb align)
  1493. {
  1494. for (unsigned int i = 1; i < N; i++)
  1495. if ((value.coeffs[i] & (align - 1)) != 0)
  1496. return false;
  1497. return true;
  1498. }
  1499. /* Return true if we can align VALUE up to the smallest multiple of
  1500. ALIGN that is >= VALUE. Store the aligned value in *ALIGNED if so. */
  1501. template<unsigned int N, typename Ca, typename Cb>
  1502. inline bool
  1503. can_align_up (const poly_int_pod<N, Ca> &value, Cb align,
  1504. poly_int_pod<N, Ca> *aligned)
  1505. {
  1506. if (!can_align_p (value, align))
  1507. return false;
  1508. *aligned = value + (-value.coeffs[0] & (align - 1));
  1509. return true;
  1510. }
  1511. /* Return true if we can align VALUE down to the largest multiple of
  1512. ALIGN that is <= VALUE. Store the aligned value in *ALIGNED if so. */
  1513. template<unsigned int N, typename Ca, typename Cb>
  1514. inline bool
  1515. can_align_down (const poly_int_pod<N, Ca> &value, Cb align,
  1516. poly_int_pod<N, Ca> *aligned)
  1517. {
  1518. if (!can_align_p (value, align))
  1519. return false;
  1520. *aligned = value - (value.coeffs[0] & (align - 1));
  1521. return true;
  1522. }
  1523. /* Return true if we can align A and B up to the smallest multiples of
  1524. ALIGN that are >= A and B respectively, and if doing so gives the
  1525. same value. */
  1526. template<unsigned int N, typename Ca, typename Cb, typename Cc>
  1527. inline bool
  1528. known_equal_after_align_up (const poly_int_pod<N, Ca> &a,
  1529. const poly_int_pod<N, Cb> &b,
  1530. Cc align)
  1531. {
  1532. poly_int<N, Ca> aligned_a;
  1533. poly_int<N, Cb> aligned_b;
  1534. return (can_align_up (a, align, &aligned_a)
  1535. && can_align_up (b, align, &aligned_b)
  1536. && known_eq (aligned_a, aligned_b));
  1537. }
  1538. /* Return true if we can align A and B down to the largest multiples of
  1539. ALIGN that are <= A and B respectively, and if doing so gives the
  1540. same value. */
  1541. template<unsigned int N, typename Ca, typename Cb, typename Cc>
  1542. inline bool
  1543. known_equal_after_align_down (const poly_int_pod<N, Ca> &a,
  1544. const poly_int_pod<N, Cb> &b,
  1545. Cc align)
  1546. {
  1547. poly_int<N, Ca> aligned_a;
  1548. poly_int<N, Cb> aligned_b;
  1549. return (can_align_down (a, align, &aligned_a)
  1550. && can_align_down (b, align, &aligned_b)
  1551. && known_eq (aligned_a, aligned_b));
  1552. }
  1553. /* Assert that we can align VALUE to ALIGN at compile time and return
  1554. the smallest multiple of ALIGN that is >= VALUE.
  1555. NOTE: When using this function, please add a comment above the call
  1556. explaining why we know the non-constant coefficients must already
  1557. be a multiple of ALIGN. */
  1558. template<unsigned int N, typename Ca, typename Cb>
  1559. inline poly_int<N, Ca>
  1560. force_align_up (const poly_int_pod<N, Ca> &value, Cb align)
  1561. {
  1562. gcc_checking_assert (can_align_p (value, align));
  1563. return value + (-value.coeffs[0] & (align - 1));
  1564. }
  1565. /* Assert that we can align VALUE to ALIGN at compile time and return
  1566. the largest multiple of ALIGN that is <= VALUE.
  1567. NOTE: When using this function, please add a comment above the call
  1568. explaining why we know the non-constant coefficients must already
  1569. be a multiple of ALIGN. */
  1570. template<unsigned int N, typename Ca, typename Cb>
  1571. inline poly_int<N, Ca>
  1572. force_align_down (const poly_int_pod<N, Ca> &value, Cb align)
  1573. {
  1574. gcc_checking_assert (can_align_p (value, align));
  1575. return value - (value.coeffs[0] & (align - 1));
  1576. }
  1577. /* Return a value <= VALUE that is a multiple of ALIGN. It will be the
  1578. greatest such value for some indeterminate values but not necessarily
  1579. for all. */
  1580. template<unsigned int N, typename Ca, typename Cb>
  1581. inline poly_int<N, Ca>
  1582. aligned_lower_bound (const poly_int_pod<N, Ca> &value, Cb align)
  1583. {
  1584. poly_int<N, Ca> r;
  1585. for (unsigned int i = 0; i < N; i++)
  1586. /* This form copes correctly with more type combinations than
  1587. value.coeffs[i] & -align would. */
  1588. POLY_SET_COEFF (Ca, r, i, (value.coeffs[i]
  1589. - (value.coeffs[i] & (align - 1))));
  1590. return r;
  1591. }
  1592. /* Return a value >= VALUE that is a multiple of ALIGN. It will be the
  1593. least such value for some indeterminate values but not necessarily
  1594. for all. */
  1595. template<unsigned int N, typename Ca, typename Cb>
  1596. inline poly_int<N, Ca>
  1597. aligned_upper_bound (const poly_int_pod<N, Ca> &value, Cb align)
  1598. {
  1599. poly_int<N, Ca> r;
  1600. for (unsigned int i = 0; i < N; i++)
  1601. POLY_SET_COEFF (Ca, r, i, (value.coeffs[i]
  1602. + (-value.coeffs[i] & (align - 1))));
  1603. return r;
  1604. }
  1605. /* Assert that we can align VALUE to ALIGN at compile time. Align VALUE
  1606. down to the largest multiple of ALIGN that is <= VALUE, then divide by
  1607. ALIGN.
  1608. NOTE: When using this function, please add a comment above the call
  1609. explaining why we know the non-constant coefficients must already
  1610. be a multiple of ALIGN. */
  1611. template<unsigned int N, typename Ca, typename Cb>
  1612. inline poly_int<N, Ca>
  1613. force_align_down_and_div (const poly_int_pod<N, Ca> &value, Cb align)
  1614. {
  1615. gcc_checking_assert (can_align_p (value, align));
  1616. poly_int<N, Ca> r;
  1617. POLY_SET_COEFF (Ca, r, 0, ((value.coeffs[0]
  1618. - (value.coeffs[0] & (align - 1)))
  1619. / align));
  1620. if (N >= 2)
  1621. for (unsigned int i = 1; i < N; i++)
  1622. POLY_SET_COEFF (Ca, r, i, value.coeffs[i] / align);
  1623. return r;
  1624. }
  1625. /* Assert that we can align VALUE to ALIGN at compile time. Align VALUE
  1626. up to the smallest multiple of ALIGN that is >= VALUE, then divide by
  1627. ALIGN.
  1628. NOTE: When using this function, please add a comment above the call
  1629. explaining why we know the non-constant coefficients must already
  1630. be a multiple of ALIGN. */
  1631. template<unsigned int N, typename Ca, typename Cb>
  1632. inline poly_int<N, Ca>
  1633. force_align_up_and_div (const poly_int_pod<N, Ca> &value, Cb align)
  1634. {
  1635. gcc_checking_assert (can_align_p (value, align));
  1636. poly_int<N, Ca> r;
  1637. POLY_SET_COEFF (Ca, r, 0, ((value.coeffs[0]
  1638. + (-value.coeffs[0] & (align - 1)))
  1639. / align));
  1640. if (N >= 2)
  1641. for (unsigned int i = 1; i < N; i++)
  1642. POLY_SET_COEFF (Ca, r, i, value.coeffs[i] / align);
  1643. return r;
  1644. }
  1645. /* Return true if we know at compile time the difference between VALUE
  1646. and the equal or preceding multiple of ALIGN. Store the value in
  1647. *MISALIGN if so. */
  1648. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1649. inline bool
  1650. known_misalignment (const poly_int_pod<N, Ca> &value, Cb align, Cm *misalign)
  1651. {
  1652. gcc_checking_assert (align != 0);
  1653. if (!can_align_p (value, align))
  1654. return false;
  1655. *misalign = value.coeffs[0] & (align - 1);
  1656. return true;
  1657. }
  1658. /* Return X & (Y - 1), asserting that this value is known. Please add
  1659. an a comment above callers to this function to explain why the condition
  1660. is known to hold. */
  1661. template<unsigned int N, typename Ca, typename Cb>
  1662. inline POLY_BINARY_COEFF (Ca, Ca)
  1663. force_get_misalignment (const poly_int_pod<N, Ca> &a, Cb align)
  1664. {
  1665. gcc_checking_assert (can_align_p (a, align));
  1666. return a.coeffs[0] & (align - 1);
  1667. }
  1668. /* Return the maximum alignment that A is known to have. Return 0
  1669. if A is known to be zero. */
  1670. template<unsigned int N, typename Ca>
  1671. inline POLY_BINARY_COEFF (Ca, Ca)
  1672. known_alignment (const poly_int_pod<N, Ca> &a)
  1673. {
  1674. typedef POLY_BINARY_COEFF (Ca, Ca) C;
  1675. C r = a.coeffs[0];
  1676. for (unsigned int i = 1; i < N; ++i)
  1677. r |= a.coeffs[i];
  1678. return r & -r;
  1679. }
  1680. /* Return true if we can compute A | B at compile time, storing the
  1681. result in RES if so. */
  1682. template<unsigned int N, typename Ca, typename Cb, typename Cr>
  1683. inline typename if_nonpoly<Cb, bool>::type
  1684. can_ior_p (const poly_int_pod<N, Ca> &a, Cb b, Cr *result)
  1685. {
  1686. /* Coefficients 1 and above must be a multiple of something greater
  1687. than B. */
  1688. typedef POLY_INT_TYPE (Ca) int_type;
  1689. if (N >= 2)
  1690. for (unsigned int i = 1; i < N; i++)
  1691. if ((-(a.coeffs[i] & -a.coeffs[i]) & b) != int_type (0))
  1692. return false;
  1693. *result = a;
  1694. result->coeffs[0] |= b;
  1695. return true;
  1696. }
  1697. /* Return true if A is a constant multiple of B, storing the
  1698. multiple in *MULTIPLE if so. */
  1699. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1700. inline typename if_nonpoly<Cb, bool>::type
  1701. constant_multiple_p (const poly_int_pod<N, Ca> &a, Cb b, Cm *multiple)
  1702. {
  1703. typedef POLY_CAST (Ca, Cb) NCa;
  1704. typedef POLY_CAST (Cb, Ca) NCb;
  1705. /* Do the modulus before the constant check, to catch divide by
  1706. zero errors. */
  1707. if (NCa (a.coeffs[0]) % NCb (b) != 0 || !a.is_constant ())
  1708. return false;
  1709. *multiple = NCa (a.coeffs[0]) / NCb (b);
  1710. return true;
  1711. }
  1712. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1713. inline typename if_nonpoly<Ca, bool>::type
  1714. constant_multiple_p (Ca a, const poly_int_pod<N, Cb> &b, Cm *multiple)
  1715. {
  1716. typedef POLY_CAST (Ca, Cb) NCa;
  1717. typedef POLY_CAST (Cb, Ca) NCb;
  1718. typedef POLY_INT_TYPE (Ca) int_type;
  1719. /* Do the modulus before the constant check, to catch divide by
  1720. zero errors. */
  1721. if (NCa (a) % NCb (b.coeffs[0]) != 0
  1722. || (a != int_type (0) && !b.is_constant ()))
  1723. return false;
  1724. *multiple = NCa (a) / NCb (b.coeffs[0]);
  1725. return true;
  1726. }
  1727. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1728. inline bool
  1729. constant_multiple_p (const poly_int_pod<N, Ca> &a,
  1730. const poly_int_pod<N, Cb> &b, Cm *multiple)
  1731. {
  1732. typedef POLY_CAST (Ca, Cb) NCa;
  1733. typedef POLY_CAST (Cb, Ca) NCb;
  1734. typedef POLY_INT_TYPE (Ca) ICa;
  1735. typedef POLY_INT_TYPE (Cb) ICb;
  1736. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1737. if (NCa (a.coeffs[0]) % NCb (b.coeffs[0]) != 0)
  1738. return false;
  1739. C r = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1740. for (unsigned int i = 1; i < N; ++i)
  1741. if (b.coeffs[i] == ICb (0)
  1742. ? a.coeffs[i] != ICa (0)
  1743. : (NCa (a.coeffs[i]) % NCb (b.coeffs[i]) != 0
  1744. || NCa (a.coeffs[i]) / NCb (b.coeffs[i]) != r))
  1745. return false;
  1746. *multiple = r;
  1747. return true;
  1748. }
  1749. /* Return true if A is a multiple of B. */
  1750. template<typename Ca, typename Cb>
  1751. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1752. multiple_p (Ca a, Cb b)
  1753. {
  1754. return a % b == 0;
  1755. }
  1756. /* Return true if A is a (polynomial) multiple of B. */
  1757. template<unsigned int N, typename Ca, typename Cb>
  1758. inline typename if_nonpoly<Cb, bool>::type
  1759. multiple_p (const poly_int_pod<N, Ca> &a, Cb b)
  1760. {
  1761. for (unsigned int i = 0; i < N; ++i)
  1762. if (a.coeffs[i] % b != 0)
  1763. return false;
  1764. return true;
  1765. }
  1766. /* Return true if A is a (constant) multiple of B. */
  1767. template<unsigned int N, typename Ca, typename Cb>
  1768. inline typename if_nonpoly<Ca, bool>::type
  1769. multiple_p (Ca a, const poly_int_pod<N, Cb> &b)
  1770. {
  1771. typedef POLY_INT_TYPE (Ca) int_type;
  1772. /* Do the modulus before the constant check, to catch divide by
  1773. potential zeros. */
  1774. return a % b.coeffs[0] == 0 && (a == int_type (0) || b.is_constant ());
  1775. }
  1776. /* Return true if A is a (polynomial) multiple of B. This handles cases
  1777. where either B is constant or the multiple is constant. */
  1778. template<unsigned int N, typename Ca, typename Cb>
  1779. inline bool
  1780. multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1781. {
  1782. if (b.is_constant ())
  1783. return multiple_p (a, b.coeffs[0]);
  1784. POLY_BINARY_COEFF (Ca, Ca) tmp;
  1785. return constant_multiple_p (a, b, &tmp);
  1786. }
  1787. /* Return true if A is a (constant) multiple of B, storing the
  1788. multiple in *MULTIPLE if so. */
  1789. template<typename Ca, typename Cb, typename Cm>
  1790. inline typename if_nonpoly2<Ca, Cb, bool>::type
  1791. multiple_p (Ca a, Cb b, Cm *multiple)
  1792. {
  1793. if (a % b != 0)
  1794. return false;
  1795. *multiple = a / b;
  1796. return true;
  1797. }
  1798. /* Return true if A is a (polynomial) multiple of B, storing the
  1799. multiple in *MULTIPLE if so. */
  1800. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1801. inline typename if_nonpoly<Cb, bool>::type
  1802. multiple_p (const poly_int_pod<N, Ca> &a, Cb b, poly_int_pod<N, Cm> *multiple)
  1803. {
  1804. if (!multiple_p (a, b))
  1805. return false;
  1806. for (unsigned int i = 0; i < N; ++i)
  1807. multiple->coeffs[i] = a.coeffs[i] / b;
  1808. return true;
  1809. }
  1810. /* Return true if B is a constant and A is a (constant) multiple of B,
  1811. storing the multiple in *MULTIPLE if so. */
  1812. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1813. inline typename if_nonpoly<Ca, bool>::type
  1814. multiple_p (Ca a, const poly_int_pod<N, Cb> &b, Cm *multiple)
  1815. {
  1816. typedef POLY_CAST (Ca, Cb) NCa;
  1817. /* Do the modulus before the constant check, to catch divide by
  1818. potential zeros. */
  1819. if (a % b.coeffs[0] != 0 || (NCa (a) != 0 && !b.is_constant ()))
  1820. return false;
  1821. *multiple = a / b.coeffs[0];
  1822. return true;
  1823. }
  1824. /* Return true if A is a (polynomial) multiple of B, storing the
  1825. multiple in *MULTIPLE if so. This handles cases where either
  1826. B is constant or the multiple is constant. */
  1827. template<unsigned int N, typename Ca, typename Cb, typename Cm>
  1828. inline bool
  1829. multiple_p (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b,
  1830. poly_int_pod<N, Cm> *multiple)
  1831. {
  1832. if (b.is_constant ())
  1833. return multiple_p (a, b.coeffs[0], multiple);
  1834. return constant_multiple_p (a, b, multiple);
  1835. }
  1836. /* Return A / B, given that A is known to be a multiple of B. */
  1837. template<unsigned int N, typename Ca, typename Cb>
  1838. inline POLY_CONST_RESULT (N, Ca, Cb)
  1839. exact_div (const poly_int_pod<N, Ca> &a, Cb b)
  1840. {
  1841. typedef POLY_CONST_COEFF (Ca, Cb) C;
  1842. poly_int<N, C> r;
  1843. for (unsigned int i = 0; i < N; i++)
  1844. {
  1845. gcc_checking_assert (a.coeffs[i] % b == 0);
  1846. POLY_SET_COEFF (C, r, i, a.coeffs[i] / b);
  1847. }
  1848. return r;
  1849. }
  1850. /* Return A / B, given that A is known to be a multiple of B. */
  1851. template<unsigned int N, typename Ca, typename Cb>
  1852. inline POLY_POLY_RESULT (N, Ca, Cb)
  1853. exact_div (const poly_int_pod<N, Ca> &a, const poly_int_pod<N, Cb> &b)
  1854. {
  1855. if (b.is_constant ())
  1856. return exact_div (a, b.coeffs[0]);
  1857. typedef POLY_CAST (Ca, Cb) NCa;
  1858. typedef POLY_CAST (Cb, Ca) NCb;
  1859. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1860. typedef POLY_INT_TYPE (Cb) int_type;
  1861. gcc_checking_assert (a.coeffs[0] % b.coeffs[0] == 0);
  1862. C r = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1863. for (unsigned int i = 1; i < N; ++i)
  1864. gcc_checking_assert (b.coeffs[i] == int_type (0)
  1865. ? a.coeffs[i] == int_type (0)
  1866. : (a.coeffs[i] % b.coeffs[i] == 0
  1867. && NCa (a.coeffs[i]) / NCb (b.coeffs[i]) == r));
  1868. return r;
  1869. }
  1870. /* Return true if there is some constant Q and polynomial r such that:
  1871. (1) a = b * Q + r
  1872. (2) |b * Q| <= |a|
  1873. (3) |r| < |b|
  1874. Store the value Q in *QUOTIENT if so. */
  1875. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  1876. inline typename if_nonpoly2<Cb, Cq, bool>::type
  1877. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b, Cq *quotient)
  1878. {
  1879. typedef POLY_CAST (Ca, Cb) NCa;
  1880. typedef POLY_CAST (Cb, Ca) NCb;
  1881. /* Do the division before the constant check, to catch divide by
  1882. zero errors. */
  1883. Cq q = NCa (a.coeffs[0]) / NCb (b);
  1884. if (!a.is_constant ())
  1885. return false;
  1886. *quotient = q;
  1887. return true;
  1888. }
  1889. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  1890. inline typename if_nonpoly<Cq, bool>::type
  1891. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  1892. const poly_int_pod<N, Cb> &b,
  1893. Cq *quotient)
  1894. {
  1895. /* We can calculate Q from the case in which the indeterminates
  1896. are zero. */
  1897. typedef POLY_CAST (Ca, Cb) NCa;
  1898. typedef POLY_CAST (Cb, Ca) NCb;
  1899. typedef POLY_INT_TYPE (Ca) ICa;
  1900. typedef POLY_INT_TYPE (Cb) ICb;
  1901. typedef POLY_BINARY_COEFF (Ca, Cb) C;
  1902. C q = NCa (a.coeffs[0]) / NCb (b.coeffs[0]);
  1903. /* Check the other coefficients and record whether the division is exact.
  1904. The only difficult case is when it isn't. If we require a and b to
  1905. ordered wrt zero, there can be no two coefficients of the same value
  1906. that have opposite signs. This means that:
  1907. |a| = |a0| + |a1 * x1| + |a2 * x2| + ...
  1908. |b| = |b0| + |b1 * x1| + |b2 * x2| + ...
  1909. The Q we've just calculated guarantees:
  1910. |b0 * Q| <= |a0|
  1911. |a0 - b0 * Q| < |b0|
  1912. and so:
  1913. (2) |b * Q| <= |a|
  1914. is satisfied if:
  1915. |bi * xi * Q| <= |ai * xi|
  1916. for each i in [1, N]. This is trivially true when xi is zero.
  1917. When it isn't we need:
  1918. (2') |bi * Q| <= |ai|
  1919. r is calculated as:
  1920. r = r0 + r1 * x1 + r2 * x2 + ...
  1921. where ri = ai - bi * Q
  1922. Restricting to ordered a and b also guarantees that no two ris
  1923. have opposite signs, so we have:
  1924. |r| = |r0| + |r1 * x1| + |r2 * x2| + ...
  1925. We know from the calculation of Q that |r0| < |b0|, so:
  1926. (3) |r| < |b|
  1927. is satisfied if:
  1928. (3') |ai - bi * Q| <= |bi|
  1929. for each i in [1, N]. */
  1930. bool rem_p = NCa (a.coeffs[0]) % NCb (b.coeffs[0]) != 0;
  1931. for (unsigned int i = 1; i < N; ++i)
  1932. {
  1933. if (b.coeffs[i] == ICb (0))
  1934. {
  1935. /* For bi == 0 we simply need: (3') |ai| == 0. */
  1936. if (a.coeffs[i] != ICa (0))
  1937. return false;
  1938. }
  1939. else
  1940. {
  1941. if (q == 0)
  1942. {
  1943. /* For Q == 0 we simply need: (3') |ai| <= |bi|. */
  1944. if (a.coeffs[i] != ICa (0))
  1945. {
  1946. /* Use negative absolute to avoid overflow, i.e.
  1947. -|ai| >= -|bi|. */
  1948. C neg_abs_a = (a.coeffs[i] < 0 ? a.coeffs[i] : -a.coeffs[i]);
  1949. C neg_abs_b = (b.coeffs[i] < 0 ? b.coeffs[i] : -b.coeffs[i]);
  1950. if (neg_abs_a < neg_abs_b)
  1951. return false;
  1952. rem_p = true;
  1953. }
  1954. }
  1955. else
  1956. {
  1957. /* Otherwise just check for the case in which ai / bi == Q. */
  1958. if (NCa (a.coeffs[i]) / NCb (b.coeffs[i]) != q)
  1959. return false;
  1960. if (NCa (a.coeffs[i]) % NCb (b.coeffs[i]) != 0)
  1961. rem_p = true;
  1962. }
  1963. }
  1964. }
  1965. /* If the division isn't exact, require both values to be ordered wrt 0,
  1966. so that we can guarantee conditions (2) and (3) for all indeterminate
  1967. values. */
  1968. if (rem_p && (!ordered_p (a, ICa (0)) || !ordered_p (b, ICb (0))))
  1969. return false;
  1970. *quotient = q;
  1971. return true;
  1972. }
  1973. /* Likewise, but also store r in *REMAINDER. */
  1974. template<unsigned int N, typename Ca, typename Cb, typename Cq, typename Cr>
  1975. inline typename if_nonpoly<Cq, bool>::type
  1976. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  1977. const poly_int_pod<N, Cb> &b,
  1978. Cq *quotient, Cr *remainder)
  1979. {
  1980. if (!can_div_trunc_p (a, b, quotient))
  1981. return false;
  1982. *remainder = a - *quotient * b;
  1983. return true;
  1984. }
  1985. /* Return true if there is some polynomial q and constant R such that:
  1986. (1) a = B * q + R
  1987. (2) |B * q| <= |a|
  1988. (3) |R| < |B|
  1989. Store the value q in *QUOTIENT if so. */
  1990. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  1991. inline typename if_nonpoly<Cb, bool>::type
  1992. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b,
  1993. poly_int_pod<N, Cq> *quotient)
  1994. {
  1995. /* The remainder must be constant. */
  1996. for (unsigned int i = 1; i < N; ++i)
  1997. if (a.coeffs[i] % b != 0)
  1998. return false;
  1999. for (unsigned int i = 0; i < N; ++i)
  2000. quotient->coeffs[i] = a.coeffs[i] / b;
  2001. return true;
  2002. }
  2003. /* Likewise, but also store R in *REMAINDER. */
  2004. template<unsigned int N, typename Ca, typename Cb, typename Cq, typename Cr>
  2005. inline typename if_nonpoly<Cb, bool>::type
  2006. can_div_trunc_p (const poly_int_pod<N, Ca> &a, Cb b,
  2007. poly_int_pod<N, Cq> *quotient, Cr *remainder)
  2008. {
  2009. if (!can_div_trunc_p (a, b, quotient))
  2010. return false;
  2011. *remainder = a.coeffs[0] % b;
  2012. return true;
  2013. }
  2014. /* Return true if we can compute A / B at compile time, rounding towards zero.
  2015. Store the result in QUOTIENT if so.
  2016. This handles cases in which either B is constant or the result is
  2017. constant. */
  2018. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  2019. inline bool
  2020. can_div_trunc_p (const poly_int_pod<N, Ca> &a,
  2021. const poly_int_pod<N, Cb> &b,
  2022. poly_int_pod<N, Cq> *quotient)
  2023. {
  2024. if (b.is_constant ())
  2025. return can_div_trunc_p (a, b.coeffs[0], quotient);
  2026. if (!can_div_trunc_p (a, b, &quotient->coeffs[0]))
  2027. return false;
  2028. for (unsigned int i = 1; i < N; ++i)
  2029. quotient->coeffs[i] = 0;
  2030. return true;
  2031. }
  2032. /* Return true if there is some constant Q and polynomial r such that:
  2033. (1) a = b * Q + r
  2034. (2) |a| <= |b * Q|
  2035. (3) |r| < |b|
  2036. Store the value Q in *QUOTIENT if so. */
  2037. template<unsigned int N, typename Ca, typename Cb, typename Cq>
  2038. inline typename if_nonpoly<Cq, bool>::type
  2039. can_div_away_from_zero_p (const poly_int_pod<N, Ca> &a,
  2040. const poly_int_pod<N, Cb> &b,
  2041. Cq *quotient)
  2042. {
  2043. if (!can_div_trunc_p (a, b, quotient))
  2044. return false;
  2045. if (maybe_ne (*quotient * b, a))
  2046. *quotient += (*quotient < 0 ? -1 : 1);
  2047. return true;
  2048. }
  2049. /* Use print_dec to print VALUE to FILE, where SGN is the sign
  2050. of the values. */
  2051. template<unsigned int N, typename C>
  2052. void
  2053. print_dec (const poly_int_pod<N, C> &value, FILE *file, signop sgn)
  2054. {
  2055. if (value.is_constant ())
  2056. print_dec (value.coeffs[0], file, sgn);
  2057. else
  2058. {
  2059. fprintf (file, "[");
  2060. for (unsigned int i = 0; i < N; ++i)
  2061. {
  2062. print_dec (value.coeffs[i], file, sgn);
  2063. fputc (i == N - 1 ? ']' : ',', file);
  2064. }
  2065. }
  2066. }
  2067. /* Likewise without the signop argument, for coefficients that have an
  2068. inherent signedness. */
  2069. template<unsigned int N, typename C>
  2070. void
  2071. print_dec (const poly_int_pod<N, C> &value, FILE *file)
  2072. {
  2073. STATIC_ASSERT (poly_coeff_traits<C>::signedness >= 0);
  2074. print_dec (value, file,
  2075. poly_coeff_traits<C>::signedness ? SIGNED : UNSIGNED);
  2076. }
  2077. /* Use print_hex to print VALUE to FILE. */
  2078. template<unsigned int N, typename C>
  2079. void
  2080. print_hex (const poly_int_pod<N, C> &value, FILE *file)
  2081. {
  2082. if (value.is_constant ())
  2083. print_hex (value.coeffs[0], file);
  2084. else
  2085. {
  2086. fprintf (file, "[");
  2087. for (unsigned int i = 0; i < N; ++i)
  2088. {
  2089. print_hex (value.coeffs[i], file);
  2090. fputc (i == N - 1 ? ']' : ',', file);
  2091. }
  2092. }
  2093. }
  2094. /* Helper for calculating the distance between two points P1 and P2,
  2095. in cases where known_le (P1, P2). T1 and T2 are the types of the
  2096. two positions, in either order. The coefficients of P2 - P1 have
  2097. type unsigned HOST_WIDE_INT if the coefficients of both T1 and T2
  2098. have C++ primitive type, otherwise P2 - P1 has its usual
  2099. wide-int-based type.
  2100. The actual subtraction should look something like this:
  2101. typedef poly_span_traits<T1, T2> span_traits;
  2102. span_traits::cast (P2) - span_traits::cast (P1)
  2103. Applying the cast before the subtraction avoids undefined overflow
  2104. for signed T1 and T2.
  2105. The implementation of the cast tries to avoid unnecessary arithmetic
  2106. or copying. */
  2107. template<typename T1, typename T2,
  2108. typename Res = POLY_BINARY_COEFF (POLY_BINARY_COEFF (T1, T2),
  2109. unsigned HOST_WIDE_INT)>
  2110. struct poly_span_traits
  2111. {
  2112. template<typename T>
  2113. static const T &cast (const T &x) { return x; }
  2114. };
  2115. template<typename T1, typename T2>
  2116. struct poly_span_traits<T1, T2, unsigned HOST_WIDE_INT>
  2117. {
  2118. template<typename T>
  2119. static typename if_nonpoly<T, unsigned HOST_WIDE_INT>::type
  2120. cast (const T &x) { return x; }
  2121. template<unsigned int N, typename T>
  2122. static poly_int<N, unsigned HOST_WIDE_INT>
  2123. cast (const poly_int_pod<N, T> &x) { return x; }
  2124. };
  2125. /* Return true if SIZE represents a known size, assuming that all-ones
  2126. indicates an unknown size. */
  2127. template<typename T>
  2128. inline bool
  2129. known_size_p (const T &a)
  2130. {
  2131. return maybe_ne (a, POLY_INT_TYPE (T) (-1));
  2132. }
  2133. /* Return true if range [POS, POS + SIZE) might include VAL.
  2134. SIZE can be the special value -1, in which case the range is
  2135. open-ended. */
  2136. template<typename T1, typename T2, typename T3>
  2137. inline bool
  2138. maybe_in_range_p (const T1 &val, const T2 &pos, const T3 &size)
  2139. {
  2140. typedef poly_span_traits<T1, T2> start_span;
  2141. typedef poly_span_traits<T3, T3> size_span;
  2142. if (known_lt (val, pos))
  2143. return false;
  2144. if (!known_size_p (size))
  2145. return true;
  2146. if ((poly_int_traits<T1>::num_coeffs > 1
  2147. || poly_int_traits<T2>::num_coeffs > 1)
  2148. && maybe_lt (val, pos))
  2149. /* In this case we don't know whether VAL >= POS is true at compile
  2150. time, so we can't prove that VAL >= POS + SIZE. */
  2151. return true;
  2152. return maybe_lt (start_span::cast (val) - start_span::cast (pos),
  2153. size_span::cast (size));
  2154. }
  2155. /* Return true if range [POS, POS + SIZE) is known to include VAL.
  2156. SIZE can be the special value -1, in which case the range is
  2157. open-ended. */
  2158. template<typename T1, typename T2, typename T3>
  2159. inline bool
  2160. known_in_range_p (const T1 &val, const T2 &pos, const T3 &size)
  2161. {
  2162. typedef poly_span_traits<T1, T2> start_span;
  2163. typedef poly_span_traits<T3, T3> size_span;
  2164. return (known_size_p (size)
  2165. && known_ge (val, pos)
  2166. && known_lt (start_span::cast (val) - start_span::cast (pos),
  2167. size_span::cast (size)));
  2168. }
  2169. /* Return true if the two ranges [POS1, POS1 + SIZE1) and [POS2, POS2 + SIZE2)
  2170. might overlap. SIZE1 and/or SIZE2 can be the special value -1, in which
  2171. case the range is open-ended. */
  2172. template<typename T1, typename T2, typename T3, typename T4>
  2173. inline bool
  2174. ranges_maybe_overlap_p (const T1 &pos1, const T2 &size1,
  2175. const T3 &pos2, const T4 &size2)
  2176. {
  2177. if (maybe_in_range_p (pos2, pos1, size1))
  2178. return maybe_ne (size2, POLY_INT_TYPE (T4) (0));
  2179. if (maybe_in_range_p (pos1, pos2, size2))
  2180. return maybe_ne (size1, POLY_INT_TYPE (T2) (0));
  2181. return false;
  2182. }
  2183. /* Return true if the two ranges [POS1, POS1 + SIZE1) and [POS2, POS2 + SIZE2)
  2184. are known to overlap. SIZE1 and/or SIZE2 can be the special value -1,
  2185. in which case the range is open-ended. */
  2186. template<typename T1, typename T2, typename T3, typename T4>
  2187. inline bool
  2188. ranges_known_overlap_p (const T1 &pos1, const T2 &size1,
  2189. const T3 &pos2, const T4 &size2)
  2190. {
  2191. typedef poly_span_traits<T1, T3> start_span;
  2192. typedef poly_span_traits<T2, T2> size1_span;
  2193. typedef poly_span_traits<T4, T4> size2_span;
  2194. /* known_gt (POS1 + SIZE1, POS2) [infinite precision]
  2195. --> known_gt (SIZE1, POS2 - POS1) [infinite precision]
  2196. --> known_gt (SIZE1, POS2 - lower_bound (POS1, POS2)) [infinite precision]
  2197. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ always nonnegative
  2198. --> known_gt (SIZE1, span1::cast (POS2 - lower_bound (POS1, POS2))).
  2199. Using the saturating subtraction enforces that SIZE1 must be
  2200. nonzero, since known_gt (0, x) is false for all nonnegative x.
  2201. If POS2.coeff[I] < POS1.coeff[I] for some I > 0, increasing
  2202. indeterminate number I makes the unsaturated condition easier to
  2203. satisfy, so using a saturated coefficient of zero tests the case in
  2204. which the indeterminate is zero (the minimum value). */
  2205. return (known_size_p (size1)
  2206. && known_size_p (size2)
  2207. && known_lt (start_span::cast (pos2)
  2208. - start_span::cast (lower_bound (pos1, pos2)),
  2209. size1_span::cast (size1))
  2210. && known_lt (start_span::cast (pos1)
  2211. - start_span::cast (lower_bound (pos1, pos2)),
  2212. size2_span::cast (size2)));
  2213. }
  2214. /* Return true if range [POS1, POS1 + SIZE1) is known to be a subrange of
  2215. [POS2, POS2 + SIZE2). SIZE1 and/or SIZE2 can be the special value -1,
  2216. in which case the range is open-ended. */
  2217. template<typename T1, typename T2, typename T3, typename T4>
  2218. inline bool
  2219. known_subrange_p (const T1 &pos1, const T2 &size1,
  2220. const T3 &pos2, const T4 &size2)
  2221. {
  2222. typedef typename poly_int_traits<T2>::coeff_type C2;
  2223. typedef poly_span_traits<T1, T3> start_span;
  2224. typedef poly_span_traits<T2, T4> size_span;
  2225. return (known_gt (size1, POLY_INT_TYPE (T2) (0))
  2226. && (poly_coeff_traits<C2>::signedness > 0
  2227. || known_size_p (size1))
  2228. && known_size_p (size2)
  2229. && known_ge (pos1, pos2)
  2230. && known_le (size1, size2)
  2231. && known_le (start_span::cast (pos1) - start_span::cast (pos2),
  2232. size_span::cast (size2) - size_span::cast (size1)));
  2233. }
  2234. /* Return true if the endpoint of the range [POS, POS + SIZE) can be
  2235. stored in a T, or if SIZE is the special value -1, which makes the
  2236. range open-ended. */
  2237. template<typename T>
  2238. inline typename if_nonpoly<T, bool>::type
  2239. endpoint_representable_p (const T &pos, const T &size)
  2240. {
  2241. return (!known_size_p (size)
  2242. || pos <= poly_coeff_traits<T>::max_value - size);
  2243. }
  2244. template<unsigned int N, typename C>
  2245. inline bool
  2246. endpoint_representable_p (const poly_int_pod<N, C> &pos,
  2247. const poly_int_pod<N, C> &size)
  2248. {
  2249. if (known_size_p (size))
  2250. for (unsigned int i = 0; i < N; ++i)
  2251. if (pos.coeffs[i] > poly_coeff_traits<C>::max_value - size.coeffs[i])
  2252. return false;
  2253. return true;
  2254. }
  2255. template<unsigned int N, typename C>
  2256. void
  2257. gt_ggc_mx (poly_int_pod<N, C> *)
  2258. {
  2259. }
  2260. template<unsigned int N, typename C>
  2261. void
  2262. gt_pch_nx (poly_int_pod<N, C> *)
  2263. {
  2264. }
  2265. template<unsigned int N, typename C>
  2266. void
  2267. gt_pch_nx (poly_int_pod<N, C> *, void (*) (void *, void *), void *)
  2268. {
  2269. }
  2270. #undef POLY_SET_COEFF
  2271. #undef POLY_INT_TYPE
  2272. #undef POLY_BINARY_COEFF
  2273. #undef CONST_CONST_RESULT
  2274. #undef POLY_CONST_RESULT
  2275. #undef CONST_POLY_RESULT
  2276. #undef POLY_POLY_RESULT
  2277. #undef POLY_CONST_COEFF
  2278. #undef CONST_POLY_COEFF
  2279. #undef POLY_POLY_COEFF
  2280. #endif