wide-int.h 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183
  1. /* Operations with very long integers. -*- C++ -*-
  2. Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY 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. #ifndef WIDE_INT_H
  16. #define WIDE_INT_H
  17. /* wide-int.[cc|h] implements a class that efficiently performs
  18. mathematical operations on finite precision integers. wide_ints
  19. are designed to be transient - they are not for long term storage
  20. of values. There is tight integration between wide_ints and the
  21. other longer storage GCC representations (rtl and tree).
  22. The actual precision of a wide_int depends on the flavor. There
  23. are three predefined flavors:
  24. 1) wide_int (the default). This flavor does the math in the
  25. precision of its input arguments. It is assumed (and checked)
  26. that the precisions of the operands and results are consistent.
  27. This is the most efficient flavor. It is not possible to examine
  28. bits above the precision that has been specified. Because of
  29. this, the default flavor has semantics that are simple to
  30. understand and in general model the underlying hardware that the
  31. compiler is targetted for.
  32. This flavor must be used at the RTL level of gcc because there
  33. is, in general, not enough information in the RTL representation
  34. to extend a value beyond the precision specified in the mode.
  35. This flavor should also be used at the TREE and GIMPLE levels of
  36. the compiler except for the circumstances described in the
  37. descriptions of the other two flavors.
  38. The default wide_int representation does not contain any
  39. information inherent about signedness of the represented value,
  40. so it can be used to represent both signed and unsigned numbers.
  41. For operations where the results depend on signedness (full width
  42. multiply, division, shifts, comparisons, and operations that need
  43. overflow detected), the signedness must be specified separately.
  44. 2) offset_int. This is a fixed size representation that is
  45. guaranteed to be large enough to compute any bit or byte sized
  46. address calculation on the target. Currently the value is 64 + 4
  47. bits rounded up to the next number even multiple of
  48. HOST_BITS_PER_WIDE_INT (but this can be changed when the first
  49. port needs more than 64 bits for the size of a pointer).
  50. This flavor can be used for all address math on the target. In
  51. this representation, the values are sign or zero extended based
  52. on their input types to the internal precision. All math is done
  53. in this precision and then the values are truncated to fit in the
  54. result type. Unlike most gimple or rtl intermediate code, it is
  55. not useful to perform the address arithmetic at the same
  56. precision in which the operands are represented because there has
  57. been no effort by the front ends to convert most addressing
  58. arithmetic to canonical types.
  59. 3) widest_int. This representation is an approximation of
  60. infinite precision math. However, it is not really infinite
  61. precision math as in the GMP library. It is really finite
  62. precision math where the precision is 4 times the size of the
  63. largest integer that the target port can represent.
  64. widest_int is supposed to be wider than any number that it needs to
  65. store, meaning that there is always at least one leading sign bit.
  66. All widest_int values are therefore signed.
  67. There are several places in the GCC where this should/must be used:
  68. * Code that does induction variable optimizations. This code
  69. works with induction variables of many different types at the
  70. same time. Because of this, it ends up doing many different
  71. calculations where the operands are not compatible types. The
  72. widest_int makes this easy, because it provides a field where
  73. nothing is lost when converting from any variable,
  74. * There are a small number of passes that currently use the
  75. widest_int that should use the default. These should be
  76. changed.
  77. There are surprising features of offset_int and widest_int
  78. that the users should be careful about:
  79. 1) Shifts and rotations are just weird. You have to specify a
  80. precision in which the shift or rotate is to happen in. The bits
  81. above this precision are zeroed. While this is what you
  82. want, it is clearly non obvious.
  83. 2) Larger precision math sometimes does not produce the same
  84. answer as would be expected for doing the math at the proper
  85. precision. In particular, a multiply followed by a divide will
  86. produce a different answer if the first product is larger than
  87. what can be represented in the input precision.
  88. The offset_int and the widest_int flavors are more expensive
  89. than the default wide int, so in addition to the caveats with these
  90. two, the default is the prefered representation.
  91. All three flavors of wide_int are represented as a vector of
  92. HOST_WIDE_INTs. The default and widest_int vectors contain enough elements
  93. to hold a value of MAX_BITSIZE_MODE_ANY_INT bits. offset_int contains only
  94. enough elements to hold ADDR_MAX_PRECISION bits. The values are stored
  95. in the vector with the least significant HOST_BITS_PER_WIDE_INT bits
  96. in element 0.
  97. The default wide_int contains three fields: the vector (VAL),
  98. the precision and a length (LEN). The length is the number of HWIs
  99. needed to represent the value. widest_int and offset_int have a
  100. constant precision that cannot be changed, so they only store the
  101. VAL and LEN fields.
  102. Since most integers used in a compiler are small values, it is
  103. generally profitable to use a representation of the value that is
  104. as small as possible. LEN is used to indicate the number of
  105. elements of the vector that are in use. The numbers are stored as
  106. sign extended numbers as a means of compression. Leading
  107. HOST_WIDE_INTs that contain strings of either -1 or 0 are removed
  108. as long as they can be reconstructed from the top bit that is being
  109. represented.
  110. The precision and length of a wide_int are always greater than 0.
  111. Any bits in a wide_int above the precision are sign-extended from the
  112. most significant bit. For example, a 4-bit value 0x8 is represented as
  113. VAL = { 0xf...fff8 }. However, as an optimization, we allow other integer
  114. constants to be represented with undefined bits above the precision.
  115. This allows INTEGER_CSTs to be pre-extended according to TYPE_SIGN,
  116. so that the INTEGER_CST representation can be used both in TYPE_PRECISION
  117. and in wider precisions.
  118. There are constructors to create the various forms of wide_int from
  119. trees, rtl and constants. For trees you can simply say:
  120. tree t = ...;
  121. wide_int x = t;
  122. However, a little more syntax is required for rtl constants since
  123. they do not have an explicit precision. To make an rtl into a
  124. wide_int, you have to pair it with a mode. The canonical way to do
  125. this is with std::make_pair as in:
  126. rtx r = ...
  127. wide_int x = std::make_pair (r, mode);
  128. Similarly, a wide_int can only be constructed from a host value if
  129. the target precision is given explicitly, such as in:
  130. wide_int x = wi::shwi (c, prec); // sign-extend C if necessary
  131. wide_int y = wi::uhwi (c, prec); // zero-extend C if necessary
  132. However, offset_int and widest_int have an inherent precision and so
  133. can be initialized directly from a host value:
  134. offset_int x = (int) c; // sign-extend C
  135. widest_int x = (unsigned int) c; // zero-extend C
  136. It is also possible to do arithmetic directly on trees, rtxes and
  137. constants. For example:
  138. wi::add (t1, t2); // add equal-sized INTEGER_CSTs t1 and t2
  139. wi::add (t1, 1); // add 1 to INTEGER_CST t1
  140. wi::add (r1, r2); // add equal-sized rtx constants r1 and r2
  141. wi::lshift (1, 100); // 1 << 100 as a widest_int
  142. Many binary operations place restrictions on the combinations of inputs,
  143. using the following rules:
  144. - {tree, rtx, wide_int} op {tree, rtx, wide_int} -> wide_int
  145. The inputs must be the same precision. The result is a wide_int
  146. of the same precision
  147. - {tree, rtx, wide_int} op (un)signed HOST_WIDE_INT -> wide_int
  148. (un)signed HOST_WIDE_INT op {tree, rtx, wide_int} -> wide_int
  149. The HOST_WIDE_INT is extended or truncated to the precision of
  150. the other input. The result is a wide_int of the same precision
  151. as that input.
  152. - (un)signed HOST_WIDE_INT op (un)signed HOST_WIDE_INT -> widest_int
  153. The inputs are extended to widest_int precision and produce a
  154. widest_int result.
  155. - offset_int op offset_int -> offset_int
  156. offset_int op (un)signed HOST_WIDE_INT -> offset_int
  157. (un)signed HOST_WIDE_INT op offset_int -> offset_int
  158. - widest_int op widest_int -> widest_int
  159. widest_int op (un)signed HOST_WIDE_INT -> widest_int
  160. (un)signed HOST_WIDE_INT op widest_int -> widest_int
  161. Other combinations like:
  162. - widest_int op offset_int and
  163. - wide_int op offset_int
  164. are not allowed. The inputs should instead be extended or truncated
  165. so that they match.
  166. The inputs to comparison functions like wi::eq_p and wi::lts_p
  167. follow the same compatibility rules, although their return types
  168. are different. Unary functions on X produce the same result as
  169. a binary operation X + X. Shift functions X op Y also produce
  170. the same result as X + X; the precision of the shift amount Y
  171. can be arbitrarily different from X. */
  172. #include "system.h"
  173. #include "hwint.h"
  174. #include "signop.h"
  175. #include "insn-modes.h"
  176. /* The MAX_BITSIZE_MODE_ANY_INT is automatically generated by a very
  177. early examination of the target's mode file. The WIDE_INT_MAX_ELTS
  178. can accomodate at least 1 more bit so that unsigned numbers of that
  179. mode can be represented as a signed value. Note that it is still
  180. possible to create fixed_wide_ints that have precisions greater than
  181. MAX_BITSIZE_MODE_ANY_INT. This can be useful when representing a
  182. double-width multiplication result, for example. */
  183. #define WIDE_INT_MAX_ELTS \
  184. ((MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT) / HOST_BITS_PER_WIDE_INT)
  185. #define WIDE_INT_MAX_PRECISION (WIDE_INT_MAX_ELTS * HOST_BITS_PER_WIDE_INT)
  186. /* This is the max size of any pointer on any machine. It does not
  187. seem to be as easy to sniff this out of the machine description as
  188. it is for MAX_BITSIZE_MODE_ANY_INT since targets may support
  189. multiple address sizes and may have different address sizes for
  190. different address spaces. However, currently the largest pointer
  191. on any platform is 64 bits. When that changes, then it is likely
  192. that a target hook should be defined so that targets can make this
  193. value larger for those targets. */
  194. #define ADDR_MAX_BITSIZE 64
  195. /* This is the internal precision used when doing any address
  196. arithmetic. The '4' is really 3 + 1. Three of the bits are for
  197. the number of extra bits needed to do bit addresses and the other bit
  198. is to allow everything to be signed without loosing any precision.
  199. Then everything is rounded up to the next HWI for efficiency. */
  200. #define ADDR_MAX_PRECISION \
  201. ((ADDR_MAX_BITSIZE + 4 + HOST_BITS_PER_WIDE_INT - 1) \
  202. & ~(HOST_BITS_PER_WIDE_INT - 1))
  203. /* The number of HWIs needed to store an offset_int. */
  204. #define OFFSET_INT_ELTS (ADDR_MAX_PRECISION / HOST_BITS_PER_WIDE_INT)
  205. /* The type of result produced by a binary operation on types T1 and T2.
  206. Defined purely for brevity. */
  207. #define WI_BINARY_RESULT(T1, T2) \
  208. typename wi::binary_traits <T1, T2>::result_type
  209. /* The type of result produced by a unary operation on type T. */
  210. #define WI_UNARY_RESULT(T) \
  211. typename wi::unary_traits <T>::result_type
  212. /* Define a variable RESULT to hold the result of a binary operation on
  213. X and Y, which have types T1 and T2 respectively. Define VAL to
  214. point to the blocks of RESULT. Once the user of the macro has
  215. filled in VAL, it should call RESULT.set_len to set the number
  216. of initialized blocks. */
  217. #define WI_BINARY_RESULT_VAR(RESULT, VAL, T1, X, T2, Y) \
  218. WI_BINARY_RESULT (T1, T2) RESULT = \
  219. wi::int_traits <WI_BINARY_RESULT (T1, T2)>::get_binary_result (X, Y); \
  220. HOST_WIDE_INT *VAL = RESULT.write_val ()
  221. /* Similar for the result of a unary operation on X, which has type T. */
  222. #define WI_UNARY_RESULT_VAR(RESULT, VAL, T, X) \
  223. WI_UNARY_RESULT (T) RESULT = \
  224. wi::int_traits <WI_UNARY_RESULT (T)>::get_binary_result (X, X); \
  225. HOST_WIDE_INT *VAL = RESULT.write_val ()
  226. template <typename T> class generic_wide_int;
  227. template <int N> struct fixed_wide_int_storage;
  228. class wide_int_storage;
  229. /* An N-bit integer. Until we can use typedef templates, use this instead. */
  230. #define FIXED_WIDE_INT(N) \
  231. generic_wide_int < fixed_wide_int_storage <N> >
  232. typedef generic_wide_int <wide_int_storage> wide_int;
  233. typedef FIXED_WIDE_INT (ADDR_MAX_PRECISION) offset_int;
  234. typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION) widest_int;
  235. template <bool SE>
  236. struct wide_int_ref_storage;
  237. typedef generic_wide_int <wide_int_ref_storage <false> > wide_int_ref;
  238. /* This can be used instead of wide_int_ref if the referenced value is
  239. known to have type T. It carries across properties of T's representation,
  240. such as whether excess upper bits in a HWI are defined, and can therefore
  241. help avoid redundant work.
  242. The macro could be replaced with a template typedef, once we're able
  243. to use those. */
  244. #define WIDE_INT_REF_FOR(T) \
  245. generic_wide_int \
  246. <wide_int_ref_storage <wi::int_traits <T>::is_sign_extended> >
  247. namespace wi
  248. {
  249. /* Classifies an integer based on its precision. */
  250. enum precision_type {
  251. /* The integer has both a precision and defined signedness. This allows
  252. the integer to be converted to any width, since we know whether to fill
  253. any extra bits with zeros or signs. */
  254. FLEXIBLE_PRECISION,
  255. /* The integer has a variable precision but no defined signedness. */
  256. VAR_PRECISION,
  257. /* The integer has a constant precision (known at GCC compile time)
  258. but no defined signedness. */
  259. CONST_PRECISION
  260. };
  261. /* This class, which has no default implementation, is expected to
  262. provide the following members:
  263. static const enum precision_type precision_type;
  264. Classifies the type of T.
  265. static const unsigned int precision;
  266. Only defined if precision_type == CONST_PRECISION. Specifies the
  267. precision of all integers of type T.
  268. static const bool host_dependent_precision;
  269. True if the precision of T depends (or can depend) on the host.
  270. static unsigned int get_precision (const T &x)
  271. Return the number of bits in X.
  272. static wi::storage_ref *decompose (HOST_WIDE_INT *scratch,
  273. unsigned int precision, const T &x)
  274. Decompose X as a PRECISION-bit integer, returning the associated
  275. wi::storage_ref. SCRATCH is available as scratch space if needed.
  276. The routine should assert that PRECISION is acceptable. */
  277. template <typename T> struct int_traits;
  278. /* This class provides a single type, result_type, which specifies the
  279. type of integer produced by a binary operation whose inputs have
  280. types T1 and T2. The definition should be symmetric. */
  281. template <typename T1, typename T2,
  282. enum precision_type P1 = int_traits <T1>::precision_type,
  283. enum precision_type P2 = int_traits <T2>::precision_type>
  284. struct binary_traits;
  285. /* The result of a unary operation on T is the same as the result of
  286. a binary operation on two values of type T. */
  287. template <typename T>
  288. struct unary_traits : public binary_traits <T, T> {};
  289. /* Specify the result type for each supported combination of binary
  290. inputs. Note that CONST_PRECISION and VAR_PRECISION cannot be
  291. mixed, in order to give stronger type checking. When both inputs
  292. are CONST_PRECISION, they must have the same precision. */
  293. template <typename T1, typename T2>
  294. struct binary_traits <T1, T2, FLEXIBLE_PRECISION, FLEXIBLE_PRECISION>
  295. {
  296. typedef widest_int result_type;
  297. };
  298. template <typename T1, typename T2>
  299. struct binary_traits <T1, T2, FLEXIBLE_PRECISION, VAR_PRECISION>
  300. {
  301. typedef wide_int result_type;
  302. };
  303. template <typename T1, typename T2>
  304. struct binary_traits <T1, T2, FLEXIBLE_PRECISION, CONST_PRECISION>
  305. {
  306. /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
  307. so as not to confuse gengtype. */
  308. typedef generic_wide_int < fixed_wide_int_storage
  309. <int_traits <T2>::precision> > result_type;
  310. };
  311. template <typename T1, typename T2>
  312. struct binary_traits <T1, T2, VAR_PRECISION, FLEXIBLE_PRECISION>
  313. {
  314. typedef wide_int result_type;
  315. };
  316. template <typename T1, typename T2>
  317. struct binary_traits <T1, T2, CONST_PRECISION, FLEXIBLE_PRECISION>
  318. {
  319. /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
  320. so as not to confuse gengtype. */
  321. typedef generic_wide_int < fixed_wide_int_storage
  322. <int_traits <T1>::precision> > result_type;
  323. };
  324. template <typename T1, typename T2>
  325. struct binary_traits <T1, T2, CONST_PRECISION, CONST_PRECISION>
  326. {
  327. /* Spelled out explicitly (rather than through FIXED_WIDE_INT)
  328. so as not to confuse gengtype. */
  329. STATIC_ASSERT (int_traits <T1>::precision == int_traits <T2>::precision);
  330. typedef generic_wide_int < fixed_wide_int_storage
  331. <int_traits <T1>::precision> > result_type;
  332. };
  333. template <typename T1, typename T2>
  334. struct binary_traits <T1, T2, VAR_PRECISION, VAR_PRECISION>
  335. {
  336. typedef wide_int result_type;
  337. };
  338. }
  339. /* Public functions for querying and operating on integers. */
  340. namespace wi
  341. {
  342. template <typename T>
  343. unsigned int get_precision (const T &);
  344. template <typename T1, typename T2>
  345. unsigned int get_binary_precision (const T1 &, const T2 &);
  346. template <typename T1, typename T2>
  347. void copy (T1 &, const T2 &);
  348. #define UNARY_PREDICATE \
  349. template <typename T> bool
  350. #define UNARY_FUNCTION \
  351. template <typename T> WI_UNARY_RESULT (T)
  352. #define BINARY_PREDICATE \
  353. template <typename T1, typename T2> bool
  354. #define BINARY_FUNCTION \
  355. template <typename T1, typename T2> WI_BINARY_RESULT (T1, T2)
  356. #define SHIFT_FUNCTION \
  357. template <typename T1, typename T2> WI_UNARY_RESULT (T1)
  358. UNARY_PREDICATE fits_shwi_p (const T &);
  359. UNARY_PREDICATE fits_uhwi_p (const T &);
  360. UNARY_PREDICATE neg_p (const T &, signop = SIGNED);
  361. template <typename T>
  362. HOST_WIDE_INT sign_mask (const T &);
  363. BINARY_PREDICATE eq_p (const T1 &, const T2 &);
  364. BINARY_PREDICATE ne_p (const T1 &, const T2 &);
  365. BINARY_PREDICATE lt_p (const T1 &, const T2 &, signop);
  366. BINARY_PREDICATE lts_p (const T1 &, const T2 &);
  367. BINARY_PREDICATE ltu_p (const T1 &, const T2 &);
  368. BINARY_PREDICATE le_p (const T1 &, const T2 &, signop);
  369. BINARY_PREDICATE les_p (const T1 &, const T2 &);
  370. BINARY_PREDICATE leu_p (const T1 &, const T2 &);
  371. BINARY_PREDICATE gt_p (const T1 &, const T2 &, signop);
  372. BINARY_PREDICATE gts_p (const T1 &, const T2 &);
  373. BINARY_PREDICATE gtu_p (const T1 &, const T2 &);
  374. BINARY_PREDICATE ge_p (const T1 &, const T2 &, signop);
  375. BINARY_PREDICATE ges_p (const T1 &, const T2 &);
  376. BINARY_PREDICATE geu_p (const T1 &, const T2 &);
  377. template <typename T1, typename T2>
  378. int cmp (const T1 &, const T2 &, signop);
  379. template <typename T1, typename T2>
  380. int cmps (const T1 &, const T2 &);
  381. template <typename T1, typename T2>
  382. int cmpu (const T1 &, const T2 &);
  383. UNARY_FUNCTION bit_not (const T &);
  384. UNARY_FUNCTION neg (const T &);
  385. UNARY_FUNCTION neg (const T &, bool *);
  386. UNARY_FUNCTION abs (const T &);
  387. UNARY_FUNCTION ext (const T &, unsigned int, signop);
  388. UNARY_FUNCTION sext (const T &, unsigned int);
  389. UNARY_FUNCTION zext (const T &, unsigned int);
  390. UNARY_FUNCTION set_bit (const T &, unsigned int);
  391. BINARY_FUNCTION min (const T1 &, const T2 &, signop);
  392. BINARY_FUNCTION smin (const T1 &, const T2 &);
  393. BINARY_FUNCTION umin (const T1 &, const T2 &);
  394. BINARY_FUNCTION max (const T1 &, const T2 &, signop);
  395. BINARY_FUNCTION smax (const T1 &, const T2 &);
  396. BINARY_FUNCTION umax (const T1 &, const T2 &);
  397. BINARY_FUNCTION bit_and (const T1 &, const T2 &);
  398. BINARY_FUNCTION bit_and_not (const T1 &, const T2 &);
  399. BINARY_FUNCTION bit_or (const T1 &, const T2 &);
  400. BINARY_FUNCTION bit_or_not (const T1 &, const T2 &);
  401. BINARY_FUNCTION bit_xor (const T1 &, const T2 &);
  402. BINARY_FUNCTION add (const T1 &, const T2 &);
  403. BINARY_FUNCTION add (const T1 &, const T2 &, signop, bool *);
  404. BINARY_FUNCTION sub (const T1 &, const T2 &);
  405. BINARY_FUNCTION sub (const T1 &, const T2 &, signop, bool *);
  406. BINARY_FUNCTION mul (const T1 &, const T2 &);
  407. BINARY_FUNCTION mul (const T1 &, const T2 &, signop, bool *);
  408. BINARY_FUNCTION smul (const T1 &, const T2 &, bool *);
  409. BINARY_FUNCTION umul (const T1 &, const T2 &, bool *);
  410. BINARY_FUNCTION mul_high (const T1 &, const T2 &, signop);
  411. BINARY_FUNCTION div_trunc (const T1 &, const T2 &, signop, bool * = 0);
  412. BINARY_FUNCTION sdiv_trunc (const T1 &, const T2 &);
  413. BINARY_FUNCTION udiv_trunc (const T1 &, const T2 &);
  414. BINARY_FUNCTION div_floor (const T1 &, const T2 &, signop, bool * = 0);
  415. BINARY_FUNCTION udiv_floor (const T1 &, const T2 &);
  416. BINARY_FUNCTION sdiv_floor (const T1 &, const T2 &);
  417. BINARY_FUNCTION div_ceil (const T1 &, const T2 &, signop, bool * = 0);
  418. BINARY_FUNCTION div_round (const T1 &, const T2 &, signop, bool * = 0);
  419. BINARY_FUNCTION divmod_trunc (const T1 &, const T2 &, signop,
  420. WI_BINARY_RESULT (T1, T2) *);
  421. BINARY_FUNCTION mod_trunc (const T1 &, const T2 &, signop, bool * = 0);
  422. BINARY_FUNCTION smod_trunc (const T1 &, const T2 &);
  423. BINARY_FUNCTION umod_trunc (const T1 &, const T2 &);
  424. BINARY_FUNCTION mod_floor (const T1 &, const T2 &, signop, bool * = 0);
  425. BINARY_FUNCTION umod_floor (const T1 &, const T2 &);
  426. BINARY_FUNCTION mod_ceil (const T1 &, const T2 &, signop, bool * = 0);
  427. BINARY_FUNCTION mod_round (const T1 &, const T2 &, signop, bool * = 0);
  428. template <typename T1, typename T2>
  429. bool multiple_of_p (const T1 &, const T2 &, signop);
  430. template <typename T1, typename T2>
  431. bool multiple_of_p (const T1 &, const T2 &, signop,
  432. WI_BINARY_RESULT (T1, T2) *);
  433. SHIFT_FUNCTION lshift (const T1 &, const T2 &);
  434. SHIFT_FUNCTION lrshift (const T1 &, const T2 &);
  435. SHIFT_FUNCTION arshift (const T1 &, const T2 &);
  436. SHIFT_FUNCTION rshift (const T1 &, const T2 &, signop sgn);
  437. SHIFT_FUNCTION lrotate (const T1 &, const T2 &, unsigned int = 0);
  438. SHIFT_FUNCTION rrotate (const T1 &, const T2 &, unsigned int = 0);
  439. #undef SHIFT_FUNCTION
  440. #undef BINARY_PREDICATE
  441. #undef BINARY_FUNCTION
  442. #undef UNARY_PREDICATE
  443. #undef UNARY_FUNCTION
  444. bool only_sign_bit_p (const wide_int_ref &, unsigned int);
  445. bool only_sign_bit_p (const wide_int_ref &);
  446. int clz (const wide_int_ref &);
  447. int clrsb (const wide_int_ref &);
  448. int ctz (const wide_int_ref &);
  449. int exact_log2 (const wide_int_ref &);
  450. int floor_log2 (const wide_int_ref &);
  451. int ffs (const wide_int_ref &);
  452. int popcount (const wide_int_ref &);
  453. int parity (const wide_int_ref &);
  454. template <typename T>
  455. unsigned HOST_WIDE_INT extract_uhwi (const T &, unsigned int, unsigned int);
  456. template <typename T>
  457. unsigned int min_precision (const T &, signop);
  458. }
  459. namespace wi
  460. {
  461. /* Contains the components of a decomposed integer for easy, direct
  462. access. */
  463. struct storage_ref
  464. {
  465. storage_ref (const HOST_WIDE_INT *, unsigned int, unsigned int);
  466. const HOST_WIDE_INT *val;
  467. unsigned int len;
  468. unsigned int precision;
  469. /* Provide enough trappings for this class to act as storage for
  470. generic_wide_int. */
  471. unsigned int get_len () const;
  472. unsigned int get_precision () const;
  473. const HOST_WIDE_INT *get_val () const;
  474. };
  475. }
  476. inline::wi::storage_ref::storage_ref (const HOST_WIDE_INT *val_in,
  477. unsigned int len_in,
  478. unsigned int precision_in)
  479. : val (val_in), len (len_in), precision (precision_in)
  480. {
  481. }
  482. inline unsigned int
  483. wi::storage_ref::get_len () const
  484. {
  485. return len;
  486. }
  487. inline unsigned int
  488. wi::storage_ref::get_precision () const
  489. {
  490. return precision;
  491. }
  492. inline const HOST_WIDE_INT *
  493. wi::storage_ref::get_val () const
  494. {
  495. return val;
  496. }
  497. /* This class defines an integer type using the storage provided by the
  498. template argument. The storage class must provide the following
  499. functions:
  500. unsigned int get_precision () const
  501. Return the number of bits in the integer.
  502. HOST_WIDE_INT *get_val () const
  503. Return a pointer to the array of blocks that encodes the integer.
  504. unsigned int get_len () const
  505. Return the number of blocks in get_val (). If this is smaller
  506. than the number of blocks implied by get_precision (), the
  507. remaining blocks are sign extensions of block get_len () - 1.
  508. Although not required by generic_wide_int itself, writable storage
  509. classes can also provide the following functions:
  510. HOST_WIDE_INT *write_val ()
  511. Get a modifiable version of get_val ()
  512. unsigned int set_len (unsigned int len)
  513. Set the value returned by get_len () to LEN. */
  514. template <typename storage>
  515. class GTY(()) generic_wide_int : public storage
  516. {
  517. public:
  518. generic_wide_int ();
  519. template <typename T>
  520. generic_wide_int (const T &);
  521. template <typename T>
  522. generic_wide_int (const T &, unsigned int);
  523. /* Conversions. */
  524. HOST_WIDE_INT to_shwi (unsigned int) const;
  525. HOST_WIDE_INT to_shwi () const;
  526. unsigned HOST_WIDE_INT to_uhwi (unsigned int) const;
  527. unsigned HOST_WIDE_INT to_uhwi () const;
  528. HOST_WIDE_INT to_short_addr () const;
  529. /* Public accessors for the interior of a wide int. */
  530. HOST_WIDE_INT sign_mask () const;
  531. HOST_WIDE_INT elt (unsigned int) const;
  532. unsigned HOST_WIDE_INT ulow () const;
  533. unsigned HOST_WIDE_INT uhigh () const;
  534. HOST_WIDE_INT slow () const;
  535. HOST_WIDE_INT shigh () const;
  536. template <typename T>
  537. generic_wide_int &operator = (const T &);
  538. #define BINARY_PREDICATE(OP, F) \
  539. template <typename T> \
  540. bool OP (const T &c) const { return wi::F (*this, c); }
  541. #define UNARY_OPERATOR(OP, F) \
  542. WI_UNARY_RESULT (generic_wide_int) OP () const { return wi::F (*this); }
  543. #define BINARY_OPERATOR(OP, F) \
  544. template <typename T> \
  545. WI_BINARY_RESULT (generic_wide_int, T) \
  546. OP (const T &c) const { return wi::F (*this, c); }
  547. #define ASSIGNMENT_OPERATOR(OP, F) \
  548. template <typename T> \
  549. generic_wide_int &OP (const T &c) { return (*this = wi::F (*this, c)); }
  550. #define INCDEC_OPERATOR(OP, DELTA) \
  551. generic_wide_int &OP () { *this += DELTA; return *this; }
  552. UNARY_OPERATOR (operator ~, bit_not)
  553. UNARY_OPERATOR (operator -, neg)
  554. BINARY_PREDICATE (operator ==, eq_p)
  555. BINARY_PREDICATE (operator !=, ne_p)
  556. BINARY_OPERATOR (operator &, bit_and)
  557. BINARY_OPERATOR (and_not, bit_and_not)
  558. BINARY_OPERATOR (operator |, bit_or)
  559. BINARY_OPERATOR (or_not, bit_or_not)
  560. BINARY_OPERATOR (operator ^, bit_xor)
  561. BINARY_OPERATOR (operator +, add)
  562. BINARY_OPERATOR (operator -, sub)
  563. BINARY_OPERATOR (operator *, mul)
  564. ASSIGNMENT_OPERATOR (operator &=, bit_and)
  565. ASSIGNMENT_OPERATOR (operator |=, bit_or)
  566. ASSIGNMENT_OPERATOR (operator ^=, bit_xor)
  567. ASSIGNMENT_OPERATOR (operator +=, add)
  568. ASSIGNMENT_OPERATOR (operator -=, sub)
  569. ASSIGNMENT_OPERATOR (operator *=, mul)
  570. INCDEC_OPERATOR (operator ++, 1)
  571. INCDEC_OPERATOR (operator --, -1)
  572. #undef BINARY_PREDICATE
  573. #undef UNARY_OPERATOR
  574. #undef BINARY_OPERATOR
  575. #undef ASSIGNMENT_OPERATOR
  576. #undef INCDEC_OPERATOR
  577. /* Debugging functions. */
  578. void dump () const;
  579. static const bool is_sign_extended
  580. = wi::int_traits <generic_wide_int <storage> >::is_sign_extended;
  581. };
  582. template <typename storage>
  583. inline generic_wide_int <storage>::generic_wide_int () {}
  584. template <typename storage>
  585. template <typename T>
  586. inline generic_wide_int <storage>::generic_wide_int (const T &x)
  587. : storage (x)
  588. {
  589. }
  590. template <typename storage>
  591. template <typename T>
  592. inline generic_wide_int <storage>::generic_wide_int (const T &x,
  593. unsigned int precision)
  594. : storage (x, precision)
  595. {
  596. }
  597. /* Return THIS as a signed HOST_WIDE_INT, sign-extending from PRECISION.
  598. If THIS does not fit in PRECISION, the information is lost. */
  599. template <typename storage>
  600. inline HOST_WIDE_INT
  601. generic_wide_int <storage>::to_shwi (unsigned int precision) const
  602. {
  603. if (precision < HOST_BITS_PER_WIDE_INT)
  604. return sext_hwi (this->get_val ()[0], precision);
  605. else
  606. return this->get_val ()[0];
  607. }
  608. /* Return THIS as a signed HOST_WIDE_INT, in its natural precision. */
  609. template <typename storage>
  610. inline HOST_WIDE_INT
  611. generic_wide_int <storage>::to_shwi () const
  612. {
  613. if (is_sign_extended)
  614. return this->get_val ()[0];
  615. else
  616. return to_shwi (this->get_precision ());
  617. }
  618. /* Return THIS as an unsigned HOST_WIDE_INT, zero-extending from
  619. PRECISION. If THIS does not fit in PRECISION, the information
  620. is lost. */
  621. template <typename storage>
  622. inline unsigned HOST_WIDE_INT
  623. generic_wide_int <storage>::to_uhwi (unsigned int precision) const
  624. {
  625. if (precision < HOST_BITS_PER_WIDE_INT)
  626. return zext_hwi (this->get_val ()[0], precision);
  627. else
  628. return this->get_val ()[0];
  629. }
  630. /* Return THIS as an signed HOST_WIDE_INT, in its natural precision. */
  631. template <typename storage>
  632. inline unsigned HOST_WIDE_INT
  633. generic_wide_int <storage>::to_uhwi () const
  634. {
  635. return to_uhwi (this->get_precision ());
  636. }
  637. /* TODO: The compiler is half converted from using HOST_WIDE_INT to
  638. represent addresses to using offset_int to represent addresses.
  639. We use to_short_addr at the interface from new code to old,
  640. unconverted code. */
  641. template <typename storage>
  642. inline HOST_WIDE_INT
  643. generic_wide_int <storage>::to_short_addr () const
  644. {
  645. return this->get_val ()[0];
  646. }
  647. /* Return the implicit value of blocks above get_len (). */
  648. template <typename storage>
  649. inline HOST_WIDE_INT
  650. generic_wide_int <storage>::sign_mask () const
  651. {
  652. unsigned int len = this->get_len ();
  653. unsigned HOST_WIDE_INT high = this->get_val ()[len - 1];
  654. if (!is_sign_extended)
  655. {
  656. unsigned int precision = this->get_precision ();
  657. int excess = len * HOST_BITS_PER_WIDE_INT - precision;
  658. if (excess > 0)
  659. high <<= excess;
  660. }
  661. return (HOST_WIDE_INT) (high) < 0 ? -1 : 0;
  662. }
  663. /* Return the signed value of the least-significant explicitly-encoded
  664. block. */
  665. template <typename storage>
  666. inline HOST_WIDE_INT
  667. generic_wide_int <storage>::slow () const
  668. {
  669. return this->get_val ()[0];
  670. }
  671. /* Return the signed value of the most-significant explicitly-encoded
  672. block. */
  673. template <typename storage>
  674. inline HOST_WIDE_INT
  675. generic_wide_int <storage>::shigh () const
  676. {
  677. return this->get_val ()[this->get_len () - 1];
  678. }
  679. /* Return the unsigned value of the least-significant
  680. explicitly-encoded block. */
  681. template <typename storage>
  682. inline unsigned HOST_WIDE_INT
  683. generic_wide_int <storage>::ulow () const
  684. {
  685. return this->get_val ()[0];
  686. }
  687. /* Return the unsigned value of the most-significant
  688. explicitly-encoded block. */
  689. template <typename storage>
  690. inline unsigned HOST_WIDE_INT
  691. generic_wide_int <storage>::uhigh () const
  692. {
  693. return this->get_val ()[this->get_len () - 1];
  694. }
  695. /* Return block I, which might be implicitly or explicit encoded. */
  696. template <typename storage>
  697. inline HOST_WIDE_INT
  698. generic_wide_int <storage>::elt (unsigned int i) const
  699. {
  700. if (i >= this->get_len ())
  701. return sign_mask ();
  702. else
  703. return this->get_val ()[i];
  704. }
  705. template <typename storage>
  706. template <typename T>
  707. generic_wide_int <storage> &
  708. generic_wide_int <storage>::operator = (const T &x)
  709. {
  710. storage::operator = (x);
  711. return *this;
  712. }
  713. /* Dump the contents of the integer to stderr, for debugging. */
  714. template <typename storage>
  715. void
  716. generic_wide_int <storage>::dump () const
  717. {
  718. unsigned int len = this->get_len ();
  719. const HOST_WIDE_INT *val = this->get_val ();
  720. unsigned int precision = this->get_precision ();
  721. fprintf (stderr, "[");
  722. if (len * HOST_BITS_PER_WIDE_INT < precision)
  723. fprintf (stderr, "...,");
  724. for (unsigned int i = 0; i < len - 1; ++i)
  725. fprintf (stderr, HOST_WIDE_INT_PRINT_HEX ",", val[len - 1 - i]);
  726. fprintf (stderr, HOST_WIDE_INT_PRINT_HEX "], precision = %d\n",
  727. val[0], precision);
  728. }
  729. namespace wi
  730. {
  731. template <typename storage>
  732. struct int_traits < generic_wide_int <storage> >
  733. : public wi::int_traits <storage>
  734. {
  735. static unsigned int get_precision (const generic_wide_int <storage> &);
  736. static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
  737. const generic_wide_int <storage> &);
  738. };
  739. }
  740. template <typename storage>
  741. inline unsigned int
  742. wi::int_traits < generic_wide_int <storage> >::
  743. get_precision (const generic_wide_int <storage> &x)
  744. {
  745. return x.get_precision ();
  746. }
  747. template <typename storage>
  748. inline wi::storage_ref
  749. wi::int_traits < generic_wide_int <storage> >::
  750. decompose (HOST_WIDE_INT *, unsigned int precision,
  751. const generic_wide_int <storage> &x)
  752. {
  753. gcc_checking_assert (precision == x.get_precision ());
  754. return wi::storage_ref (x.get_val (), x.get_len (), precision);
  755. }
  756. /* Provide the storage for a wide_int_ref. This acts like a read-only
  757. wide_int, with the optimization that VAL is normally a pointer to
  758. another integer's storage, so that no array copy is needed. */
  759. template <bool SE>
  760. struct wide_int_ref_storage : public wi::storage_ref
  761. {
  762. private:
  763. /* Scratch space that can be used when decomposing the original integer.
  764. It must live as long as this object. */
  765. HOST_WIDE_INT scratch[2];
  766. public:
  767. wide_int_ref_storage (const wi::storage_ref &);
  768. template <typename T>
  769. wide_int_ref_storage (const T &);
  770. template <typename T>
  771. wide_int_ref_storage (const T &, unsigned int);
  772. };
  773. /* Create a reference from an existing reference. */
  774. template <bool SE>
  775. inline wide_int_ref_storage <SE>::
  776. wide_int_ref_storage (const wi::storage_ref &x)
  777. : storage_ref (x)
  778. {}
  779. /* Create a reference to integer X in its natural precision. Note
  780. that the natural precision is host-dependent for primitive
  781. types. */
  782. template <bool SE>
  783. template <typename T>
  784. inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x)
  785. : storage_ref (wi::int_traits <T>::decompose (scratch,
  786. wi::get_precision (x), x))
  787. {
  788. }
  789. /* Create a reference to integer X in precision PRECISION. */
  790. template <bool SE>
  791. template <typename T>
  792. inline wide_int_ref_storage <SE>::wide_int_ref_storage (const T &x,
  793. unsigned int precision)
  794. : storage_ref (wi::int_traits <T>::decompose (scratch, precision, x))
  795. {
  796. }
  797. namespace wi
  798. {
  799. template <bool SE>
  800. struct int_traits <wide_int_ref_storage <SE> >
  801. {
  802. static const enum precision_type precision_type = VAR_PRECISION;
  803. /* wi::storage_ref can be a reference to a primitive type,
  804. so this is the conservatively-correct setting. */
  805. static const bool host_dependent_precision = true;
  806. static const bool is_sign_extended = SE;
  807. };
  808. }
  809. namespace wi
  810. {
  811. unsigned int force_to_size (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  812. unsigned int, unsigned int, unsigned int,
  813. signop sgn);
  814. unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  815. unsigned int, unsigned int, bool = true);
  816. }
  817. /* The storage used by wide_int. */
  818. class GTY(()) wide_int_storage
  819. {
  820. private:
  821. HOST_WIDE_INT val[WIDE_INT_MAX_ELTS];
  822. unsigned int len;
  823. unsigned int precision;
  824. public:
  825. wide_int_storage ();
  826. template <typename T>
  827. wide_int_storage (const T &);
  828. /* The standard generic_wide_int storage methods. */
  829. unsigned int get_precision () const;
  830. const HOST_WIDE_INT *get_val () const;
  831. unsigned int get_len () const;
  832. HOST_WIDE_INT *write_val ();
  833. void set_len (unsigned int, bool = false);
  834. static wide_int from (const wide_int_ref &, unsigned int, signop);
  835. static wide_int from_array (const HOST_WIDE_INT *, unsigned int,
  836. unsigned int, bool = true);
  837. static wide_int create (unsigned int);
  838. /* FIXME: target-dependent, so should disappear. */
  839. wide_int bswap () const;
  840. };
  841. namespace wi
  842. {
  843. template <>
  844. struct int_traits <wide_int_storage>
  845. {
  846. static const enum precision_type precision_type = VAR_PRECISION;
  847. /* Guaranteed by a static assert in the wide_int_storage constructor. */
  848. static const bool host_dependent_precision = false;
  849. static const bool is_sign_extended = true;
  850. template <typename T1, typename T2>
  851. static wide_int get_binary_result (const T1 &, const T2 &);
  852. };
  853. }
  854. inline wide_int_storage::wide_int_storage () {}
  855. /* Initialize the storage from integer X, in its natural precision.
  856. Note that we do not allow integers with host-dependent precision
  857. to become wide_ints; wide_ints must always be logically independent
  858. of the host. */
  859. template <typename T>
  860. inline wide_int_storage::wide_int_storage (const T &x)
  861. {
  862. { STATIC_ASSERT (!wi::int_traits<T>::host_dependent_precision); }
  863. { STATIC_ASSERT (wi::int_traits<T>::precision_type != wi::CONST_PRECISION); }
  864. WIDE_INT_REF_FOR (T) xi (x);
  865. precision = xi.precision;
  866. wi::copy (*this, xi);
  867. }
  868. inline unsigned int
  869. wide_int_storage::get_precision () const
  870. {
  871. return precision;
  872. }
  873. inline const HOST_WIDE_INT *
  874. wide_int_storage::get_val () const
  875. {
  876. return val;
  877. }
  878. inline unsigned int
  879. wide_int_storage::get_len () const
  880. {
  881. return len;
  882. }
  883. inline HOST_WIDE_INT *
  884. wide_int_storage::write_val ()
  885. {
  886. return val;
  887. }
  888. inline void
  889. wide_int_storage::set_len (unsigned int l, bool is_sign_extended)
  890. {
  891. len = l;
  892. if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > precision)
  893. val[len - 1] = sext_hwi (val[len - 1],
  894. precision % HOST_BITS_PER_WIDE_INT);
  895. }
  896. /* Treat X as having signedness SGN and convert it to a PRECISION-bit
  897. number. */
  898. inline wide_int
  899. wide_int_storage::from (const wide_int_ref &x, unsigned int precision,
  900. signop sgn)
  901. {
  902. wide_int result = wide_int::create (precision);
  903. result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
  904. x.precision, precision, sgn));
  905. return result;
  906. }
  907. /* Create a wide_int from the explicit block encoding given by VAL and
  908. LEN. PRECISION is the precision of the integer. NEED_CANON_P is
  909. true if the encoding may have redundant trailing blocks. */
  910. inline wide_int
  911. wide_int_storage::from_array (const HOST_WIDE_INT *val, unsigned int len,
  912. unsigned int precision, bool need_canon_p)
  913. {
  914. wide_int result = wide_int::create (precision);
  915. result.set_len (wi::from_array (result.write_val (), val, len, precision,
  916. need_canon_p));
  917. return result;
  918. }
  919. /* Return an uninitialized wide_int with precision PRECISION. */
  920. inline wide_int
  921. wide_int_storage::create (unsigned int precision)
  922. {
  923. wide_int x;
  924. x.precision = precision;
  925. return x;
  926. }
  927. template <typename T1, typename T2>
  928. inline wide_int
  929. wi::int_traits <wide_int_storage>::get_binary_result (const T1 &x, const T2 &y)
  930. {
  931. /* This shouldn't be used for two flexible-precision inputs. */
  932. STATIC_ASSERT (wi::int_traits <T1>::precision_type != FLEXIBLE_PRECISION
  933. || wi::int_traits <T2>::precision_type != FLEXIBLE_PRECISION);
  934. if (wi::int_traits <T1>::precision_type == FLEXIBLE_PRECISION)
  935. return wide_int::create (wi::get_precision (y));
  936. else
  937. return wide_int::create (wi::get_precision (x));
  938. }
  939. /* The storage used by FIXED_WIDE_INT (N). */
  940. template <int N>
  941. class GTY(()) fixed_wide_int_storage
  942. {
  943. private:
  944. HOST_WIDE_INT val[(N + HOST_BITS_PER_WIDE_INT + 1) / HOST_BITS_PER_WIDE_INT];
  945. unsigned int len;
  946. public:
  947. fixed_wide_int_storage ();
  948. template <typename T>
  949. fixed_wide_int_storage (const T &);
  950. /* The standard generic_wide_int storage methods. */
  951. unsigned int get_precision () const;
  952. const HOST_WIDE_INT *get_val () const;
  953. unsigned int get_len () const;
  954. HOST_WIDE_INT *write_val ();
  955. void set_len (unsigned int, bool = false);
  956. static FIXED_WIDE_INT (N) from (const wide_int_ref &, signop);
  957. static FIXED_WIDE_INT (N) from_array (const HOST_WIDE_INT *, unsigned int,
  958. bool = true);
  959. };
  960. namespace wi
  961. {
  962. template <int N>
  963. struct int_traits < fixed_wide_int_storage <N> >
  964. {
  965. static const enum precision_type precision_type = CONST_PRECISION;
  966. static const bool host_dependent_precision = false;
  967. static const bool is_sign_extended = true;
  968. static const unsigned int precision = N;
  969. template <typename T1, typename T2>
  970. static FIXED_WIDE_INT (N) get_binary_result (const T1 &, const T2 &);
  971. };
  972. }
  973. template <int N>
  974. inline fixed_wide_int_storage <N>::fixed_wide_int_storage () {}
  975. /* Initialize the storage from integer X, in precision N. */
  976. template <int N>
  977. template <typename T>
  978. inline fixed_wide_int_storage <N>::fixed_wide_int_storage (const T &x)
  979. {
  980. /* Check for type compatibility. We don't want to initialize a
  981. fixed-width integer from something like a wide_int. */
  982. WI_BINARY_RESULT (T, FIXED_WIDE_INT (N)) *assertion ATTRIBUTE_UNUSED;
  983. wi::copy (*this, WIDE_INT_REF_FOR (T) (x, N));
  984. }
  985. template <int N>
  986. inline unsigned int
  987. fixed_wide_int_storage <N>::get_precision () const
  988. {
  989. return N;
  990. }
  991. template <int N>
  992. inline const HOST_WIDE_INT *
  993. fixed_wide_int_storage <N>::get_val () const
  994. {
  995. return val;
  996. }
  997. template <int N>
  998. inline unsigned int
  999. fixed_wide_int_storage <N>::get_len () const
  1000. {
  1001. return len;
  1002. }
  1003. template <int N>
  1004. inline HOST_WIDE_INT *
  1005. fixed_wide_int_storage <N>::write_val ()
  1006. {
  1007. return val;
  1008. }
  1009. template <int N>
  1010. inline void
  1011. fixed_wide_int_storage <N>::set_len (unsigned int l, bool)
  1012. {
  1013. len = l;
  1014. /* There are no excess bits in val[len - 1]. */
  1015. STATIC_ASSERT (N % HOST_BITS_PER_WIDE_INT == 0);
  1016. }
  1017. /* Treat X as having signedness SGN and convert it to an N-bit number. */
  1018. template <int N>
  1019. inline FIXED_WIDE_INT (N)
  1020. fixed_wide_int_storage <N>::from (const wide_int_ref &x, signop sgn)
  1021. {
  1022. FIXED_WIDE_INT (N) result;
  1023. result.set_len (wi::force_to_size (result.write_val (), x.val, x.len,
  1024. x.precision, N, sgn));
  1025. return result;
  1026. }
  1027. /* Create a FIXED_WIDE_INT (N) from the explicit block encoding given by
  1028. VAL and LEN. NEED_CANON_P is true if the encoding may have redundant
  1029. trailing blocks. */
  1030. template <int N>
  1031. inline FIXED_WIDE_INT (N)
  1032. fixed_wide_int_storage <N>::from_array (const HOST_WIDE_INT *val,
  1033. unsigned int len,
  1034. bool need_canon_p)
  1035. {
  1036. FIXED_WIDE_INT (N) result;
  1037. result.set_len (wi::from_array (result.write_val (), val, len,
  1038. N, need_canon_p));
  1039. return result;
  1040. }
  1041. template <int N>
  1042. template <typename T1, typename T2>
  1043. inline FIXED_WIDE_INT (N)
  1044. wi::int_traits < fixed_wide_int_storage <N> >::
  1045. get_binary_result (const T1 &, const T2 &)
  1046. {
  1047. return FIXED_WIDE_INT (N) ();
  1048. }
  1049. /* A reference to one element of a trailing_wide_ints structure. */
  1050. class trailing_wide_int_storage
  1051. {
  1052. private:
  1053. /* The precision of the integer, which is a fixed property of the
  1054. parent trailing_wide_ints. */
  1055. unsigned int m_precision;
  1056. /* A pointer to the length field. */
  1057. unsigned char *m_len;
  1058. /* A pointer to the HWI array. There are enough elements to hold all
  1059. values of precision M_PRECISION. */
  1060. HOST_WIDE_INT *m_val;
  1061. public:
  1062. trailing_wide_int_storage (unsigned int, unsigned char *, HOST_WIDE_INT *);
  1063. /* The standard generic_wide_int storage methods. */
  1064. unsigned int get_len () const;
  1065. unsigned int get_precision () const;
  1066. const HOST_WIDE_INT *get_val () const;
  1067. HOST_WIDE_INT *write_val ();
  1068. void set_len (unsigned int, bool = false);
  1069. template <typename T>
  1070. trailing_wide_int_storage &operator = (const T &);
  1071. };
  1072. typedef generic_wide_int <trailing_wide_int_storage> trailing_wide_int;
  1073. /* trailing_wide_int behaves like a wide_int. */
  1074. namespace wi
  1075. {
  1076. template <>
  1077. struct int_traits <trailing_wide_int_storage>
  1078. : public int_traits <wide_int_storage> {};
  1079. }
  1080. /* An array of N wide_int-like objects that can be put at the end of
  1081. a variable-sized structure. Use extra_size to calculate how many
  1082. bytes beyond the sizeof need to be allocated. Use set_precision
  1083. to initialize the structure. */
  1084. template <int N>
  1085. class GTY(()) trailing_wide_ints
  1086. {
  1087. private:
  1088. /* The shared precision of each number. */
  1089. unsigned short m_precision;
  1090. /* The shared maximum length of each number. */
  1091. unsigned char m_max_len;
  1092. /* The current length of each number. */
  1093. unsigned char m_len[N];
  1094. /* The variable-length part of the structure, which always contains
  1095. at least one HWI. Element I starts at index I * M_MAX_LEN. */
  1096. HOST_WIDE_INT m_val[1];
  1097. public:
  1098. void set_precision (unsigned int);
  1099. trailing_wide_int operator [] (unsigned int);
  1100. static size_t extra_size (unsigned int);
  1101. };
  1102. inline trailing_wide_int_storage::
  1103. trailing_wide_int_storage (unsigned int precision, unsigned char *len,
  1104. HOST_WIDE_INT *val)
  1105. : m_precision (precision), m_len (len), m_val (val)
  1106. {
  1107. }
  1108. inline unsigned int
  1109. trailing_wide_int_storage::get_len () const
  1110. {
  1111. return *m_len;
  1112. }
  1113. inline unsigned int
  1114. trailing_wide_int_storage::get_precision () const
  1115. {
  1116. return m_precision;
  1117. }
  1118. inline const HOST_WIDE_INT *
  1119. trailing_wide_int_storage::get_val () const
  1120. {
  1121. return m_val;
  1122. }
  1123. inline HOST_WIDE_INT *
  1124. trailing_wide_int_storage::write_val ()
  1125. {
  1126. return m_val;
  1127. }
  1128. inline void
  1129. trailing_wide_int_storage::set_len (unsigned int len, bool is_sign_extended)
  1130. {
  1131. *m_len = len;
  1132. if (!is_sign_extended && len * HOST_BITS_PER_WIDE_INT > m_precision)
  1133. m_val[len - 1] = sext_hwi (m_val[len - 1],
  1134. m_precision % HOST_BITS_PER_WIDE_INT);
  1135. }
  1136. template <typename T>
  1137. inline trailing_wide_int_storage &
  1138. trailing_wide_int_storage::operator = (const T &x)
  1139. {
  1140. WIDE_INT_REF_FOR (T) xi (x, m_precision);
  1141. wi::copy (*this, xi);
  1142. return *this;
  1143. }
  1144. /* Initialize the structure and record that all elements have precision
  1145. PRECISION. */
  1146. template <int N>
  1147. inline void
  1148. trailing_wide_ints <N>::set_precision (unsigned int precision)
  1149. {
  1150. m_precision = precision;
  1151. m_max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
  1152. / HOST_BITS_PER_WIDE_INT);
  1153. }
  1154. /* Return a reference to element INDEX. */
  1155. template <int N>
  1156. inline trailing_wide_int
  1157. trailing_wide_ints <N>::operator [] (unsigned int index)
  1158. {
  1159. return trailing_wide_int_storage (m_precision, &m_len[index],
  1160. &m_val[index * m_max_len]);
  1161. }
  1162. /* Return how many extra bytes need to be added to the end of the structure
  1163. in order to handle N wide_ints of precision PRECISION. */
  1164. template <int N>
  1165. inline size_t
  1166. trailing_wide_ints <N>::extra_size (unsigned int precision)
  1167. {
  1168. unsigned int max_len = ((precision + HOST_BITS_PER_WIDE_INT - 1)
  1169. / HOST_BITS_PER_WIDE_INT);
  1170. return (N * max_len - 1) * sizeof (HOST_WIDE_INT);
  1171. }
  1172. /* This macro is used in structures that end with a trailing_wide_ints field
  1173. called FIELD. It declares get_NAME() and set_NAME() methods to access
  1174. element I of FIELD. */
  1175. #define TRAILING_WIDE_INT_ACCESSOR(NAME, FIELD, I) \
  1176. trailing_wide_int get_##NAME () { return FIELD[I]; } \
  1177. template <typename T> void set_##NAME (const T &x) { FIELD[I] = x; }
  1178. namespace wi
  1179. {
  1180. /* Implementation of int_traits for primitive integer types like "int". */
  1181. template <typename T, bool signed_p>
  1182. struct primitive_int_traits
  1183. {
  1184. static const enum precision_type precision_type = FLEXIBLE_PRECISION;
  1185. static const bool host_dependent_precision = true;
  1186. static const bool is_sign_extended = true;
  1187. static unsigned int get_precision (T);
  1188. static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int, T);
  1189. };
  1190. }
  1191. template <typename T, bool signed_p>
  1192. inline unsigned int
  1193. wi::primitive_int_traits <T, signed_p>::get_precision (T)
  1194. {
  1195. return sizeof (T) * CHAR_BIT;
  1196. }
  1197. template <typename T, bool signed_p>
  1198. inline wi::storage_ref
  1199. wi::primitive_int_traits <T, signed_p>::decompose (HOST_WIDE_INT *scratch,
  1200. unsigned int precision, T x)
  1201. {
  1202. scratch[0] = x;
  1203. if (signed_p || scratch[0] >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
  1204. return wi::storage_ref (scratch, 1, precision);
  1205. scratch[1] = 0;
  1206. return wi::storage_ref (scratch, 2, precision);
  1207. }
  1208. /* Allow primitive C types to be used in wi:: routines. */
  1209. namespace wi
  1210. {
  1211. template <>
  1212. struct int_traits <int>
  1213. : public primitive_int_traits <int, true> {};
  1214. template <>
  1215. struct int_traits <unsigned int>
  1216. : public primitive_int_traits <unsigned int, false> {};
  1217. template <>
  1218. struct int_traits <long>
  1219. : public primitive_int_traits <long, true> {};
  1220. template <>
  1221. struct int_traits <unsigned long>
  1222. : public primitive_int_traits <unsigned long, false> {};
  1223. #if defined HAVE_LONG_LONG
  1224. template <>
  1225. struct int_traits <long long>
  1226. : public primitive_int_traits <long long, true> {};
  1227. template <>
  1228. struct int_traits <unsigned long long>
  1229. : public primitive_int_traits <unsigned long long, false> {};
  1230. #endif
  1231. }
  1232. namespace wi
  1233. {
  1234. /* Stores HWI-sized integer VAL, treating it as having signedness SGN
  1235. and precision PRECISION. */
  1236. struct hwi_with_prec
  1237. {
  1238. hwi_with_prec (HOST_WIDE_INT, unsigned int, signop);
  1239. HOST_WIDE_INT val;
  1240. unsigned int precision;
  1241. signop sgn;
  1242. };
  1243. hwi_with_prec shwi (HOST_WIDE_INT, unsigned int);
  1244. hwi_with_prec uhwi (unsigned HOST_WIDE_INT, unsigned int);
  1245. hwi_with_prec minus_one (unsigned int);
  1246. hwi_with_prec zero (unsigned int);
  1247. hwi_with_prec one (unsigned int);
  1248. hwi_with_prec two (unsigned int);
  1249. }
  1250. inline wi::hwi_with_prec::hwi_with_prec (HOST_WIDE_INT v, unsigned int p,
  1251. signop s)
  1252. : val (v), precision (p), sgn (s)
  1253. {
  1254. }
  1255. /* Return a signed integer that has value VAL and precision PRECISION. */
  1256. inline wi::hwi_with_prec
  1257. wi::shwi (HOST_WIDE_INT val, unsigned int precision)
  1258. {
  1259. return hwi_with_prec (val, precision, SIGNED);
  1260. }
  1261. /* Return an unsigned integer that has value VAL and precision PRECISION. */
  1262. inline wi::hwi_with_prec
  1263. wi::uhwi (unsigned HOST_WIDE_INT val, unsigned int precision)
  1264. {
  1265. return hwi_with_prec (val, precision, UNSIGNED);
  1266. }
  1267. /* Return a wide int of -1 with precision PRECISION. */
  1268. inline wi::hwi_with_prec
  1269. wi::minus_one (unsigned int precision)
  1270. {
  1271. return wi::shwi (-1, precision);
  1272. }
  1273. /* Return a wide int of 0 with precision PRECISION. */
  1274. inline wi::hwi_with_prec
  1275. wi::zero (unsigned int precision)
  1276. {
  1277. return wi::shwi (0, precision);
  1278. }
  1279. /* Return a wide int of 1 with precision PRECISION. */
  1280. inline wi::hwi_with_prec
  1281. wi::one (unsigned int precision)
  1282. {
  1283. return wi::shwi (1, precision);
  1284. }
  1285. /* Return a wide int of 2 with precision PRECISION. */
  1286. inline wi::hwi_with_prec
  1287. wi::two (unsigned int precision)
  1288. {
  1289. return wi::shwi (2, precision);
  1290. }
  1291. namespace wi
  1292. {
  1293. template <>
  1294. struct int_traits <wi::hwi_with_prec>
  1295. {
  1296. static const enum precision_type precision_type = VAR_PRECISION;
  1297. /* hwi_with_prec has an explicitly-given precision, rather than the
  1298. precision of HOST_WIDE_INT. */
  1299. static const bool host_dependent_precision = false;
  1300. static const bool is_sign_extended = true;
  1301. static unsigned int get_precision (const wi::hwi_with_prec &);
  1302. static wi::storage_ref decompose (HOST_WIDE_INT *, unsigned int,
  1303. const wi::hwi_with_prec &);
  1304. };
  1305. }
  1306. inline unsigned int
  1307. wi::int_traits <wi::hwi_with_prec>::get_precision (const wi::hwi_with_prec &x)
  1308. {
  1309. return x.precision;
  1310. }
  1311. inline wi::storage_ref
  1312. wi::int_traits <wi::hwi_with_prec>::
  1313. decompose (HOST_WIDE_INT *scratch, unsigned int precision,
  1314. const wi::hwi_with_prec &x)
  1315. {
  1316. gcc_checking_assert (precision == x.precision);
  1317. scratch[0] = x.val;
  1318. if (x.sgn == SIGNED || x.val >= 0 || precision <= HOST_BITS_PER_WIDE_INT)
  1319. return wi::storage_ref (scratch, 1, precision);
  1320. scratch[1] = 0;
  1321. return wi::storage_ref (scratch, 2, precision);
  1322. }
  1323. /* Private functions for handling large cases out of line. They take
  1324. individual length and array parameters because that is cheaper for
  1325. the inline caller than constructing an object on the stack and
  1326. passing a reference to it. (Although many callers use wide_int_refs,
  1327. we generally want those to be removed by SRA.) */
  1328. namespace wi
  1329. {
  1330. bool eq_p_large (const HOST_WIDE_INT *, unsigned int,
  1331. const HOST_WIDE_INT *, unsigned int, unsigned int);
  1332. bool lts_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
  1333. const HOST_WIDE_INT *, unsigned int);
  1334. bool ltu_p_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
  1335. const HOST_WIDE_INT *, unsigned int);
  1336. int cmps_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
  1337. const HOST_WIDE_INT *, unsigned int);
  1338. int cmpu_large (const HOST_WIDE_INT *, unsigned int, unsigned int,
  1339. const HOST_WIDE_INT *, unsigned int);
  1340. unsigned int sext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1341. unsigned int,
  1342. unsigned int, unsigned int);
  1343. unsigned int zext_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1344. unsigned int,
  1345. unsigned int, unsigned int);
  1346. unsigned int set_bit_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1347. unsigned int, unsigned int, unsigned int);
  1348. unsigned int lshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1349. unsigned int, unsigned int, unsigned int);
  1350. unsigned int lrshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1351. unsigned int, unsigned int, unsigned int,
  1352. unsigned int);
  1353. unsigned int arshift_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1354. unsigned int, unsigned int, unsigned int,
  1355. unsigned int);
  1356. unsigned int and_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
  1357. const HOST_WIDE_INT *, unsigned int, unsigned int);
  1358. unsigned int and_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1359. unsigned int, const HOST_WIDE_INT *,
  1360. unsigned int, unsigned int);
  1361. unsigned int or_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
  1362. const HOST_WIDE_INT *, unsigned int, unsigned int);
  1363. unsigned int or_not_large (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1364. unsigned int, const HOST_WIDE_INT *,
  1365. unsigned int, unsigned int);
  1366. unsigned int xor_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
  1367. const HOST_WIDE_INT *, unsigned int, unsigned int);
  1368. unsigned int add_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
  1369. const HOST_WIDE_INT *, unsigned int, unsigned int,
  1370. signop, bool *);
  1371. unsigned int sub_large (HOST_WIDE_INT *, const HOST_WIDE_INT *, unsigned int,
  1372. const HOST_WIDE_INT *, unsigned int, unsigned int,
  1373. signop, bool *);
  1374. unsigned int mul_internal (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1375. unsigned int, const HOST_WIDE_INT *,
  1376. unsigned int, unsigned int, signop, bool *,
  1377. bool);
  1378. unsigned int divmod_internal (HOST_WIDE_INT *, unsigned int *,
  1379. HOST_WIDE_INT *, const HOST_WIDE_INT *,
  1380. unsigned int, unsigned int,
  1381. const HOST_WIDE_INT *,
  1382. unsigned int, unsigned int,
  1383. signop, bool *);
  1384. }
  1385. /* Return the number of bits that integer X can hold. */
  1386. template <typename T>
  1387. inline unsigned int
  1388. wi::get_precision (const T &x)
  1389. {
  1390. return wi::int_traits <T>::get_precision (x);
  1391. }
  1392. /* Return the number of bits that the result of a binary operation can
  1393. hold when the input operands are X and Y. */
  1394. template <typename T1, typename T2>
  1395. inline unsigned int
  1396. wi::get_binary_precision (const T1 &x, const T2 &y)
  1397. {
  1398. return get_precision (wi::int_traits <WI_BINARY_RESULT (T1, T2)>::
  1399. get_binary_result (x, y));
  1400. }
  1401. /* Copy the contents of Y to X, but keeping X's current precision. */
  1402. template <typename T1, typename T2>
  1403. inline void
  1404. wi::copy (T1 &x, const T2 &y)
  1405. {
  1406. HOST_WIDE_INT *xval = x.write_val ();
  1407. const HOST_WIDE_INT *yval = y.get_val ();
  1408. unsigned int len = y.get_len ();
  1409. unsigned int i = 0;
  1410. do
  1411. xval[i] = yval[i];
  1412. while (++i < len);
  1413. x.set_len (len, y.is_sign_extended);
  1414. }
  1415. /* Return true if X fits in a HOST_WIDE_INT with no loss of precision. */
  1416. template <typename T>
  1417. inline bool
  1418. wi::fits_shwi_p (const T &x)
  1419. {
  1420. WIDE_INT_REF_FOR (T) xi (x);
  1421. return xi.len == 1;
  1422. }
  1423. /* Return true if X fits in an unsigned HOST_WIDE_INT with no loss of
  1424. precision. */
  1425. template <typename T>
  1426. inline bool
  1427. wi::fits_uhwi_p (const T &x)
  1428. {
  1429. WIDE_INT_REF_FOR (T) xi (x);
  1430. if (xi.precision <= HOST_BITS_PER_WIDE_INT)
  1431. return true;
  1432. if (xi.len == 1)
  1433. return xi.slow () >= 0;
  1434. return xi.len == 2 && xi.uhigh () == 0;
  1435. }
  1436. /* Return true if X is negative based on the interpretation of SGN.
  1437. For UNSIGNED, this is always false. */
  1438. template <typename T>
  1439. inline bool
  1440. wi::neg_p (const T &x, signop sgn)
  1441. {
  1442. WIDE_INT_REF_FOR (T) xi (x);
  1443. if (sgn == UNSIGNED)
  1444. return false;
  1445. return xi.sign_mask () < 0;
  1446. }
  1447. /* Return -1 if the top bit of X is set and 0 if the top bit is clear. */
  1448. template <typename T>
  1449. inline HOST_WIDE_INT
  1450. wi::sign_mask (const T &x)
  1451. {
  1452. WIDE_INT_REF_FOR (T) xi (x);
  1453. return xi.sign_mask ();
  1454. }
  1455. /* Return true if X == Y. X and Y must be binary-compatible. */
  1456. template <typename T1, typename T2>
  1457. inline bool
  1458. wi::eq_p (const T1 &x, const T2 &y)
  1459. {
  1460. unsigned int precision = get_binary_precision (x, y);
  1461. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1462. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1463. if (xi.is_sign_extended && yi.is_sign_extended)
  1464. {
  1465. /* This case reduces to array equality. */
  1466. if (xi.len != yi.len)
  1467. return false;
  1468. unsigned int i = 0;
  1469. do
  1470. if (xi.val[i] != yi.val[i])
  1471. return false;
  1472. while (++i != xi.len);
  1473. return true;
  1474. }
  1475. if (__builtin_expect (yi.len == 1, true))
  1476. {
  1477. /* XI is only equal to YI if it too has a single HWI. */
  1478. if (xi.len != 1)
  1479. return false;
  1480. /* Excess bits in xi.val[0] will be signs or zeros, so comparisons
  1481. with 0 are simple. */
  1482. if (STATIC_CONSTANT_P (yi.val[0] == 0))
  1483. return xi.val[0] == 0;
  1484. /* Otherwise flush out any excess bits first. */
  1485. unsigned HOST_WIDE_INT diff = xi.val[0] ^ yi.val[0];
  1486. int excess = HOST_BITS_PER_WIDE_INT - precision;
  1487. if (excess > 0)
  1488. diff <<= excess;
  1489. return diff == 0;
  1490. }
  1491. return eq_p_large (xi.val, xi.len, yi.val, yi.len, precision);
  1492. }
  1493. /* Return true if X != Y. X and Y must be binary-compatible. */
  1494. template <typename T1, typename T2>
  1495. inline bool
  1496. wi::ne_p (const T1 &x, const T2 &y)
  1497. {
  1498. return !eq_p (x, y);
  1499. }
  1500. /* Return true if X < Y when both are treated as signed values. */
  1501. template <typename T1, typename T2>
  1502. inline bool
  1503. wi::lts_p (const T1 &x, const T2 &y)
  1504. {
  1505. unsigned int precision = get_binary_precision (x, y);
  1506. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1507. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1508. /* We optimize x < y, where y is 64 or fewer bits. */
  1509. if (wi::fits_shwi_p (yi))
  1510. {
  1511. /* Make lts_p (x, 0) as efficient as wi::neg_p (x). */
  1512. if (STATIC_CONSTANT_P (yi.val[0] == 0))
  1513. return neg_p (xi);
  1514. /* If x fits directly into a shwi, we can compare directly. */
  1515. if (wi::fits_shwi_p (xi))
  1516. return xi.to_shwi () < yi.to_shwi ();
  1517. /* If x doesn't fit and is negative, then it must be more
  1518. negative than any value in y, and hence smaller than y. */
  1519. if (neg_p (xi))
  1520. return true;
  1521. /* If x is positive, then it must be larger than any value in y,
  1522. and hence greater than y. */
  1523. return false;
  1524. }
  1525. /* Optimize the opposite case, if it can be detected at compile time. */
  1526. if (STATIC_CONSTANT_P (xi.len == 1))
  1527. /* If YI is negative it is lower than the least HWI.
  1528. If YI is positive it is greater than the greatest HWI. */
  1529. return !neg_p (yi);
  1530. return lts_p_large (xi.val, xi.len, precision, yi.val, yi.len);
  1531. }
  1532. /* Return true if X < Y when both are treated as unsigned values. */
  1533. template <typename T1, typename T2>
  1534. inline bool
  1535. wi::ltu_p (const T1 &x, const T2 &y)
  1536. {
  1537. unsigned int precision = get_binary_precision (x, y);
  1538. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1539. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1540. /* Optimize comparisons with constants. */
  1541. if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
  1542. return xi.len == 1 && xi.to_uhwi () < (unsigned HOST_WIDE_INT) yi.val[0];
  1543. if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
  1544. return yi.len != 1 || yi.to_uhwi () > (unsigned HOST_WIDE_INT) xi.val[0];
  1545. /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
  1546. for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
  1547. values does not change the result. */
  1548. if (__builtin_expect (xi.len + yi.len == 2, true))
  1549. {
  1550. unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
  1551. unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
  1552. return xl < yl;
  1553. }
  1554. return ltu_p_large (xi.val, xi.len, precision, yi.val, yi.len);
  1555. }
  1556. /* Return true if X < Y. Signedness of X and Y is indicated by SGN. */
  1557. template <typename T1, typename T2>
  1558. inline bool
  1559. wi::lt_p (const T1 &x, const T2 &y, signop sgn)
  1560. {
  1561. if (sgn == SIGNED)
  1562. return lts_p (x, y);
  1563. else
  1564. return ltu_p (x, y);
  1565. }
  1566. /* Return true if X <= Y when both are treated as signed values. */
  1567. template <typename T1, typename T2>
  1568. inline bool
  1569. wi::les_p (const T1 &x, const T2 &y)
  1570. {
  1571. return !lts_p (y, x);
  1572. }
  1573. /* Return true if X <= Y when both are treated as unsigned values. */
  1574. template <typename T1, typename T2>
  1575. inline bool
  1576. wi::leu_p (const T1 &x, const T2 &y)
  1577. {
  1578. return !ltu_p (y, x);
  1579. }
  1580. /* Return true if X <= Y. Signedness of X and Y is indicated by SGN. */
  1581. template <typename T1, typename T2>
  1582. inline bool
  1583. wi::le_p (const T1 &x, const T2 &y, signop sgn)
  1584. {
  1585. if (sgn == SIGNED)
  1586. return les_p (x, y);
  1587. else
  1588. return leu_p (x, y);
  1589. }
  1590. /* Return true if X > Y when both are treated as signed values. */
  1591. template <typename T1, typename T2>
  1592. inline bool
  1593. wi::gts_p (const T1 &x, const T2 &y)
  1594. {
  1595. return lts_p (y, x);
  1596. }
  1597. /* Return true if X > Y when both are treated as unsigned values. */
  1598. template <typename T1, typename T2>
  1599. inline bool
  1600. wi::gtu_p (const T1 &x, const T2 &y)
  1601. {
  1602. return ltu_p (y, x);
  1603. }
  1604. /* Return true if X > Y. Signedness of X and Y is indicated by SGN. */
  1605. template <typename T1, typename T2>
  1606. inline bool
  1607. wi::gt_p (const T1 &x, const T2 &y, signop sgn)
  1608. {
  1609. if (sgn == SIGNED)
  1610. return gts_p (x, y);
  1611. else
  1612. return gtu_p (x, y);
  1613. }
  1614. /* Return true if X >= Y when both are treated as signed values. */
  1615. template <typename T1, typename T2>
  1616. inline bool
  1617. wi::ges_p (const T1 &x, const T2 &y)
  1618. {
  1619. return !lts_p (x, y);
  1620. }
  1621. /* Return true if X >= Y when both are treated as unsigned values. */
  1622. template <typename T1, typename T2>
  1623. inline bool
  1624. wi::geu_p (const T1 &x, const T2 &y)
  1625. {
  1626. return !ltu_p (x, y);
  1627. }
  1628. /* Return true if X >= Y. Signedness of X and Y is indicated by SGN. */
  1629. template <typename T1, typename T2>
  1630. inline bool
  1631. wi::ge_p (const T1 &x, const T2 &y, signop sgn)
  1632. {
  1633. if (sgn == SIGNED)
  1634. return ges_p (x, y);
  1635. else
  1636. return geu_p (x, y);
  1637. }
  1638. /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
  1639. as signed values. */
  1640. template <typename T1, typename T2>
  1641. inline int
  1642. wi::cmps (const T1 &x, const T2 &y)
  1643. {
  1644. unsigned int precision = get_binary_precision (x, y);
  1645. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1646. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1647. if (wi::fits_shwi_p (yi))
  1648. {
  1649. /* Special case for comparisons with 0. */
  1650. if (STATIC_CONSTANT_P (yi.val[0] == 0))
  1651. return neg_p (xi) ? -1 : !(xi.len == 1 && xi.val[0] == 0);
  1652. /* If x fits into a signed HWI, we can compare directly. */
  1653. if (wi::fits_shwi_p (xi))
  1654. {
  1655. HOST_WIDE_INT xl = xi.to_shwi ();
  1656. HOST_WIDE_INT yl = yi.to_shwi ();
  1657. return xl < yl ? -1 : xl > yl;
  1658. }
  1659. /* If x doesn't fit and is negative, then it must be more
  1660. negative than any signed HWI, and hence smaller than y. */
  1661. if (neg_p (xi))
  1662. return -1;
  1663. /* If x is positive, then it must be larger than any signed HWI,
  1664. and hence greater than y. */
  1665. return 1;
  1666. }
  1667. /* Optimize the opposite case, if it can be detected at compile time. */
  1668. if (STATIC_CONSTANT_P (xi.len == 1))
  1669. /* If YI is negative it is lower than the least HWI.
  1670. If YI is positive it is greater than the greatest HWI. */
  1671. return neg_p (yi) ? 1 : -1;
  1672. return cmps_large (xi.val, xi.len, precision, yi.val, yi.len);
  1673. }
  1674. /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Treat both X and Y
  1675. as unsigned values. */
  1676. template <typename T1, typename T2>
  1677. inline int
  1678. wi::cmpu (const T1 &x, const T2 &y)
  1679. {
  1680. unsigned int precision = get_binary_precision (x, y);
  1681. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1682. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1683. /* Optimize comparisons with constants. */
  1684. if (STATIC_CONSTANT_P (yi.len == 1 && yi.val[0] >= 0))
  1685. {
  1686. /* If XI doesn't fit in a HWI then it must be larger than YI. */
  1687. if (xi.len != 1)
  1688. return 1;
  1689. /* Otherwise compare directly. */
  1690. unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
  1691. unsigned HOST_WIDE_INT yl = yi.val[0];
  1692. return xl < yl ? -1 : xl > yl;
  1693. }
  1694. if (STATIC_CONSTANT_P (xi.len == 1 && xi.val[0] >= 0))
  1695. {
  1696. /* If YI doesn't fit in a HWI then it must be larger than XI. */
  1697. if (yi.len != 1)
  1698. return -1;
  1699. /* Otherwise compare directly. */
  1700. unsigned HOST_WIDE_INT xl = xi.val[0];
  1701. unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
  1702. return xl < yl ? -1 : xl > yl;
  1703. }
  1704. /* Optimize the case of two HWIs. The HWIs are implicitly sign-extended
  1705. for precisions greater than HOST_BITS_WIDE_INT, but sign-extending both
  1706. values does not change the result. */
  1707. if (__builtin_expect (xi.len + yi.len == 2, true))
  1708. {
  1709. unsigned HOST_WIDE_INT xl = xi.to_uhwi ();
  1710. unsigned HOST_WIDE_INT yl = yi.to_uhwi ();
  1711. return xl < yl ? -1 : xl > yl;
  1712. }
  1713. return cmpu_large (xi.val, xi.len, precision, yi.val, yi.len);
  1714. }
  1715. /* Return -1 if X < Y, 0 if X == Y and 1 if X > Y. Signedness of
  1716. X and Y indicated by SGN. */
  1717. template <typename T1, typename T2>
  1718. inline int
  1719. wi::cmp (const T1 &x, const T2 &y, signop sgn)
  1720. {
  1721. if (sgn == SIGNED)
  1722. return cmps (x, y);
  1723. else
  1724. return cmpu (x, y);
  1725. }
  1726. /* Return ~x. */
  1727. template <typename T>
  1728. inline WI_UNARY_RESULT (T)
  1729. wi::bit_not (const T &x)
  1730. {
  1731. WI_UNARY_RESULT_VAR (result, val, T, x);
  1732. WIDE_INT_REF_FOR (T) xi (x, get_precision (result));
  1733. for (unsigned int i = 0; i < xi.len; ++i)
  1734. val[i] = ~xi.val[i];
  1735. result.set_len (xi.len);
  1736. return result;
  1737. }
  1738. /* Return -x. */
  1739. template <typename T>
  1740. inline WI_UNARY_RESULT (T)
  1741. wi::neg (const T &x)
  1742. {
  1743. return sub (0, x);
  1744. }
  1745. /* Return -x. Indicate in *OVERFLOW if X is the minimum signed value. */
  1746. template <typename T>
  1747. inline WI_UNARY_RESULT (T)
  1748. wi::neg (const T &x, bool *overflow)
  1749. {
  1750. *overflow = only_sign_bit_p (x);
  1751. return sub (0, x);
  1752. }
  1753. /* Return the absolute value of x. */
  1754. template <typename T>
  1755. inline WI_UNARY_RESULT (T)
  1756. wi::abs (const T &x)
  1757. {
  1758. return neg_p (x) ? neg (x) : WI_UNARY_RESULT (T) (x);
  1759. }
  1760. /* Return the result of sign-extending the low OFFSET bits of X. */
  1761. template <typename T>
  1762. inline WI_UNARY_RESULT (T)
  1763. wi::sext (const T &x, unsigned int offset)
  1764. {
  1765. WI_UNARY_RESULT_VAR (result, val, T, x);
  1766. unsigned int precision = get_precision (result);
  1767. WIDE_INT_REF_FOR (T) xi (x, precision);
  1768. if (offset <= HOST_BITS_PER_WIDE_INT)
  1769. {
  1770. val[0] = sext_hwi (xi.ulow (), offset);
  1771. result.set_len (1, true);
  1772. }
  1773. else
  1774. result.set_len (sext_large (val, xi.val, xi.len, precision, offset));
  1775. return result;
  1776. }
  1777. /* Return the result of zero-extending the low OFFSET bits of X. */
  1778. template <typename T>
  1779. inline WI_UNARY_RESULT (T)
  1780. wi::zext (const T &x, unsigned int offset)
  1781. {
  1782. WI_UNARY_RESULT_VAR (result, val, T, x);
  1783. unsigned int precision = get_precision (result);
  1784. WIDE_INT_REF_FOR (T) xi (x, precision);
  1785. /* This is not just an optimization, it is actually required to
  1786. maintain canonization. */
  1787. if (offset >= precision)
  1788. {
  1789. wi::copy (result, xi);
  1790. return result;
  1791. }
  1792. /* In these cases we know that at least the top bit will be clear,
  1793. so no sign extension is necessary. */
  1794. if (offset < HOST_BITS_PER_WIDE_INT)
  1795. {
  1796. val[0] = zext_hwi (xi.ulow (), offset);
  1797. result.set_len (1, true);
  1798. }
  1799. else
  1800. result.set_len (zext_large (val, xi.val, xi.len, precision, offset), true);
  1801. return result;
  1802. }
  1803. /* Return the result of extending the low OFFSET bits of X according to
  1804. signedness SGN. */
  1805. template <typename T>
  1806. inline WI_UNARY_RESULT (T)
  1807. wi::ext (const T &x, unsigned int offset, signop sgn)
  1808. {
  1809. return sgn == SIGNED ? sext (x, offset) : zext (x, offset);
  1810. }
  1811. /* Return an integer that represents X | (1 << bit). */
  1812. template <typename T>
  1813. inline WI_UNARY_RESULT (T)
  1814. wi::set_bit (const T &x, unsigned int bit)
  1815. {
  1816. WI_UNARY_RESULT_VAR (result, val, T, x);
  1817. unsigned int precision = get_precision (result);
  1818. WIDE_INT_REF_FOR (T) xi (x, precision);
  1819. if (precision <= HOST_BITS_PER_WIDE_INT)
  1820. {
  1821. val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
  1822. result.set_len (1);
  1823. }
  1824. else
  1825. result.set_len (set_bit_large (val, xi.val, xi.len, precision, bit));
  1826. return result;
  1827. }
  1828. /* Return the mininum of X and Y, treating them both as having
  1829. signedness SGN. */
  1830. template <typename T1, typename T2>
  1831. inline WI_BINARY_RESULT (T1, T2)
  1832. wi::min (const T1 &x, const T2 &y, signop sgn)
  1833. {
  1834. WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
  1835. unsigned int precision = get_precision (result);
  1836. if (wi::le_p (x, y, sgn))
  1837. wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
  1838. else
  1839. wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
  1840. return result;
  1841. }
  1842. /* Return the minimum of X and Y, treating both as signed values. */
  1843. template <typename T1, typename T2>
  1844. inline WI_BINARY_RESULT (T1, T2)
  1845. wi::smin (const T1 &x, const T2 &y)
  1846. {
  1847. return wi::min (x, y, SIGNED);
  1848. }
  1849. /* Return the minimum of X and Y, treating both as unsigned values. */
  1850. template <typename T1, typename T2>
  1851. inline WI_BINARY_RESULT (T1, T2)
  1852. wi::umin (const T1 &x, const T2 &y)
  1853. {
  1854. return wi::min (x, y, UNSIGNED);
  1855. }
  1856. /* Return the maxinum of X and Y, treating them both as having
  1857. signedness SGN. */
  1858. template <typename T1, typename T2>
  1859. inline WI_BINARY_RESULT (T1, T2)
  1860. wi::max (const T1 &x, const T2 &y, signop sgn)
  1861. {
  1862. WI_BINARY_RESULT_VAR (result, val ATTRIBUTE_UNUSED, T1, x, T2, y);
  1863. unsigned int precision = get_precision (result);
  1864. if (wi::ge_p (x, y, sgn))
  1865. wi::copy (result, WIDE_INT_REF_FOR (T1) (x, precision));
  1866. else
  1867. wi::copy (result, WIDE_INT_REF_FOR (T2) (y, precision));
  1868. return result;
  1869. }
  1870. /* Return the maximum of X and Y, treating both as signed values. */
  1871. template <typename T1, typename T2>
  1872. inline WI_BINARY_RESULT (T1, T2)
  1873. wi::smax (const T1 &x, const T2 &y)
  1874. {
  1875. return wi::max (x, y, SIGNED);
  1876. }
  1877. /* Return the maximum of X and Y, treating both as unsigned values. */
  1878. template <typename T1, typename T2>
  1879. inline WI_BINARY_RESULT (T1, T2)
  1880. wi::umax (const T1 &x, const T2 &y)
  1881. {
  1882. return wi::max (x, y, UNSIGNED);
  1883. }
  1884. /* Return X & Y. */
  1885. template <typename T1, typename T2>
  1886. inline WI_BINARY_RESULT (T1, T2)
  1887. wi::bit_and (const T1 &x, const T2 &y)
  1888. {
  1889. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1890. unsigned int precision = get_precision (result);
  1891. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1892. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1893. bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
  1894. if (__builtin_expect (xi.len + yi.len == 2, true))
  1895. {
  1896. val[0] = xi.ulow () & yi.ulow ();
  1897. result.set_len (1, is_sign_extended);
  1898. }
  1899. else
  1900. result.set_len (and_large (val, xi.val, xi.len, yi.val, yi.len,
  1901. precision), is_sign_extended);
  1902. return result;
  1903. }
  1904. /* Return X & ~Y. */
  1905. template <typename T1, typename T2>
  1906. inline WI_BINARY_RESULT (T1, T2)
  1907. wi::bit_and_not (const T1 &x, const T2 &y)
  1908. {
  1909. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1910. unsigned int precision = get_precision (result);
  1911. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1912. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1913. bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
  1914. if (__builtin_expect (xi.len + yi.len == 2, true))
  1915. {
  1916. val[0] = xi.ulow () & ~yi.ulow ();
  1917. result.set_len (1, is_sign_extended);
  1918. }
  1919. else
  1920. result.set_len (and_not_large (val, xi.val, xi.len, yi.val, yi.len,
  1921. precision), is_sign_extended);
  1922. return result;
  1923. }
  1924. /* Return X | Y. */
  1925. template <typename T1, typename T2>
  1926. inline WI_BINARY_RESULT (T1, T2)
  1927. wi::bit_or (const T1 &x, const T2 &y)
  1928. {
  1929. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1930. unsigned int precision = get_precision (result);
  1931. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1932. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1933. bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
  1934. if (__builtin_expect (xi.len + yi.len == 2, true))
  1935. {
  1936. val[0] = xi.ulow () | yi.ulow ();
  1937. result.set_len (1, is_sign_extended);
  1938. }
  1939. else
  1940. result.set_len (or_large (val, xi.val, xi.len,
  1941. yi.val, yi.len, precision), is_sign_extended);
  1942. return result;
  1943. }
  1944. /* Return X | ~Y. */
  1945. template <typename T1, typename T2>
  1946. inline WI_BINARY_RESULT (T1, T2)
  1947. wi::bit_or_not (const T1 &x, const T2 &y)
  1948. {
  1949. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1950. unsigned int precision = get_precision (result);
  1951. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1952. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1953. bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
  1954. if (__builtin_expect (xi.len + yi.len == 2, true))
  1955. {
  1956. val[0] = xi.ulow () | ~yi.ulow ();
  1957. result.set_len (1, is_sign_extended);
  1958. }
  1959. else
  1960. result.set_len (or_not_large (val, xi.val, xi.len, yi.val, yi.len,
  1961. precision), is_sign_extended);
  1962. return result;
  1963. }
  1964. /* Return X ^ Y. */
  1965. template <typename T1, typename T2>
  1966. inline WI_BINARY_RESULT (T1, T2)
  1967. wi::bit_xor (const T1 &x, const T2 &y)
  1968. {
  1969. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1970. unsigned int precision = get_precision (result);
  1971. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1972. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1973. bool is_sign_extended = xi.is_sign_extended && yi.is_sign_extended;
  1974. if (__builtin_expect (xi.len + yi.len == 2, true))
  1975. {
  1976. val[0] = xi.ulow () ^ yi.ulow ();
  1977. result.set_len (1, is_sign_extended);
  1978. }
  1979. else
  1980. result.set_len (xor_large (val, xi.val, xi.len,
  1981. yi.val, yi.len, precision), is_sign_extended);
  1982. return result;
  1983. }
  1984. /* Return X + Y. */
  1985. template <typename T1, typename T2>
  1986. inline WI_BINARY_RESULT (T1, T2)
  1987. wi::add (const T1 &x, const T2 &y)
  1988. {
  1989. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  1990. unsigned int precision = get_precision (result);
  1991. WIDE_INT_REF_FOR (T1) xi (x, precision);
  1992. WIDE_INT_REF_FOR (T2) yi (y, precision);
  1993. if (precision <= HOST_BITS_PER_WIDE_INT)
  1994. {
  1995. val[0] = xi.ulow () + yi.ulow ();
  1996. result.set_len (1);
  1997. }
  1998. /* If the precision is known at compile time to be greater than
  1999. HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
  2000. knowing that (a) all bits in those HWIs are significant and
  2001. (b) the result has room for at least two HWIs. This provides
  2002. a fast path for things like offset_int and widest_int.
  2003. The STATIC_CONSTANT_P test prevents this path from being
  2004. used for wide_ints. wide_ints with precisions greater than
  2005. HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
  2006. point handling them inline. */
  2007. else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
  2008. && __builtin_expect (xi.len + yi.len == 2, true))
  2009. {
  2010. unsigned HOST_WIDE_INT xl = xi.ulow ();
  2011. unsigned HOST_WIDE_INT yl = yi.ulow ();
  2012. unsigned HOST_WIDE_INT resultl = xl + yl;
  2013. val[0] = resultl;
  2014. val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
  2015. result.set_len (1 + (((resultl ^ xl) & (resultl ^ yl))
  2016. >> (HOST_BITS_PER_WIDE_INT - 1)));
  2017. }
  2018. else
  2019. result.set_len (add_large (val, xi.val, xi.len,
  2020. yi.val, yi.len, precision,
  2021. UNSIGNED, 0));
  2022. return result;
  2023. }
  2024. /* Return X + Y. Treat X and Y as having the signednes given by SGN
  2025. and indicate in *OVERFLOW whether the operation overflowed. */
  2026. template <typename T1, typename T2>
  2027. inline WI_BINARY_RESULT (T1, T2)
  2028. wi::add (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2029. {
  2030. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2031. unsigned int precision = get_precision (result);
  2032. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2033. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2034. if (precision <= HOST_BITS_PER_WIDE_INT)
  2035. {
  2036. unsigned HOST_WIDE_INT xl = xi.ulow ();
  2037. unsigned HOST_WIDE_INT yl = yi.ulow ();
  2038. unsigned HOST_WIDE_INT resultl = xl + yl;
  2039. if (sgn == SIGNED)
  2040. *overflow = (((resultl ^ xl) & (resultl ^ yl))
  2041. >> (precision - 1)) & 1;
  2042. else
  2043. *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
  2044. < (xl << (HOST_BITS_PER_WIDE_INT - precision)));
  2045. val[0] = resultl;
  2046. result.set_len (1);
  2047. }
  2048. else
  2049. result.set_len (add_large (val, xi.val, xi.len,
  2050. yi.val, yi.len, precision,
  2051. sgn, overflow));
  2052. return result;
  2053. }
  2054. /* Return X - Y. */
  2055. template <typename T1, typename T2>
  2056. inline WI_BINARY_RESULT (T1, T2)
  2057. wi::sub (const T1 &x, const T2 &y)
  2058. {
  2059. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2060. unsigned int precision = get_precision (result);
  2061. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2062. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2063. if (precision <= HOST_BITS_PER_WIDE_INT)
  2064. {
  2065. val[0] = xi.ulow () - yi.ulow ();
  2066. result.set_len (1);
  2067. }
  2068. /* If the precision is known at compile time to be greater than
  2069. HOST_BITS_PER_WIDE_INT, we can optimize the single-HWI case
  2070. knowing that (a) all bits in those HWIs are significant and
  2071. (b) the result has room for at least two HWIs. This provides
  2072. a fast path for things like offset_int and widest_int.
  2073. The STATIC_CONSTANT_P test prevents this path from being
  2074. used for wide_ints. wide_ints with precisions greater than
  2075. HOST_BITS_PER_WIDE_INT are relatively rare and there's not much
  2076. point handling them inline. */
  2077. else if (STATIC_CONSTANT_P (precision > HOST_BITS_PER_WIDE_INT)
  2078. && __builtin_expect (xi.len + yi.len == 2, true))
  2079. {
  2080. unsigned HOST_WIDE_INT xl = xi.ulow ();
  2081. unsigned HOST_WIDE_INT yl = yi.ulow ();
  2082. unsigned HOST_WIDE_INT resultl = xl - yl;
  2083. val[0] = resultl;
  2084. val[1] = (HOST_WIDE_INT) resultl < 0 ? 0 : -1;
  2085. result.set_len (1 + (((resultl ^ xl) & (xl ^ yl))
  2086. >> (HOST_BITS_PER_WIDE_INT - 1)));
  2087. }
  2088. else
  2089. result.set_len (sub_large (val, xi.val, xi.len,
  2090. yi.val, yi.len, precision,
  2091. UNSIGNED, 0));
  2092. return result;
  2093. }
  2094. /* Return X - Y. Treat X and Y as having the signednes given by SGN
  2095. and indicate in *OVERFLOW whether the operation overflowed. */
  2096. template <typename T1, typename T2>
  2097. inline WI_BINARY_RESULT (T1, T2)
  2098. wi::sub (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2099. {
  2100. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2101. unsigned int precision = get_precision (result);
  2102. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2103. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2104. if (precision <= HOST_BITS_PER_WIDE_INT)
  2105. {
  2106. unsigned HOST_WIDE_INT xl = xi.ulow ();
  2107. unsigned HOST_WIDE_INT yl = yi.ulow ();
  2108. unsigned HOST_WIDE_INT resultl = xl - yl;
  2109. if (sgn == SIGNED)
  2110. *overflow = (((xl ^ yl) & (resultl ^ xl)) >> (precision - 1)) & 1;
  2111. else
  2112. *overflow = ((resultl << (HOST_BITS_PER_WIDE_INT - precision))
  2113. > (xl << (HOST_BITS_PER_WIDE_INT - precision)));
  2114. val[0] = resultl;
  2115. result.set_len (1);
  2116. }
  2117. else
  2118. result.set_len (sub_large (val, xi.val, xi.len,
  2119. yi.val, yi.len, precision,
  2120. sgn, overflow));
  2121. return result;
  2122. }
  2123. /* Return X * Y. */
  2124. template <typename T1, typename T2>
  2125. inline WI_BINARY_RESULT (T1, T2)
  2126. wi::mul (const T1 &x, const T2 &y)
  2127. {
  2128. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2129. unsigned int precision = get_precision (result);
  2130. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2131. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2132. if (precision <= HOST_BITS_PER_WIDE_INT)
  2133. {
  2134. val[0] = xi.ulow () * yi.ulow ();
  2135. result.set_len (1);
  2136. }
  2137. else
  2138. result.set_len (mul_internal (val, xi.val, xi.len, yi.val, yi.len,
  2139. precision, UNSIGNED, 0, false));
  2140. return result;
  2141. }
  2142. /* Return X * Y. Treat X and Y as having the signednes given by SGN
  2143. and indicate in *OVERFLOW whether the operation overflowed. */
  2144. template <typename T1, typename T2>
  2145. inline WI_BINARY_RESULT (T1, T2)
  2146. wi::mul (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2147. {
  2148. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2149. unsigned int precision = get_precision (result);
  2150. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2151. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2152. result.set_len (mul_internal (val, xi.val, xi.len,
  2153. yi.val, yi.len, precision,
  2154. sgn, overflow, false));
  2155. return result;
  2156. }
  2157. /* Return X * Y, treating both X and Y as signed values. Indicate in
  2158. *OVERFLOW whether the operation overflowed. */
  2159. template <typename T1, typename T2>
  2160. inline WI_BINARY_RESULT (T1, T2)
  2161. wi::smul (const T1 &x, const T2 &y, bool *overflow)
  2162. {
  2163. return mul (x, y, SIGNED, overflow);
  2164. }
  2165. /* Return X * Y, treating both X and Y as unsigned values. Indicate in
  2166. *OVERFLOW whether the operation overflowed. */
  2167. template <typename T1, typename T2>
  2168. inline WI_BINARY_RESULT (T1, T2)
  2169. wi::umul (const T1 &x, const T2 &y, bool *overflow)
  2170. {
  2171. return mul (x, y, UNSIGNED, overflow);
  2172. }
  2173. /* Perform a widening multiplication of X and Y, extending the values
  2174. according to SGN, and return the high part of the result. */
  2175. template <typename T1, typename T2>
  2176. inline WI_BINARY_RESULT (T1, T2)
  2177. wi::mul_high (const T1 &x, const T2 &y, signop sgn)
  2178. {
  2179. WI_BINARY_RESULT_VAR (result, val, T1, x, T2, y);
  2180. unsigned int precision = get_precision (result);
  2181. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2182. WIDE_INT_REF_FOR (T2) yi (y, precision);
  2183. result.set_len (mul_internal (val, xi.val, xi.len,
  2184. yi.val, yi.len, precision,
  2185. sgn, 0, true));
  2186. return result;
  2187. }
  2188. /* Return X / Y, rouding towards 0. Treat X and Y as having the
  2189. signedness given by SGN. Indicate in *OVERFLOW if the result
  2190. overflows. */
  2191. template <typename T1, typename T2>
  2192. inline WI_BINARY_RESULT (T1, T2)
  2193. wi::div_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2194. {
  2195. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2196. unsigned int precision = get_precision (quotient);
  2197. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2198. WIDE_INT_REF_FOR (T2) yi (y);
  2199. quotient.set_len (divmod_internal (quotient_val, 0, 0, xi.val, xi.len,
  2200. precision,
  2201. yi.val, yi.len, yi.precision,
  2202. sgn, overflow));
  2203. return quotient;
  2204. }
  2205. /* Return X / Y, rouding towards 0. Treat X and Y as signed values. */
  2206. template <typename T1, typename T2>
  2207. inline WI_BINARY_RESULT (T1, T2)
  2208. wi::sdiv_trunc (const T1 &x, const T2 &y)
  2209. {
  2210. return div_trunc (x, y, SIGNED);
  2211. }
  2212. /* Return X / Y, rouding towards 0. Treat X and Y as unsigned values. */
  2213. template <typename T1, typename T2>
  2214. inline WI_BINARY_RESULT (T1, T2)
  2215. wi::udiv_trunc (const T1 &x, const T2 &y)
  2216. {
  2217. return div_trunc (x, y, UNSIGNED);
  2218. }
  2219. /* Return X / Y, rouding towards -inf. Treat X and Y as having the
  2220. signedness given by SGN. Indicate in *OVERFLOW if the result
  2221. overflows. */
  2222. template <typename T1, typename T2>
  2223. inline WI_BINARY_RESULT (T1, T2)
  2224. wi::div_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2225. {
  2226. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2227. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2228. unsigned int precision = get_precision (quotient);
  2229. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2230. WIDE_INT_REF_FOR (T2) yi (y);
  2231. unsigned int remainder_len;
  2232. quotient.set_len (divmod_internal (quotient_val,
  2233. &remainder_len, remainder_val,
  2234. xi.val, xi.len, precision,
  2235. yi.val, yi.len, yi.precision, sgn,
  2236. overflow));
  2237. remainder.set_len (remainder_len);
  2238. if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
  2239. return quotient - 1;
  2240. return quotient;
  2241. }
  2242. /* Return X / Y, rouding towards -inf. Treat X and Y as signed values. */
  2243. template <typename T1, typename T2>
  2244. inline WI_BINARY_RESULT (T1, T2)
  2245. wi::sdiv_floor (const T1 &x, const T2 &y)
  2246. {
  2247. return div_floor (x, y, SIGNED);
  2248. }
  2249. /* Return X / Y, rouding towards -inf. Treat X and Y as unsigned values. */
  2250. /* ??? Why do we have both this and udiv_trunc. Aren't they the same? */
  2251. template <typename T1, typename T2>
  2252. inline WI_BINARY_RESULT (T1, T2)
  2253. wi::udiv_floor (const T1 &x, const T2 &y)
  2254. {
  2255. return div_floor (x, y, UNSIGNED);
  2256. }
  2257. /* Return X / Y, rouding towards +inf. Treat X and Y as having the
  2258. signedness given by SGN. Indicate in *OVERFLOW if the result
  2259. overflows. */
  2260. template <typename T1, typename T2>
  2261. inline WI_BINARY_RESULT (T1, T2)
  2262. wi::div_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2263. {
  2264. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2265. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2266. unsigned int precision = get_precision (quotient);
  2267. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2268. WIDE_INT_REF_FOR (T2) yi (y);
  2269. unsigned int remainder_len;
  2270. quotient.set_len (divmod_internal (quotient_val,
  2271. &remainder_len, remainder_val,
  2272. xi.val, xi.len, precision,
  2273. yi.val, yi.len, yi.precision, sgn,
  2274. overflow));
  2275. remainder.set_len (remainder_len);
  2276. if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
  2277. return quotient + 1;
  2278. return quotient;
  2279. }
  2280. /* Return X / Y, rouding towards nearest with ties away from zero.
  2281. Treat X and Y as having the signedness given by SGN. Indicate
  2282. in *OVERFLOW if the result overflows. */
  2283. template <typename T1, typename T2>
  2284. inline WI_BINARY_RESULT (T1, T2)
  2285. wi::div_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2286. {
  2287. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2288. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2289. unsigned int precision = get_precision (quotient);
  2290. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2291. WIDE_INT_REF_FOR (T2) yi (y);
  2292. unsigned int remainder_len;
  2293. quotient.set_len (divmod_internal (quotient_val,
  2294. &remainder_len, remainder_val,
  2295. xi.val, xi.len, precision,
  2296. yi.val, yi.len, yi.precision, sgn,
  2297. overflow));
  2298. remainder.set_len (remainder_len);
  2299. if (remainder != 0)
  2300. {
  2301. if (sgn == SIGNED)
  2302. {
  2303. WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
  2304. if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
  2305. {
  2306. if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
  2307. return quotient - 1;
  2308. else
  2309. return quotient + 1;
  2310. }
  2311. }
  2312. else
  2313. {
  2314. if (wi::geu_p (remainder, wi::sub (y, remainder)))
  2315. return quotient + 1;
  2316. }
  2317. }
  2318. return quotient;
  2319. }
  2320. /* Return X / Y, rouding towards 0. Treat X and Y as having the
  2321. signedness given by SGN. Store the remainder in *REMAINDER_PTR. */
  2322. template <typename T1, typename T2>
  2323. inline WI_BINARY_RESULT (T1, T2)
  2324. wi::divmod_trunc (const T1 &x, const T2 &y, signop sgn,
  2325. WI_BINARY_RESULT (T1, T2) *remainder_ptr)
  2326. {
  2327. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2328. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2329. unsigned int precision = get_precision (quotient);
  2330. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2331. WIDE_INT_REF_FOR (T2) yi (y);
  2332. unsigned int remainder_len;
  2333. quotient.set_len (divmod_internal (quotient_val,
  2334. &remainder_len, remainder_val,
  2335. xi.val, xi.len, precision,
  2336. yi.val, yi.len, yi.precision, sgn, 0));
  2337. remainder.set_len (remainder_len);
  2338. *remainder_ptr = remainder;
  2339. return quotient;
  2340. }
  2341. /* Compute X / Y, rouding towards 0, and return the remainder.
  2342. Treat X and Y as having the signedness given by SGN. Indicate
  2343. in *OVERFLOW if the division overflows. */
  2344. template <typename T1, typename T2>
  2345. inline WI_BINARY_RESULT (T1, T2)
  2346. wi::mod_trunc (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2347. {
  2348. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2349. unsigned int precision = get_precision (remainder);
  2350. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2351. WIDE_INT_REF_FOR (T2) yi (y);
  2352. unsigned int remainder_len;
  2353. divmod_internal (0, &remainder_len, remainder_val,
  2354. xi.val, xi.len, precision,
  2355. yi.val, yi.len, yi.precision, sgn, overflow);
  2356. remainder.set_len (remainder_len);
  2357. return remainder;
  2358. }
  2359. /* Compute X / Y, rouding towards 0, and return the remainder.
  2360. Treat X and Y as signed values. */
  2361. template <typename T1, typename T2>
  2362. inline WI_BINARY_RESULT (T1, T2)
  2363. wi::smod_trunc (const T1 &x, const T2 &y)
  2364. {
  2365. return mod_trunc (x, y, SIGNED);
  2366. }
  2367. /* Compute X / Y, rouding towards 0, and return the remainder.
  2368. Treat X and Y as unsigned values. */
  2369. template <typename T1, typename T2>
  2370. inline WI_BINARY_RESULT (T1, T2)
  2371. wi::umod_trunc (const T1 &x, const T2 &y)
  2372. {
  2373. return mod_trunc (x, y, UNSIGNED);
  2374. }
  2375. /* Compute X / Y, rouding towards -inf, and return the remainder.
  2376. Treat X and Y as having the signedness given by SGN. Indicate
  2377. in *OVERFLOW if the division overflows. */
  2378. template <typename T1, typename T2>
  2379. inline WI_BINARY_RESULT (T1, T2)
  2380. wi::mod_floor (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2381. {
  2382. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2383. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2384. unsigned int precision = get_precision (quotient);
  2385. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2386. WIDE_INT_REF_FOR (T2) yi (y);
  2387. unsigned int remainder_len;
  2388. quotient.set_len (divmod_internal (quotient_val,
  2389. &remainder_len, remainder_val,
  2390. xi.val, xi.len, precision,
  2391. yi.val, yi.len, yi.precision, sgn,
  2392. overflow));
  2393. remainder.set_len (remainder_len);
  2394. if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn) && remainder != 0)
  2395. return remainder + y;
  2396. return remainder;
  2397. }
  2398. /* Compute X / Y, rouding towards -inf, and return the remainder.
  2399. Treat X and Y as unsigned values. */
  2400. /* ??? Why do we have both this and umod_trunc. Aren't they the same? */
  2401. template <typename T1, typename T2>
  2402. inline WI_BINARY_RESULT (T1, T2)
  2403. wi::umod_floor (const T1 &x, const T2 &y)
  2404. {
  2405. return mod_floor (x, y, UNSIGNED);
  2406. }
  2407. /* Compute X / Y, rouding towards +inf, and return the remainder.
  2408. Treat X and Y as having the signedness given by SGN. Indicate
  2409. in *OVERFLOW if the division overflows. */
  2410. template <typename T1, typename T2>
  2411. inline WI_BINARY_RESULT (T1, T2)
  2412. wi::mod_ceil (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2413. {
  2414. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2415. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2416. unsigned int precision = get_precision (quotient);
  2417. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2418. WIDE_INT_REF_FOR (T2) yi (y);
  2419. unsigned int remainder_len;
  2420. quotient.set_len (divmod_internal (quotient_val,
  2421. &remainder_len, remainder_val,
  2422. xi.val, xi.len, precision,
  2423. yi.val, yi.len, yi.precision, sgn,
  2424. overflow));
  2425. remainder.set_len (remainder_len);
  2426. if (wi::neg_p (x, sgn) == wi::neg_p (y, sgn) && remainder != 0)
  2427. return remainder - y;
  2428. return remainder;
  2429. }
  2430. /* Compute X / Y, rouding towards nearest with ties away from zero,
  2431. and return the remainder. Treat X and Y as having the signedness
  2432. given by SGN. Indicate in *OVERFLOW if the division overflows. */
  2433. template <typename T1, typename T2>
  2434. inline WI_BINARY_RESULT (T1, T2)
  2435. wi::mod_round (const T1 &x, const T2 &y, signop sgn, bool *overflow)
  2436. {
  2437. WI_BINARY_RESULT_VAR (quotient, quotient_val, T1, x, T2, y);
  2438. WI_BINARY_RESULT_VAR (remainder, remainder_val, T1, x, T2, y);
  2439. unsigned int precision = get_precision (quotient);
  2440. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2441. WIDE_INT_REF_FOR (T2) yi (y);
  2442. unsigned int remainder_len;
  2443. quotient.set_len (divmod_internal (quotient_val,
  2444. &remainder_len, remainder_val,
  2445. xi.val, xi.len, precision,
  2446. yi.val, yi.len, yi.precision, sgn,
  2447. overflow));
  2448. remainder.set_len (remainder_len);
  2449. if (remainder != 0)
  2450. {
  2451. if (sgn == SIGNED)
  2452. {
  2453. WI_BINARY_RESULT (T1, T2) abs_remainder = wi::abs (remainder);
  2454. if (wi::geu_p (abs_remainder, wi::sub (wi::abs (y), abs_remainder)))
  2455. {
  2456. if (wi::neg_p (x, sgn) != wi::neg_p (y, sgn))
  2457. return remainder + y;
  2458. else
  2459. return remainder - y;
  2460. }
  2461. }
  2462. else
  2463. {
  2464. if (wi::geu_p (remainder, wi::sub (y, remainder)))
  2465. return remainder - y;
  2466. }
  2467. }
  2468. return remainder;
  2469. }
  2470. /* Return true if X is a multiple of Y. Treat X and Y as having the
  2471. signedness given by SGN. */
  2472. template <typename T1, typename T2>
  2473. inline bool
  2474. wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn)
  2475. {
  2476. return wi::mod_trunc (x, y, sgn) == 0;
  2477. }
  2478. /* Return true if X is a multiple of Y, storing X / Y in *RES if so.
  2479. Treat X and Y as having the signedness given by SGN. */
  2480. template <typename T1, typename T2>
  2481. inline bool
  2482. wi::multiple_of_p (const T1 &x, const T2 &y, signop sgn,
  2483. WI_BINARY_RESULT (T1, T2) *res)
  2484. {
  2485. WI_BINARY_RESULT (T1, T2) remainder;
  2486. WI_BINARY_RESULT (T1, T2) quotient
  2487. = divmod_trunc (x, y, sgn, &remainder);
  2488. if (remainder == 0)
  2489. {
  2490. *res = quotient;
  2491. return true;
  2492. }
  2493. return false;
  2494. }
  2495. /* Return X << Y. Return 0 if Y is greater than or equal to
  2496. the precision of X. */
  2497. template <typename T1, typename T2>
  2498. inline WI_UNARY_RESULT (T1)
  2499. wi::lshift (const T1 &x, const T2 &y)
  2500. {
  2501. WI_UNARY_RESULT_VAR (result, val, T1, x);
  2502. unsigned int precision = get_precision (result);
  2503. WIDE_INT_REF_FOR (T1) xi (x, precision);
  2504. WIDE_INT_REF_FOR (T2) yi (y);
  2505. /* Handle the simple cases quickly. */
  2506. if (geu_p (yi, precision))
  2507. {
  2508. val[0] = 0;
  2509. result.set_len (1);
  2510. }
  2511. else
  2512. {
  2513. unsigned int shift = yi.to_uhwi ();
  2514. /* For fixed-precision integers like offset_int and widest_int,
  2515. handle the case where the shift value is constant and the
  2516. result is a single nonnegative HWI (meaning that we don't
  2517. need to worry about val[1]). This is particularly common
  2518. for converting a byte count to a bit count.
  2519. For variable-precision integers like wide_int, handle HWI
  2520. and sub-HWI integers inline. */
  2521. if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
  2522. ? (STATIC_CONSTANT_P (shift < HOST_BITS_PER_WIDE_INT - 1)
  2523. && xi.len == 1
  2524. && xi.val[0] <= (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
  2525. HOST_WIDE_INT_MAX >> shift))
  2526. : precision <= HOST_BITS_PER_WIDE_INT)
  2527. {
  2528. val[0] = xi.ulow () << shift;
  2529. result.set_len (1);
  2530. }
  2531. else
  2532. result.set_len (lshift_large (val, xi.val, xi.len,
  2533. precision, shift));
  2534. }
  2535. return result;
  2536. }
  2537. /* Return X >> Y, using a logical shift. Return 0 if Y is greater than
  2538. or equal to the precision of X. */
  2539. template <typename T1, typename T2>
  2540. inline WI_UNARY_RESULT (T1)
  2541. wi::lrshift (const T1 &x, const T2 &y)
  2542. {
  2543. WI_UNARY_RESULT_VAR (result, val, T1, x);
  2544. /* Do things in the precision of the input rather than the output,
  2545. since the result can be no larger than that. */
  2546. WIDE_INT_REF_FOR (T1) xi (x);
  2547. WIDE_INT_REF_FOR (T2) yi (y);
  2548. /* Handle the simple cases quickly. */
  2549. if (geu_p (yi, xi.precision))
  2550. {
  2551. val[0] = 0;
  2552. result.set_len (1);
  2553. }
  2554. else
  2555. {
  2556. unsigned int shift = yi.to_uhwi ();
  2557. /* For fixed-precision integers like offset_int and widest_int,
  2558. handle the case where the shift value is constant and the
  2559. shifted value is a single nonnegative HWI (meaning that all
  2560. bits above the HWI are zero). This is particularly common
  2561. for converting a bit count to a byte count.
  2562. For variable-precision integers like wide_int, handle HWI
  2563. and sub-HWI integers inline. */
  2564. if (STATIC_CONSTANT_P (xi.precision > HOST_BITS_PER_WIDE_INT)
  2565. ? xi.len == 1 && xi.val[0] >= 0
  2566. : xi.precision <= HOST_BITS_PER_WIDE_INT)
  2567. {
  2568. val[0] = xi.to_uhwi () >> shift;
  2569. result.set_len (1);
  2570. }
  2571. else
  2572. result.set_len (lrshift_large (val, xi.val, xi.len, xi.precision,
  2573. get_precision (result), shift));
  2574. }
  2575. return result;
  2576. }
  2577. /* Return X >> Y, using an arithmetic shift. Return a sign mask if
  2578. Y is greater than or equal to the precision of X. */
  2579. template <typename T1, typename T2>
  2580. inline WI_UNARY_RESULT (T1)
  2581. wi::arshift (const T1 &x, const T2 &y)
  2582. {
  2583. WI_UNARY_RESULT_VAR (result, val, T1, x);
  2584. /* Do things in the precision of the input rather than the output,
  2585. since the result can be no larger than that. */
  2586. WIDE_INT_REF_FOR (T1) xi (x);
  2587. WIDE_INT_REF_FOR (T2) yi (y);
  2588. /* Handle the simple cases quickly. */
  2589. if (geu_p (yi, xi.precision))
  2590. {
  2591. val[0] = sign_mask (x);
  2592. result.set_len (1);
  2593. }
  2594. else
  2595. {
  2596. unsigned int shift = yi.to_uhwi ();
  2597. if (xi.precision <= HOST_BITS_PER_WIDE_INT)
  2598. {
  2599. val[0] = sext_hwi (xi.ulow () >> shift, xi.precision - shift);
  2600. result.set_len (1, true);
  2601. }
  2602. else
  2603. result.set_len (arshift_large (val, xi.val, xi.len, xi.precision,
  2604. get_precision (result), shift));
  2605. }
  2606. return result;
  2607. }
  2608. /* Return X >> Y, using an arithmetic shift if SGN is SIGNED and a
  2609. logical shift otherwise. */
  2610. template <typename T1, typename T2>
  2611. inline WI_UNARY_RESULT (T1)
  2612. wi::rshift (const T1 &x, const T2 &y, signop sgn)
  2613. {
  2614. if (sgn == UNSIGNED)
  2615. return lrshift (x, y);
  2616. else
  2617. return arshift (x, y);
  2618. }
  2619. /* Return the result of rotating the low WIDTH bits of X left by Y
  2620. bits and zero-extending the result. Use a full-width rotate if
  2621. WIDTH is zero. */
  2622. template <typename T1, typename T2>
  2623. WI_UNARY_RESULT (T1)
  2624. wi::lrotate (const T1 &x, const T2 &y, unsigned int width)
  2625. {
  2626. unsigned int precision = get_binary_precision (x, x);
  2627. if (width == 0)
  2628. width = precision;
  2629. WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
  2630. WI_UNARY_RESULT (T1) left = wi::lshift (x, ymod);
  2631. WI_UNARY_RESULT (T1) right = wi::lrshift (x, wi::sub (width, ymod));
  2632. if (width != precision)
  2633. return wi::zext (left, width) | wi::zext (right, width);
  2634. return left | right;
  2635. }
  2636. /* Return the result of rotating the low WIDTH bits of X right by Y
  2637. bits and zero-extending the result. Use a full-width rotate if
  2638. WIDTH is zero. */
  2639. template <typename T1, typename T2>
  2640. WI_UNARY_RESULT (T1)
  2641. wi::rrotate (const T1 &x, const T2 &y, unsigned int width)
  2642. {
  2643. unsigned int precision = get_binary_precision (x, x);
  2644. if (width == 0)
  2645. width = precision;
  2646. WI_UNARY_RESULT (T2) ymod = umod_trunc (y, width);
  2647. WI_UNARY_RESULT (T1) right = wi::lrshift (x, ymod);
  2648. WI_UNARY_RESULT (T1) left = wi::lshift (x, wi::sub (width, ymod));
  2649. if (width != precision)
  2650. return wi::zext (left, width) | wi::zext (right, width);
  2651. return left | right;
  2652. }
  2653. /* Return 0 if the number of 1s in X is even and 1 if the number of 1s
  2654. is odd. */
  2655. inline int
  2656. wi::parity (const wide_int_ref &x)
  2657. {
  2658. return popcount (x) & 1;
  2659. }
  2660. /* Extract WIDTH bits from X, starting at BITPOS. */
  2661. template <typename T>
  2662. inline unsigned HOST_WIDE_INT
  2663. wi::extract_uhwi (const T &x, unsigned int bitpos, unsigned int width)
  2664. {
  2665. unsigned precision = get_precision (x);
  2666. if (precision < bitpos + width)
  2667. precision = bitpos + width;
  2668. WIDE_INT_REF_FOR (T) xi (x, precision);
  2669. /* Handle this rare case after the above, so that we assert about
  2670. bogus BITPOS values. */
  2671. if (width == 0)
  2672. return 0;
  2673. unsigned int start = bitpos / HOST_BITS_PER_WIDE_INT;
  2674. unsigned int shift = bitpos % HOST_BITS_PER_WIDE_INT;
  2675. unsigned HOST_WIDE_INT res = xi.elt (start);
  2676. res >>= shift;
  2677. if (shift + width > HOST_BITS_PER_WIDE_INT)
  2678. {
  2679. unsigned HOST_WIDE_INT upper = xi.elt (start + 1);
  2680. res |= upper << (-shift % HOST_BITS_PER_WIDE_INT);
  2681. }
  2682. return zext_hwi (res, width);
  2683. }
  2684. /* Return the minimum precision needed to store X with sign SGN. */
  2685. template <typename T>
  2686. inline unsigned int
  2687. wi::min_precision (const T &x, signop sgn)
  2688. {
  2689. if (sgn == SIGNED)
  2690. return get_precision (x) - clrsb (x);
  2691. else
  2692. return get_precision (x) - clz (x);
  2693. }
  2694. template<typename T>
  2695. void
  2696. gt_ggc_mx (generic_wide_int <T> *)
  2697. {
  2698. }
  2699. template<typename T>
  2700. void
  2701. gt_pch_nx (generic_wide_int <T> *)
  2702. {
  2703. }
  2704. template<typename T>
  2705. void
  2706. gt_pch_nx (generic_wide_int <T> *, void (*) (void *, void *), void *)
  2707. {
  2708. }
  2709. template<int N>
  2710. void
  2711. gt_ggc_mx (trailing_wide_ints <N> *)
  2712. {
  2713. }
  2714. template<int N>
  2715. void
  2716. gt_pch_nx (trailing_wide_ints <N> *)
  2717. {
  2718. }
  2719. template<int N>
  2720. void
  2721. gt_pch_nx (trailing_wide_ints <N> *, void (*) (void *, void *), void *)
  2722. {
  2723. }
  2724. namespace wi
  2725. {
  2726. /* Used for overloaded functions in which the only other acceptable
  2727. scalar type is a pointer. It stops a plain 0 from being treated
  2728. as a null pointer. */
  2729. struct never_used1 {};
  2730. struct never_used2 {};
  2731. wide_int min_value (unsigned int, signop);
  2732. wide_int min_value (never_used1 *);
  2733. wide_int min_value (never_used2 *);
  2734. wide_int max_value (unsigned int, signop);
  2735. wide_int max_value (never_used1 *);
  2736. wide_int max_value (never_used2 *);
  2737. /* FIXME: this is target dependent, so should be elsewhere.
  2738. It also seems to assume that CHAR_BIT == BITS_PER_UNIT. */
  2739. wide_int from_buffer (const unsigned char *, unsigned int);
  2740. #ifndef GENERATOR_FILE
  2741. void to_mpz (const wide_int_ref &, mpz_t, signop);
  2742. #endif
  2743. wide_int mask (unsigned int, bool, unsigned int);
  2744. wide_int shifted_mask (unsigned int, unsigned int, bool, unsigned int);
  2745. wide_int set_bit_in_zero (unsigned int, unsigned int);
  2746. wide_int insert (const wide_int &x, const wide_int &y, unsigned int,
  2747. unsigned int);
  2748. template <typename T>
  2749. T mask (unsigned int, bool);
  2750. template <typename T>
  2751. T shifted_mask (unsigned int, unsigned int, bool);
  2752. template <typename T>
  2753. T set_bit_in_zero (unsigned int);
  2754. unsigned int mask (HOST_WIDE_INT *, unsigned int, bool, unsigned int);
  2755. unsigned int shifted_mask (HOST_WIDE_INT *, unsigned int, unsigned int,
  2756. bool, unsigned int);
  2757. unsigned int from_array (HOST_WIDE_INT *, const HOST_WIDE_INT *,
  2758. unsigned int, unsigned int, bool);
  2759. }
  2760. /* Return a PRECISION-bit integer in which the low WIDTH bits are set
  2761. and the other bits are clear, or the inverse if NEGATE_P. */
  2762. inline wide_int
  2763. wi::mask (unsigned int width, bool negate_p, unsigned int precision)
  2764. {
  2765. wide_int result = wide_int::create (precision);
  2766. result.set_len (mask (result.write_val (), width, negate_p, precision));
  2767. return result;
  2768. }
  2769. /* Return a PRECISION-bit integer in which the low START bits are clear,
  2770. the next WIDTH bits are set, and the other bits are clear,
  2771. or the inverse if NEGATE_P. */
  2772. inline wide_int
  2773. wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p,
  2774. unsigned int precision)
  2775. {
  2776. wide_int result = wide_int::create (precision);
  2777. result.set_len (shifted_mask (result.write_val (), start, width, negate_p,
  2778. precision));
  2779. return result;
  2780. }
  2781. /* Return a PRECISION-bit integer in which bit BIT is set and all the
  2782. others are clear. */
  2783. inline wide_int
  2784. wi::set_bit_in_zero (unsigned int bit, unsigned int precision)
  2785. {
  2786. return shifted_mask (bit, 1, false, precision);
  2787. }
  2788. /* Return an integer of type T in which the low WIDTH bits are set
  2789. and the other bits are clear, or the inverse if NEGATE_P. */
  2790. template <typename T>
  2791. inline T
  2792. wi::mask (unsigned int width, bool negate_p)
  2793. {
  2794. STATIC_ASSERT (wi::int_traits<T>::precision);
  2795. T result;
  2796. result.set_len (mask (result.write_val (), width, negate_p,
  2797. wi::int_traits <T>::precision));
  2798. return result;
  2799. }
  2800. /* Return an integer of type T in which the low START bits are clear,
  2801. the next WIDTH bits are set, and the other bits are clear, or the
  2802. inverse if NEGATE_P. */
  2803. template <typename T>
  2804. inline T
  2805. wi::shifted_mask (unsigned int start, unsigned int width, bool negate_p)
  2806. {
  2807. STATIC_ASSERT (wi::int_traits<T>::precision);
  2808. T result;
  2809. result.set_len (shifted_mask (result.write_val (), start, width,
  2810. negate_p,
  2811. wi::int_traits <T>::precision));
  2812. return result;
  2813. }
  2814. /* Return an integer of type T in which bit BIT is set and all the
  2815. others are clear. */
  2816. template <typename T>
  2817. inline T
  2818. wi::set_bit_in_zero (unsigned int bit)
  2819. {
  2820. return shifted_mask <T> (bit, 1, false);
  2821. }
  2822. #endif /* WIDE_INT_H */