wide-int.h 108 KB

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