Int128_t.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EAStdC/internal/Config.h>
  5. #include <EAStdC/Int128_t.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <math.h>
  10. #include <EAAssert/eaassert.h>
  11. #if defined(_MSC_VER)
  12. #pragma warning(push)
  13. #pragma warning(disable: 4723) // potential divide by 0
  14. #pragma warning(disable: 4365) // 'argument' : conversion from 'int' to 'uint32_t', signed/unsigned mismatch
  15. #pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
  16. #endif
  17. namespace EA
  18. {
  19. namespace StdC
  20. {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Constants
  23. // EASTDC_INT128_MIN is equal to: -170141183460469231731687303715884105728;
  24. const int128_t EASTDC_INT128_MIN(0x00000000, 0x00000000, 0x00000000, 0x80000000);
  25. // EASTDC_INT128_MAX is equal to: 170141183460469231731687303715884105727;
  26. const int128_t EASTDC_INT128_MAX(0xffffffff, 0xffffffff, 0xffffffff, 0x7fffffff);
  27. // EASTDC_UINT128_MIN is equal to: 0;
  28. const uint128_t EASTDC_UINT128_MIN(0x00000000, 0x00000000, 0x00000000, 0x00000000);
  29. // EASTDC_UINT128_MAX is equal to: 340282366920938463463374607431768211455;
  30. const uint128_t EASTDC_UINT128_MAX(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // int128_t
  33. ///////////////////////////////////////////////////////////////////////////////
  34. int128_t_base::int128_t_base()
  35. {
  36. #if EA_INT128_USE_INT64
  37. mPart1 = 0;
  38. mPart0 = 0;
  39. #else
  40. mPart3 = 0;
  41. mPart2 = 0;
  42. mPart1 = 0;
  43. mPart0 = 0;
  44. #endif
  45. }
  46. int128_t_base::int128_t_base(uint32_t nPart0, uint32_t nPart1, uint32_t nPart2, uint32_t nPart3)
  47. {
  48. #if EA_INT128_USE_INT64
  49. mPart1 = ((uint64_t)nPart3 << 32) + nPart2;
  50. mPart0 = ((uint64_t)nPart1 << 32) + nPart0;
  51. #else
  52. mPart3 = nPart3;
  53. mPart2 = nPart2;
  54. mPart1 = nPart1;
  55. mPart0 = nPart0;
  56. #endif
  57. }
  58. int128_t_base::int128_t_base(uint64_t nPart0, uint64_t nPart1)
  59. {
  60. #if EA_INT128_USE_INT64
  61. mPart1 = nPart1;
  62. mPart0 = nPart0;
  63. #else
  64. mPart3 = (uint32_t)(nPart1 >> 32);
  65. mPart2 = (uint32_t) nPart1;
  66. mPart1 = (uint32_t)(nPart0 >> 32);
  67. mPart0 = (uint32_t) nPart0;
  68. #endif
  69. }
  70. int128_t_base::int128_t_base(uint8_t value)
  71. {
  72. #if EA_INT128_USE_INT64
  73. mPart1 = 0;
  74. mPart0 = value;
  75. #else
  76. mPart3 = 0;
  77. mPart2 = 0;
  78. mPart1 = 0;
  79. mPart0 = value;
  80. #endif
  81. }
  82. int128_t_base::int128_t_base(uint16_t value)
  83. {
  84. #if EA_INT128_USE_INT64
  85. mPart1 = 0;
  86. mPart0 = value;
  87. #else
  88. mPart3 = 0;
  89. mPart2 = 0;
  90. mPart1 = 0;
  91. mPart0 = value;
  92. #endif
  93. }
  94. int128_t_base::int128_t_base(uint32_t value)
  95. {
  96. #if EA_INT128_USE_INT64
  97. mPart1 = 0;
  98. mPart0 = value;
  99. #else
  100. mPart3 = 0;
  101. mPart2 = 0;
  102. mPart1 = 0;
  103. mPart0 = value;
  104. #endif
  105. }
  106. #if defined(INT128_UINT_TYPE)
  107. int128_t_base::int128_t_base(INT128_UINT_TYPE value)
  108. {
  109. #if EA_INT128_USE_INT64
  110. mPart1 = 0;
  111. mPart0 = value;
  112. #else
  113. mPart3 = 0;
  114. mPart2 = 0;
  115. mPart1 = 0;
  116. mPart0 = value;
  117. #endif
  118. }
  119. #endif
  120. int128_t_base::int128_t_base(uint64_t value)
  121. {
  122. #if EA_INT128_USE_INT64
  123. mPart1 = 0;
  124. mPart0 = value;
  125. #else
  126. mPart3 = 0;
  127. mPart2 = 0;
  128. mPart1 = (uint32_t) ((value >> 32) & 0xffffffff);
  129. mPart0 = (uint32_t) ((value >> 0) & 0xffffffff);
  130. #endif
  131. }
  132. int128_t_base::int128_t_base(const int128_t_base& value)
  133. {
  134. #if EA_INT128_USE_INT64
  135. mPart1 = value.mPart1;
  136. mPart0 = value.mPart0;
  137. #else
  138. mPart3 = value.mPart3;
  139. mPart2 = value.mPart2;
  140. mPart1 = value.mPart1;
  141. mPart0 = value.mPart0;
  142. #endif
  143. }
  144. int128_t_base& int128_t_base::operator=(const int128_t_base& value)
  145. {
  146. #if EA_INT128_USE_INT64
  147. mPart1 = value.mPart1;
  148. mPart0 = value.mPart0;
  149. #else
  150. mPart3 = value.mPart3;
  151. mPart2 = value.mPart2;
  152. mPart1 = value.mPart1;
  153. mPart0 = value.mPart0;
  154. #endif
  155. return *this;
  156. }
  157. ///////////////////////////////////////////////////////////////////////////////
  158. // operatorPlus
  159. //
  160. // Returns: (value1 + value2) into result.
  161. // The output 'result' *is* allowed to point to the same memory as one of the inputs.
  162. // To consider: Fix 'defect' of this function whereby it doesn't implement overflow wraparound.
  163. //
  164. void int128_t_base::operatorPlus(const int128_t_base& value1, const int128_t_base& value2, int128_t_base& result)
  165. {
  166. #if defined(EA_ASM_STYLE_INTEL) && defined(EA_PROCESSOR_X86)
  167. __asm
  168. {
  169. mov ebx, value1
  170. mov ecx, value2
  171. mov edx, result
  172. mov eax, [ebx]
  173. add eax, [ecx] ;(nCarry, tmp) = value1.mPart0 + value2.mPart0
  174. mov [edx], eax ;result.mPart0 = value1.mPart0 + value2.mPart0
  175. mov eax, [ebx+4]
  176. adc eax, [ecx+4] ;(nCarry, tmp) = value1.mPart1 + value2.mPart1
  177. mov [edx+4], eax ;result.mPart1 = value1.mPart1 + value2.mPart1 + nCarry
  178. mov eax, [ebx+8]
  179. adc eax, [ecx+8] ;(nCarry, tmp) = value1.mPart2 + value2.mPart2
  180. mov [edx+8], eax ;result.mPart2 = value1.mPart2 + value2.mPart2 + nCarry
  181. mov eax, [ebx+12]
  182. adc eax, [ecx+12] ;(nCarry, tmp) = value1.mPart3 + value2.mPart3
  183. mov [edx+12], eax ;result.mPart3 = value1.mPart3 + value2.mPart3 + nCarry
  184. }
  185. #elif EA_INT128_USE_INT64
  186. uint64_t t = value1.mPart0 + value2.mPart0;
  187. uint64_t nCarry = (t < value1.mPart0) && (t < value2.mPart0);
  188. result.mPart0 = t;
  189. result.mPart1 = value1.mPart1 + value2.mPart1 + nCarry;
  190. #else
  191. uint64_t t = ((uint64_t)value1.mPart0) + ((uint64_t)value2.mPart0);
  192. uint32_t nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  193. result.mPart0 = (uint32_t) t;
  194. t = ((uint64_t)value1.mPart1) + ((uint64_t)value2.mPart1) + nCarry;
  195. nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  196. result.mPart1 = (uint32_t) t;
  197. t = ((uint64_t)value1.mPart2) + ((uint64_t)value2.mPart2) + nCarry;
  198. nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  199. result.mPart2 = (uint32_t) t;
  200. t = ((uint64_t)value1.mPart3) + ((uint64_t)value2.mPart3) + nCarry;
  201. //nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  202. result.mPart3 = (uint32_t) t;
  203. #endif
  204. }
  205. ///////////////////////////////////////////////////////////////////////////////
  206. // operatorMinus
  207. //
  208. // Returns: (value1 - value2) into result.
  209. // The output 'result' *is* allowed to point to the same memory as one of the inputs.
  210. // To consider: Fix 'defect' of this function whereby it doesn't implement overflow wraparound.
  211. //
  212. void int128_t_base::operatorMinus(const int128_t_base& value1, const int128_t_base& value2, int128_t_base& result)
  213. {
  214. #if EA_INT128_USE_INT64
  215. uint64_t t = (value1.mPart0 - value2.mPart0);
  216. uint64_t nCarry = (value1.mPart0 < value2.mPart0) ? 1 : 0;
  217. result.mPart0 = t;
  218. result.mPart1 = (value1.mPart1 - value2.mPart1) - nCarry;
  219. #else
  220. uint64_t t = ((uint64_t)value1.mPart0) - ((uint64_t)value2.mPart0);
  221. uint32_t nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  222. result.mPart0 = (uint32_t) t;
  223. t = (((uint64_t)value1.mPart1) - ((uint64_t)value2.mPart1)) - nCarry;
  224. nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  225. result.mPart1 = (uint32_t) t;
  226. t = (((uint64_t)value1.mPart2) - ((uint64_t)value2.mPart2)) - nCarry;
  227. nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  228. result.mPart2 = (uint32_t) t;
  229. t = (((uint64_t)value1.mPart3) - ((uint64_t)value2.mPart3)) - nCarry;
  230. //nCarry = (uint32_t)((t > 0xffffffff) ? 1 : 0);
  231. result.mPart3 = (uint32_t) t;
  232. #endif
  233. }
  234. ///////////////////////////////////////////////////////////////////////////////
  235. // operatorMul
  236. //
  237. // 32 bit systems:
  238. // The way this works is like decimal multiplication by hand with a pencil and
  239. // paper. The difference is that we work with blocks of 32 bits intead of blocks
  240. // of ten. Here is a multiplication of 0x00000008000000040000000200000001 x
  241. // the same value done like you do with pencil and paper:
  242. //
  243. // Part 3 2 1 0
  244. // 00000008 00000004 00000002 00000001
  245. // x 00000008 00000004 00000002 00000001
  246. // -------------------------------------------
  247. // | 00000008 00000004 00000002 00000001
  248. // 00000010 | 00000008 00000004 00000002 (00000000)
  249. // 00000020 00000010 | 00000008 00000004 (00000000)(00000000)
  250. // + 00000040 00000020 00000010 | 00000008 (00000000)(00000000)(00000000)
  251. // -------------------------------------------------------------------------
  252. //
  253. // That the numbers above have columns each with the same values is a coincidence
  254. // of the choice of the two multiplying numbers and in reality numbers would
  255. // likely be much more complicated. But the above is easy to show. Note that
  256. // the numbers to the left of the column with 00000008 are outside the range
  257. // of 128 bits. As a result, in our implementation below, we skip the steps that
  258. // create these values, as they would just get lost anyway.
  259. //
  260. // 64 bit systems:
  261. // This is how it would be able to work if we could get a 128 bit result from
  262. // two 64 bit values. None of the 64 bit systems that we are currently working
  263. // with have C language support for multiplying two 64 bit numbers and retrieving
  264. // the 128 bit result. However, many 64 bit platforms have support at the asm
  265. // level for doing such a thing.
  266. // Part 1 Part 0
  267. // 0000000000000002 0000000000000001
  268. // x 0000000000000002 0000000000000001
  269. // -------------------------------------------
  270. // | 0000000000000002 0000000000000001
  271. // + 0000000000000004 | 0000000000000002 (0000000000000000)
  272. // -------------------------------------------------------------------------
  273. //
  274. void int128_t_base::operatorMul(const int128_t_base& a, const int128_t_base& b, int128_t_base& result)
  275. {
  276. // To consider: Use compiler or OS-provided custom functionality here, such as
  277. // Windows UnsignedMultiply128 and GCC's built-in int128_t.
  278. #if EA_INT128_USE_INT64
  279. #if defined(DISABLED_PLATFORM_WIN64)
  280. // To do: Implement x86-64 asm here.
  281. #else
  282. // Else we are stuck doing something less efficient. In this case we
  283. // fall back to doing 32 bit multiplies as with 32 bit platforms.
  284. result = (a.mPart0 & 0xffffffff) * (b.mPart0 & 0xffffffff);
  285. int128_t v01 = (a.mPart0 & 0xffffffff) * ((b.mPart0 >> 32) & 0xffffffff);
  286. int128_t v02 = (a.mPart0 & 0xffffffff) * (b.mPart1 & 0xffffffff);
  287. int128_t v03 = (a.mPart0 & 0xffffffff) * ((b.mPart1 >> 32) & 0xffffffff);
  288. int128_t v10 = ((a.mPart0 >> 32) & 0xffffffff) * (b.mPart0 & 0xffffffff);
  289. int128_t v11 = ((a.mPart0 >> 32) & 0xffffffff) * ((b.mPart0 >> 32) & 0xffffffff);
  290. int128_t v12 = ((a.mPart0 >> 32) & 0xffffffff) * (b.mPart1 & 0xffffffff);
  291. int128_t v20 = (a.mPart1 & 0xffffffff) * (b.mPart0 & 0xffffffff);
  292. int128_t v21 = (a.mPart1 & 0xffffffff) * ((b.mPart0 >> 32) & 0xffffffff);
  293. int128_t v30 = ((a.mPart1 >> 32) & 0xffffffff) * (b.mPart0 & 0xffffffff);
  294. // Do row addition, shifting as needed.
  295. operatorPlus(result, v01 << 32, result);
  296. operatorPlus(result, v02 << 64, result);
  297. operatorPlus(result, v03 << 96, result);
  298. operatorPlus(result, v10 << 32, result);
  299. operatorPlus(result, v11 << 64, result);
  300. operatorPlus(result, v12 << 96, result);
  301. operatorPlus(result, v20 << 64, result);
  302. operatorPlus(result, v21 << 96, result);
  303. operatorPlus(result, v30 << 96, result);
  304. #endif
  305. #else
  306. // Do part-by-part multiplication, skipping overflowing combinations.
  307. result = ((uint64_t)a.mPart0) * ((uint64_t)b.mPart0);
  308. uint128_t v01 = ((uint64_t)a.mPart0) * ((uint64_t)b.mPart1);
  309. uint128_t v02 = ((uint64_t)a.mPart0) * ((uint64_t)b.mPart2);
  310. uint128_t v03 = ((uint64_t)a.mPart0) * ((uint64_t)b.mPart3);
  311. uint128_t v10 = ((uint64_t)a.mPart1) * ((uint64_t)b.mPart0);
  312. uint128_t v11 = ((uint64_t)a.mPart1) * ((uint64_t)b.mPart1);
  313. uint128_t v12 = ((uint64_t)a.mPart1) * ((uint64_t)b.mPart2);
  314. uint128_t v20 = ((uint64_t)a.mPart2) * ((uint64_t)b.mPart0);
  315. uint128_t v21 = ((uint64_t)a.mPart2) * ((uint64_t)b.mPart1);
  316. uint128_t v30 = ((uint64_t)a.mPart3) * ((uint64_t)b.mPart0);
  317. // Do row addition, shifting as needed.
  318. operatorPlus(result, v01 << 32, result);
  319. operatorPlus(result, v02 << 64, result);
  320. operatorPlus(result, v03 << 96, result);
  321. operatorPlus(result, v10 << 32, result);
  322. operatorPlus(result, v11 << 64, result);
  323. operatorPlus(result, v12 << 96, result);
  324. operatorPlus(result, v20 << 64, result);
  325. operatorPlus(result, v21 << 96, result);
  326. operatorPlus(result, v30 << 96, result);
  327. #endif
  328. }
  329. ///////////////////////////////////////////////////////////////////////////////
  330. // operatorShiftRight
  331. //
  332. // Returns: value >> nShift into result
  333. // The output 'result' may *not* be the same as one the input.
  334. // With rightward shifts of negative numbers, shift in zero from the left side.
  335. //
  336. void int128_t_base::operatorShiftRight(const int128_t_base& value, int nShift, int128_t_base& result)
  337. {
  338. #if EA_INT128_USE_INT64
  339. if(nShift >= 0)
  340. {
  341. if(nShift < 64)
  342. { // 0 - 63
  343. result.mPart1 = (value.mPart1 >> nShift);
  344. if(nShift == 0)
  345. result.mPart0 = (value.mPart0 >> nShift);
  346. else
  347. result.mPart0 = (value.mPart0 >> nShift) | (value.mPart1 << (64 - nShift));
  348. }
  349. else
  350. { // 64+
  351. result.mPart1 = 0;
  352. result.mPart0 = (value.mPart1 >> (nShift - 64));
  353. }
  354. }
  355. else // (nShift < 0)
  356. operatorShiftLeft(value, -nShift, result);
  357. #else
  358. if(nShift >= 0)
  359. {
  360. if(nShift <= 32)
  361. {
  362. if(nShift == 32)
  363. { // We can't use the code further below for 0-31 because 32 bit
  364. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  365. result.mPart0 = value.mPart1;
  366. result.mPart1 = value.mPart2;
  367. result.mPart2 = value.mPart3;
  368. result.mPart3 = 0;
  369. }
  370. else
  371. { // 0 - 31
  372. result.mPart3 = (value.mPart3 >> nShift);
  373. result.mPart2 = (value.mPart2 >> nShift) | (value.mPart3 << (32 - nShift));
  374. result.mPart1 = (value.mPart1 >> nShift) | (value.mPart2 << (32 - nShift));
  375. result.mPart0 = (value.mPart0 >> nShift) | (value.mPart1 << (32 - nShift));
  376. }
  377. }
  378. else if(nShift <= 64)
  379. {
  380. if(nShift == 64)
  381. { // We can't use the code further below for 0-31 because 32 bit
  382. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  383. result.mPart0 = value.mPart2;
  384. result.mPart1 = value.mPart3;
  385. result.mPart2 = 0;
  386. result.mPart3 = 0;
  387. }
  388. else
  389. { // 33 - 63
  390. result.mPart3 = 0;
  391. result.mPart2 = (value.mPart3 >> (nShift - 32));
  392. result.mPart1 = (value.mPart2 >> (nShift - 32)) | (value.mPart3 << (64 - nShift));
  393. result.mPart0 = (value.mPart1 >> (nShift - 32)) | (value.mPart2 << (64 - nShift));
  394. }
  395. }
  396. else if(nShift <= 96)
  397. {
  398. if(nShift == 96)
  399. { // We can't use the code further below for 0-31 because 32 bit
  400. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  401. result.mPart0 = value.mPart3;
  402. result.mPart1 = 0;
  403. result.mPart2 = 0;
  404. result.mPart3 = 0;
  405. }
  406. else
  407. { // 65 - 95
  408. result.mPart3 = 0;
  409. result.mPart2 = 0;
  410. result.mPart1 = (value.mPart3 >> (nShift - 64));
  411. result.mPart0 = (value.mPart2 >> (nShift - 64)) | (value.mPart3 << (96 - nShift));
  412. }
  413. }
  414. else if(nShift < 128)
  415. { // 96 - 127
  416. result.mPart3 = 0;
  417. result.mPart2 = 0;
  418. result.mPart1 = 0;
  419. result.mPart0 = (value.mPart3 >> (nShift - 96));
  420. }
  421. else
  422. { // 128+
  423. result.mPart3 = 0;
  424. result.mPart2 = 0;
  425. result.mPart1 = 0;
  426. result.mPart0 = 0;
  427. }
  428. }
  429. else // (nShift < 0)
  430. operatorShiftLeft(value, -nShift, result);
  431. #endif
  432. }
  433. ///////////////////////////////////////////////////////////////////////////////
  434. // operatorShiftRight
  435. //
  436. // Returns: value << nShift into result
  437. // The output 'result' may *not* be the same as one the input.
  438. // With rightward shifts of negative numbers, shift in zero from the left side.
  439. //
  440. void int128_t_base::operatorShiftLeft(const int128_t_base& value, int nShift, int128_t_base& result)
  441. {
  442. #if EA_INT128_USE_INT64
  443. if(nShift >= 0)
  444. {
  445. if(nShift < 64)
  446. {
  447. if(nShift) // We need to have a special case because CPUs convert a shift by 64 to a no-op.
  448. {
  449. // 1 - 63
  450. result.mPart0 = (value.mPart0 << nShift);
  451. result.mPart1 = (value.mPart1 << nShift) | (value.mPart0 >> (64 - nShift));
  452. }
  453. else
  454. {
  455. result.mPart0 = value.mPart0;
  456. result.mPart1 = value.mPart1;
  457. }
  458. }
  459. else
  460. { // 64+
  461. result.mPart0 = 0;
  462. result.mPart1 = (value.mPart0 << (nShift - 64));
  463. }
  464. }
  465. else // (nShift < 0)
  466. operatorShiftRight(value, -nShift, result);
  467. #else
  468. if(nShift >= 0)
  469. {
  470. if(nShift <= 32)
  471. {
  472. if(nShift == 32)
  473. { // We can't use the code further below for 32 because 32 bit
  474. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  475. result.mPart0 = 0;
  476. result.mPart1 = value.mPart0;
  477. result.mPart2 = value.mPart1;
  478. result.mPart3 = value.mPart2;
  479. }
  480. else if(nShift)
  481. { // 1 - 31
  482. result.mPart0 = (value.mPart0 << nShift);
  483. result.mPart1 = (value.mPart1 << nShift) | (value.mPart0 >> (32 - nShift));
  484. result.mPart2 = (value.mPart2 << nShift) | (value.mPart1 >> (32 - nShift));
  485. result.mPart3 = (value.mPart3 << nShift) | (value.mPart2 >> (32 - nShift));
  486. }
  487. else
  488. {
  489. result.mPart0 = value.mPart0;
  490. result.mPart1 = value.mPart1;
  491. result.mPart2 = value.mPart2;
  492. result.mPart3 = value.mPart3;
  493. }
  494. }
  495. else if(nShift <= 64)
  496. {
  497. if(nShift == 64)
  498. { // We can't use the code further below for 0-31 because 32 bit
  499. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  500. result.mPart0 = 0;
  501. result.mPart1 = 0;
  502. result.mPart2 = value.mPart0;
  503. result.mPart3 = value.mPart1;
  504. }
  505. else
  506. { // 33 - 63
  507. result.mPart0 = 0;
  508. result.mPart1 = (value.mPart0 << (nShift - 32));
  509. result.mPart2 = (value.mPart1 << (nShift - 32)) | (value.mPart0 >> (64 - nShift));
  510. result.mPart3 = (value.mPart2 << (nShift - 32)) | (value.mPart1 >> (64 - nShift));
  511. }
  512. }
  513. else if(nShift <= 96)
  514. {
  515. if(nShift == 96)
  516. { // We can't use the code further below for 0-31 because 32 bit
  517. // processors (e.g. Intel) often implement a shift of 32 as a no-op.
  518. result.mPart0 = 0;
  519. result.mPart1 = 0;
  520. result.mPart2 = 0;
  521. result.mPart3 = value.mPart0;
  522. }
  523. else
  524. { // 65 - 95
  525. result.mPart0 = 0;
  526. result.mPart1 = 0;
  527. result.mPart2 = (value.mPart0 << (nShift - 64));
  528. result.mPart3 = (value.mPart1 << (nShift - 64)) | (value.mPart0 >> (96 - nShift));
  529. }
  530. }
  531. else if(nShift < 128)
  532. { // 96 - 127
  533. result.mPart0 = 0;
  534. result.mPart1 = 0;
  535. result.mPart2 = 0;
  536. result.mPart3 = (value.mPart0 << (nShift - 96));
  537. }
  538. else
  539. { // 128+
  540. result.mPart3 = 0;
  541. result.mPart2 = 0;
  542. result.mPart1 = 0;
  543. result.mPart0 = 0;
  544. }
  545. }
  546. else // (nShift < 0)
  547. operatorShiftRight(value, -nShift, result);
  548. #endif
  549. }
  550. bool int128_t_base::operator!() const
  551. {
  552. #if EA_INT128_USE_INT64
  553. return (mPart0 == 0) && (mPart1 == 0);
  554. #else
  555. return (mPart0 == 0) && (mPart1 == 0) && (mPart2 == 0) && (mPart3 == 0);
  556. #endif
  557. }
  558. ///////////////////////////////////////////////////////////////////////////////
  559. // operatorXOR
  560. //
  561. // Returns: value1 ^ value2 into result
  562. // The output 'result' may be the same as one the input.
  563. //
  564. void int128_t_base::operatorXOR(const int128_t_base& value1, const int128_t_base& value2, int128_t_base& result)
  565. {
  566. #if EA_INT128_USE_INT64
  567. result.mPart0 = (value1.mPart0 ^ value2.mPart0);
  568. result.mPart1 = (value1.mPart1 ^ value2.mPart1);
  569. #else
  570. result.mPart0 = (value1.mPart0 ^ value2.mPart0);
  571. result.mPart1 = (value1.mPart1 ^ value2.mPart1);
  572. result.mPart2 = (value1.mPart2 ^ value2.mPart2);
  573. result.mPart3 = (value1.mPart3 ^ value2.mPart3);
  574. #endif
  575. }
  576. ///////////////////////////////////////////////////////////////////////////////
  577. // operatorOR
  578. //
  579. // Returns: value1 | value2 into result
  580. // The output 'result' may be the same as one the input.
  581. //
  582. void int128_t_base::operatorOR(const int128_t_base& value1, const int128_t_base& value2, int128_t_base& result)
  583. {
  584. #if EA_INT128_USE_INT64
  585. result.mPart0 = (value1.mPart0 | value2.mPart0);
  586. result.mPart1 = (value1.mPart1 | value2.mPart1);
  587. #else
  588. result.mPart0 = (value1.mPart0 | value2.mPart0);
  589. result.mPart1 = (value1.mPart1 | value2.mPart1);
  590. result.mPart2 = (value1.mPart2 | value2.mPart2);
  591. result.mPart3 = (value1.mPart3 | value2.mPart3);
  592. #endif
  593. }
  594. ///////////////////////////////////////////////////////////////////////////////
  595. // operatorAND
  596. //
  597. // Returns: value1 & value2 into result
  598. // The output 'result' may be the same as one the input.
  599. //
  600. void int128_t_base::operatorAND(const int128_t_base& value1, const int128_t_base& value2, int128_t_base& result)
  601. {
  602. #if EA_INT128_USE_INT64
  603. result.mPart0 = (value1.mPart0 & value2.mPart0);
  604. result.mPart1 = (value1.mPart1 & value2.mPart1);
  605. #else
  606. result.mPart0 = (value1.mPart0 & value2.mPart0);
  607. result.mPart1 = (value1.mPart1 & value2.mPart1);
  608. result.mPart2 = (value1.mPart2 & value2.mPart2);
  609. result.mPart3 = (value1.mPart3 & value2.mPart3);
  610. #endif
  611. }
  612. bool int128_t_base::AsBool() const
  613. {
  614. #if EA_INT128_USE_INT64
  615. return (mPart0 || mPart1);
  616. #else
  617. return (mPart0 || mPart1 || mPart2 || mPart3);
  618. #endif
  619. }
  620. uint8_t int128_t_base::AsUint8() const
  621. {
  622. // OK for EA_INT128_USE_INT64
  623. return (uint8_t) mPart0;
  624. }
  625. uint16_t int128_t_base::AsUint16() const
  626. {
  627. // OK for EA_INT128_USE_INT64
  628. return (uint16_t) mPart0;
  629. }
  630. uint32_t int128_t_base::AsUint32() const
  631. {
  632. // OK for EA_INT128_USE_INT64
  633. return (uint32_t) mPart0;
  634. }
  635. uint64_t int128_t_base::AsUint64() const
  636. {
  637. #if EA_INT128_USE_INT64
  638. return mPart0;
  639. #else
  640. return (((uint64_t) mPart1) << 32) + mPart0;
  641. #endif
  642. }
  643. int int128_t_base::GetBit(int nIndex) const
  644. {
  645. // EA_ASSERT((nIndex >= 0) && (nIndex < 128));
  646. #if EA_INT128_USE_INT64
  647. const uint64_t nBitMask = ((uint64_t)1 << (nIndex % 64));
  648. if(nIndex < 64)
  649. return ((mPart0 & nBitMask) ? 1 : 0);
  650. else if(nIndex < 128)
  651. return ((mPart1 & nBitMask) ? 1 : 0);
  652. return 0;
  653. #else
  654. const uint32_t nBitMask = ((uint32_t)1 << (nIndex % 32));
  655. if(nIndex < 32)
  656. return ((mPart0 & nBitMask) ? 1 : 0);
  657. else if(nIndex < 64)
  658. return ((mPart1 & nBitMask) ? 1 : 0);
  659. else if(nIndex < 96)
  660. return ((mPart2 & nBitMask) ? 1 : 0);
  661. else if(nIndex < 128)
  662. return ((mPart3 & nBitMask) ? 1 : 0);
  663. return 0;
  664. #endif
  665. }
  666. void int128_t_base::SetBit(int nIndex, int value)
  667. {
  668. // EA_ASSERT((nIndex >= 0) && (nIndex < 128));
  669. #if EA_INT128_USE_INT64
  670. const uint64_t nBitMask = ((uint64_t)1 << (nIndex % 64));
  671. if(nIndex < 64)
  672. {
  673. if(value)
  674. mPart0 = mPart0 | nBitMask;
  675. else
  676. mPart0 = mPart0 & ~nBitMask;
  677. }
  678. else if(nIndex < 128)
  679. {
  680. if(value)
  681. mPart1 = mPart1 | nBitMask;
  682. else
  683. mPart1 = mPart1 & ~nBitMask;
  684. }
  685. #else
  686. const uint32_t nBitMask = ((uint32_t)1 << (nIndex % 32));
  687. if(nIndex < 32)
  688. {
  689. if(value)
  690. mPart0 = mPart0 | nBitMask;
  691. else
  692. mPart0 = mPart0 & ~nBitMask;
  693. }
  694. else if(nIndex < 64)
  695. {
  696. if(value)
  697. mPart1 = mPart1 | nBitMask;
  698. else
  699. mPart1 = mPart1 & ~nBitMask;
  700. }
  701. else if(nIndex < 96)
  702. {
  703. if(value)
  704. mPart2 = mPart2 | nBitMask;
  705. else
  706. mPart2 = mPart2 & ~nBitMask;
  707. }
  708. else if(nIndex < 128)
  709. {
  710. if(value)
  711. mPart3 = mPart3 | nBitMask;
  712. else
  713. mPart3 = mPart3 & ~nBitMask;
  714. }
  715. #endif
  716. }
  717. // part is in the range of [0,15]
  718. uint8_t int128_t_base::GetPartUint8(int nIndex) const
  719. {
  720. #if EA_INT128_USE_INT64
  721. uint64_t value(0);
  722. switch (nIndex / 8)
  723. {
  724. case 0:
  725. value = mPart0;
  726. break;
  727. case 1:
  728. value = mPart1;
  729. break;
  730. }
  731. nIndex = ((nIndex % 8) * 8);
  732. return (uint8_t)((value & ((uint64_t)0xff << nIndex)) >> nIndex);
  733. #else
  734. uint32_t value(0);
  735. switch (nIndex / 4)
  736. {
  737. case 0:
  738. value = mPart0;
  739. break;
  740. case 1:
  741. value = mPart1;
  742. break;
  743. case 2:
  744. value = mPart2;
  745. break;
  746. case 3:
  747. value = mPart3;
  748. break;
  749. }
  750. nIndex = ((nIndex % 4) * 8);
  751. return (uint8_t)(((value & ((uint32_t)0xff << nIndex))) >> nIndex);
  752. #endif
  753. }
  754. // part is in the range of [0,7]
  755. uint16_t int128_t_base::GetPartUint16(int nIndex) const
  756. {
  757. #if EA_INT128_USE_INT64
  758. uint64_t value(0);
  759. switch (nIndex / 4)
  760. {
  761. case 0:
  762. value = mPart0;
  763. break;
  764. case 1:
  765. value = mPart1;
  766. break;
  767. }
  768. nIndex = ((nIndex % 4) * 16);
  769. return (uint16_t)(((value & ((uint64_t)0xffff << nIndex))) >> nIndex);
  770. #else
  771. uint32_t value(0);
  772. switch (nIndex / 2)
  773. {
  774. case 0:
  775. value = mPart0;
  776. break;
  777. case 1:
  778. value = mPart1;
  779. break;
  780. case 2:
  781. value = mPart2;
  782. break;
  783. case 3:
  784. value = mPart3;
  785. break;
  786. }
  787. if(nIndex % 2)
  788. return (uint16_t)(value >> 16);
  789. else
  790. return (uint16_t)(value);
  791. #endif
  792. }
  793. // part is in the range of [0,3]
  794. uint32_t int128_t_base::GetPartUint32(int nIndex) const
  795. {
  796. #if EA_INT128_USE_INT64
  797. switch (nIndex)
  798. {
  799. case 0:
  800. return (uint32_t) mPart0;
  801. case 1:
  802. return (uint32_t)(mPart0 >> 32);
  803. case 2:
  804. return (uint32_t) mPart1;
  805. case 3:
  806. return (uint32_t)(mPart1 >> 32);
  807. }
  808. return 0;
  809. #else
  810. switch (nIndex)
  811. {
  812. case 0:
  813. return mPart0;
  814. case 1:
  815. return mPart1;
  816. case 2:
  817. return mPart2;
  818. case 3:
  819. return mPart3;
  820. }
  821. return 0;
  822. #endif
  823. }
  824. // part is in the range of [0,1]
  825. uint64_t int128_t_base::GetPartUint64(int nIndex) const
  826. {
  827. #if EA_INT128_USE_INT64
  828. if(nIndex == 0)
  829. return mPart0;
  830. else if(nIndex == 1)
  831. return mPart1;
  832. return 0;
  833. #else
  834. if(nIndex == 0)
  835. return uint64_t((uint64_t(mPart1) << 32) + mPart0);
  836. else if(nIndex == 1)
  837. return uint64_t((uint64_t(mPart3) << 32) + mPart2);
  838. return 0;
  839. #endif
  840. }
  841. void int128_t_base::SetPartUint8(int nIndex, uint8_t value)
  842. {
  843. #if EA_INT128_USE_INT64
  844. uint64_t* pValue;
  845. switch (nIndex / 8)
  846. {
  847. case 0:
  848. pValue = &mPart0;
  849. break;
  850. case 1:
  851. pValue = &mPart1;
  852. break;
  853. default:
  854. return;
  855. }
  856. nIndex %= 8;
  857. *pValue = ((*pValue & ~(UINT64_C(0xff) << (nIndex * 8))) + ((uint64_t)value << (nIndex * 8)));
  858. #else
  859. uint32_t* pValue;
  860. switch (nIndex / 4)
  861. {
  862. case 0:
  863. pValue = &mPart0;
  864. break;
  865. case 1:
  866. pValue = &mPart1;
  867. break;
  868. case 2:
  869. pValue = &mPart2;
  870. break;
  871. case 3:
  872. pValue = &mPart3;
  873. break;
  874. default:
  875. return;
  876. }
  877. switch (nIndex % 4)
  878. {
  879. case 0:
  880. *pValue = ((*pValue & 0xffffff00) + (value << 0));
  881. break;
  882. case 1:
  883. *pValue = ((*pValue & 0xffff00ff) + (value << 8));
  884. break;
  885. case 2:
  886. *pValue = ((*pValue & 0xff00ffff) + (value << 16));
  887. break;
  888. case 3:
  889. *pValue = ((*pValue & 0x00ffffff) + (value << 24));
  890. break;
  891. }
  892. #endif
  893. }
  894. void int128_t_base::SetPartUint16(int nIndex, uint16_t value)
  895. {
  896. #if EA_INT128_USE_INT64
  897. uint64_t* pValue;
  898. switch (nIndex / 4)
  899. {
  900. case 0:
  901. pValue = &mPart0;
  902. break;
  903. case 1:
  904. pValue = &mPart1;
  905. break;
  906. default:
  907. return;
  908. }
  909. nIndex %= 4;
  910. *pValue = ((*pValue & ~(UINT64_C(0xffff) << (nIndex * 16))) + ((uint64_t)value << (nIndex * 16)));
  911. #else
  912. uint32_t* pValue;
  913. switch (nIndex / 2)
  914. {
  915. case 0:
  916. pValue = &mPart0;
  917. break;
  918. case 1:
  919. pValue = &mPart1;
  920. break;
  921. case 2:
  922. pValue = &mPart2;
  923. break;
  924. case 3:
  925. pValue = &mPart3;
  926. break;
  927. default:
  928. return;
  929. }
  930. if(nIndex % 2)
  931. *pValue = ((*pValue & 0x0000ffff) + (value << 16));
  932. else
  933. *pValue = ((*pValue & 0xffff0000) + (value));
  934. #endif
  935. }
  936. void int128_t_base::SetPartUint32(int nIndex, uint32_t value)
  937. {
  938. #if EA_INT128_USE_INT64
  939. switch (nIndex)
  940. {
  941. case 0:
  942. mPart0 = (mPart0 & UINT64_C(0xffffffff00000000)) + value;
  943. break;
  944. case 1:
  945. mPart0 = (mPart0 & UINT64_C(0x00000000ffffffff)) + ((uint64_t)value << 32);
  946. break;
  947. case 2:
  948. mPart1 = (mPart1 & UINT64_C(0xffffffff00000000)) + value;
  949. break;
  950. case 3:
  951. mPart1 = (mPart1 & UINT64_C(0x00000000ffffffff)) + ((uint64_t)value << 32);
  952. break;
  953. }
  954. #else
  955. switch (nIndex)
  956. {
  957. case 0:
  958. mPart0 = value;
  959. break;
  960. case 1:
  961. mPart1 = value;
  962. break;
  963. case 2:
  964. mPart2 = value;
  965. break;
  966. case 3:
  967. mPart3 = value;
  968. break;
  969. }
  970. #endif
  971. }
  972. void int128_t_base::SetPartUint64(int nIndex, uint64_t value)
  973. {
  974. #if EA_INT128_USE_INT64
  975. if(nIndex == 0)
  976. mPart0 = value;
  977. else if(nIndex == 1)
  978. mPart1 = value;
  979. #else
  980. if(nIndex == 0)
  981. {
  982. mPart0 = (uint32_t)(value);
  983. mPart1 = (uint32_t)(value >> 32);
  984. }
  985. else if(nIndex == 1)
  986. {
  987. mPart2 = (uint32_t)(value);
  988. mPart3 = (uint32_t)(value >> 32);
  989. }
  990. #endif
  991. }
  992. bool int128_t_base::IsZero() const
  993. {
  994. #if EA_INT128_USE_INT64
  995. return (mPart0 == 0) && // Check mPart0 first as this will likely yield faster execution.
  996. (mPart1 == 0);
  997. #else
  998. return (mPart0 == 0) && // Check mPart0 first as this will likely yield faster execution.
  999. (mPart1 == 0) &&
  1000. (mPart2 == 0) &&
  1001. (mPart3 == 0);
  1002. #endif
  1003. }
  1004. void int128_t_base::SetZero()
  1005. {
  1006. #if EA_INT128_USE_INT64
  1007. mPart1 = 0;
  1008. mPart0 = 0;
  1009. #else
  1010. mPart3 = 0;
  1011. mPart2 = 0;
  1012. mPart1 = 0;
  1013. mPart0 = 0;
  1014. #endif
  1015. }
  1016. void int128_t_base::TwosComplement()
  1017. {
  1018. #if EA_INT128_USE_INT64
  1019. mPart1 = ~mPart1;
  1020. mPart0 = ~mPart0;
  1021. #else
  1022. mPart3 = ~mPart3;
  1023. mPart2 = ~mPart2;
  1024. mPart1 = ~mPart1;
  1025. mPart0 = ~mPart0;
  1026. #endif
  1027. // What we want to do, but isn't available at this level:
  1028. // operator++();
  1029. // Alternative:
  1030. int128_t_base one((uint32_t)1);
  1031. operatorPlus(*this, one, *this);
  1032. }
  1033. void int128_t_base::InverseTwosComplement()
  1034. {
  1035. // What we want to do, but isn't available at this level:
  1036. // operator--();
  1037. // Alternative:
  1038. int128_t_base one((uint32_t)1);
  1039. operatorMinus(*this, one, *this);
  1040. #if EA_INT128_USE_INT64
  1041. mPart1 = ~mPart1;
  1042. mPart0 = ~mPart0;
  1043. #else
  1044. mPart3 = ~mPart3;
  1045. mPart2 = ~mPart2;
  1046. mPart1 = ~mPart1;
  1047. mPart0 = ~mPart0;
  1048. #endif
  1049. }
  1050. void int128_t_base::DoubleToUint128(double value)
  1051. {
  1052. // Currently this function is limited to 64 bits of integer input.
  1053. // We need to make a better version of this function. Perhaps we should implement
  1054. // it via dissecting the IEEE floating point format (sign, exponent, matissa).
  1055. // EA_ASSERT(fabs(value) < 18446744073709551616.0); // Assert that the input is <= 64 bits of integer.
  1056. #if EA_INT128_USE_INT64
  1057. mPart1 = 0;
  1058. mPart0 = (value >= 0 ? (uint64_t)value : (uint64_t)-value);
  1059. #else
  1060. const uint64_t value64 = (value >= 0 ? (uint64_t)value : (uint64_t)-value);
  1061. mPart3 = 0;
  1062. mPart2 = 0;
  1063. mPart1 = (uint32_t) (value64 >> 32);
  1064. mPart0 = (uint32_t)((value64 >> 0) & 0xffffffff);
  1065. // Below is a version I have been working on a version that works up to the full 128 bits.
  1066. // The implementation below has a roundoff problem for some cases and would have to be reworked.
  1067. /*
  1068. double valueTemp(value);
  1069. if(value < 0)
  1070. valueTemp = -valueTemp;
  1071. //Get part3
  1072. mPart3 = (uint32_t)(valueTemp / 79228162514264337593543950336.0); // 79228162514264337593543950336.0 is the same as 0xffffffffffffffffffffffff + 1, or 0x1000000000000000000000000.
  1073. valueTemp -= (mPart3 * 79228162514264337593543950336.0);
  1074. //Get part2
  1075. mPart2 = (uint32_t)(valueTemp / 18446744073709551616.0); // 18446744073709551616.0 is the same as 0xffffffffffffffff + 1, or 0x10000000000000000.
  1076. valueTemp -= (mPart2 * 18446744073709551616.0);
  1077. //Get part1
  1078. mPart1 = (uint32_t)(valueTemp / 4294967296.0); // 4294967296.0 is the same as 0xffffffff + 1, or 0x100000000.
  1079. valueTemp -= (mPart1 * 4294967296.0);
  1080. //Get part0
  1081. mPart0 = (uint32_t)(valueTemp);
  1082. */
  1083. #endif
  1084. }
  1085. ///////////////////////////////////////////////////////////////////////////////
  1086. // int128_t
  1087. ///////////////////////////////////////////////////////////////////////////////
  1088. int128_t::int128_t()
  1089. #if EA_INT128_USE_INT64
  1090. : int128_t_base(0, 0)
  1091. #else
  1092. : int128_t_base(0, 0, 0, 0)
  1093. #endif
  1094. {
  1095. }
  1096. int128_t::int128_t(uint32_t nPart0, uint32_t nPart1, uint32_t nPart2, uint32_t nPart3)
  1097. : int128_t_base(nPart0, nPart1, nPart2, nPart3) // OK for EA_INT128_USE_INT64
  1098. {
  1099. }
  1100. int128_t::int128_t(uint64_t nPart0, uint64_t nPart1)
  1101. : int128_t_base(nPart0, nPart1) // OK for EA_INT128_USE_INT64
  1102. {
  1103. }
  1104. int128_t::int128_t(uint8_t value)
  1105. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1106. {
  1107. }
  1108. int128_t::int128_t(uint16_t value)
  1109. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1110. {
  1111. }
  1112. int128_t::int128_t(uint32_t value)
  1113. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1114. {
  1115. }
  1116. #if defined(INT128_UINT_TYPE)
  1117. int128_t::int128_t(INT128_UINT_TYPE value)
  1118. : int128_t_base((uint64_t)value) // OK for EA_INT128_USE_INT64
  1119. {
  1120. }
  1121. #endif
  1122. int128_t::int128_t(uint64_t value)
  1123. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1124. {
  1125. }
  1126. int128_t::int128_t(int8_t value)
  1127. {
  1128. if(value < 0)
  1129. {
  1130. *this = int128_t((uint8_t)-value);
  1131. TwosComplement();
  1132. }
  1133. else
  1134. {
  1135. #if EA_INT128_USE_INT64
  1136. mPart1 = 0;
  1137. mPart0 = value;
  1138. #else
  1139. mPart3 = 0;
  1140. mPart2 = 0;
  1141. mPart1 = 0;
  1142. mPart0 = value;
  1143. #endif
  1144. }
  1145. }
  1146. int128_t::int128_t(int16_t value)
  1147. {
  1148. if(value < 0)
  1149. {
  1150. *this = int128_t((uint16_t)-value);
  1151. TwosComplement();
  1152. }
  1153. else
  1154. {
  1155. #if EA_INT128_USE_INT64
  1156. mPart1 = 0;
  1157. mPart0 = value;
  1158. #else
  1159. mPart3 = 0;
  1160. mPart2 = 0;
  1161. mPart1 = 0;
  1162. mPart0 = value;
  1163. #endif
  1164. }
  1165. }
  1166. int128_t::int128_t(int32_t value)
  1167. {
  1168. if(value < 0)
  1169. {
  1170. *this = int128_t((uint32_t)-value);
  1171. TwosComplement();
  1172. }
  1173. else
  1174. {
  1175. #if EA_INT128_USE_INT64
  1176. mPart1 = 0;
  1177. mPart0 = value;
  1178. #else
  1179. mPart3 = 0;
  1180. mPart2 = 0;
  1181. mPart1 = 0;
  1182. mPart0 = (uint32_t)value;
  1183. #endif
  1184. }
  1185. }
  1186. #if defined(INT128_INT_TYPE)
  1187. int128_t::int128_t(INT128_INT_TYPE value)
  1188. {
  1189. operator=(int128_t((int64_t)value));
  1190. }
  1191. #endif
  1192. int128_t::int128_t(int64_t value)
  1193. {
  1194. if(value < 0)
  1195. {
  1196. *this = int128_t((int64_t)-value);
  1197. TwosComplement();
  1198. }
  1199. else
  1200. {
  1201. #if EA_INT128_USE_INT64
  1202. mPart1 = 0;
  1203. mPart0 = (uint64_t) (value);
  1204. #else
  1205. mPart3 = 0;
  1206. mPart2 = 0;
  1207. mPart1 = (uint32_t) ((value >> 32) & 0xffffffff);
  1208. mPart0 = (uint32_t) (value & 0xffffffff);
  1209. #endif
  1210. }
  1211. }
  1212. int128_t::int128_t(const int128_t& value)
  1213. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1214. {
  1215. }
  1216. // Not defined because doing so would make the compiler unable to
  1217. // decide how to choose binary functions involving int128/uint128.
  1218. //int128_t::int128_t(const uint128_t& value)
  1219. // : int128_t_base(value) // OK for EA_INT128_USE_INT64
  1220. //{
  1221. //}
  1222. int128_t::int128_t(const float value)
  1223. {
  1224. // OK for EA_INT128_USE_INT64
  1225. DoubleToUint128(value);
  1226. if(value < 0)
  1227. Negate();
  1228. }
  1229. int128_t::int128_t(const double value)
  1230. {
  1231. // OK for EA_INT128_USE_INT64
  1232. DoubleToUint128(value);
  1233. if(value < 0)
  1234. Negate();
  1235. }
  1236. int128_t::int128_t(const char* pValue, int nBase){
  1237. // OK for EA_INT128_USE_INT64
  1238. const int128_t value(StrToInt128(pValue, NULL, nBase));
  1239. operator=(value);
  1240. }
  1241. int128_t::int128_t(const wchar_t* pValue, int nBase){
  1242. // OK for EA_INT128_USE_INT64
  1243. wchar_t* pTextEnd(NULL);
  1244. const int128_t value(StrToInt128(pValue, &pTextEnd, nBase));
  1245. operator=(value);
  1246. }
  1247. int128_t& int128_t::operator=(const int128_t_base& value)
  1248. {
  1249. // C++ requires operator= to be subclassed, even if the subclassed
  1250. // implementation is identical to the base implementation.
  1251. // OK for EA_INT128_USE_INT64
  1252. int128_t_base::operator=(value);
  1253. return *this;
  1254. }
  1255. int128_t int128_t::operator-() const
  1256. {
  1257. // OK for EA_INT128_USE_INT64
  1258. int128_t returnValue(*this);
  1259. returnValue.Negate();
  1260. return returnValue;
  1261. }
  1262. int128_t& int128_t::operator++()
  1263. {
  1264. // OK for EA_INT128_USE_INT64
  1265. int128_t_base one((uint32_t)1);
  1266. operatorPlus(*this, one, *this);
  1267. return *this;
  1268. }
  1269. int128_t& int128_t::operator--()
  1270. {
  1271. // OK for EA_INT128_USE_INT64
  1272. int128_t_base one((uint32_t)1);
  1273. operatorMinus(*this, one, *this);
  1274. return *this;
  1275. }
  1276. int128_t int128_t::operator++(int)
  1277. {
  1278. // OK for EA_INT128_USE_INT64
  1279. int128_t temp((uint32_t)1);
  1280. operatorPlus(*this, temp, temp);
  1281. return temp;
  1282. }
  1283. int128_t int128_t::operator--(int)
  1284. {
  1285. // OK for EA_INT128_USE_INT64
  1286. int128_t temp((uint32_t)1);
  1287. operatorMinus(*this, temp, temp);
  1288. return temp;
  1289. }
  1290. int128_t int128_t::operator+() const
  1291. {
  1292. // OK for EA_INT128_USE_INT64
  1293. return *this;
  1294. }
  1295. int128_t int128_t::operator~() const
  1296. {
  1297. #if EA_INT128_USE_INT64
  1298. return int128_t(~mPart0, ~mPart1);
  1299. #else
  1300. return int128_t(~mPart0, ~mPart1, ~mPart2, ~mPart3);
  1301. #endif
  1302. }
  1303. int128_t operator+(const int128_t& value1, const int128_t& value2)
  1304. {
  1305. // OK for EA_INT128_USE_INT64
  1306. int128_t temp;
  1307. int128_t::operatorPlus(value1, value2, temp);
  1308. return temp;
  1309. }
  1310. int128_t operator-(const int128_t& value1, const int128_t& value2)
  1311. {
  1312. // OK for EA_INT128_USE_INT64
  1313. int128_t temp;
  1314. int128_t::operatorMinus(value1, value2, temp);
  1315. return temp;
  1316. }
  1317. ///////////////////////////////////////////////////////////////////////////////
  1318. // operator *
  1319. //
  1320. int128_t operator*(const int128_t& value1, const int128_t& value2)
  1321. {
  1322. int128_t a(value1);
  1323. int128_t b(value2);
  1324. int128_t returnValue;
  1325. // Correctly handle negative values
  1326. bool bANegative(false);
  1327. bool bBNegative(false);
  1328. if(a.IsNegative())
  1329. {
  1330. bANegative = true;
  1331. a.Negate();
  1332. }
  1333. if(b.IsNegative())
  1334. {
  1335. bBNegative = true;
  1336. b.Negate();
  1337. }
  1338. int128_t_base::operatorMul(a, b, returnValue);
  1339. // Do negation as needed.
  1340. if(bANegative != bBNegative)
  1341. returnValue.Negate();
  1342. return returnValue;
  1343. }
  1344. int128_t operator/(const int128_t& value1, const int128_t& value2)
  1345. {
  1346. // OK for EA_INT128_USE_INT64
  1347. int128_t remainder;
  1348. int128_t quotient;
  1349. value1.Modulus(value2, quotient, remainder);
  1350. return quotient;
  1351. }
  1352. int128_t operator%(const int128_t& value1, const int128_t& value2)
  1353. {
  1354. // OK for EA_INT128_USE_INT64
  1355. int128_t remainder;
  1356. int128_t quotient;
  1357. value1.Modulus(value2, quotient, remainder);
  1358. return remainder;
  1359. }
  1360. int128_t& int128_t::operator+=(const int128_t& value)
  1361. {
  1362. // OK for EA_INT128_USE_INT64
  1363. operatorPlus(*this, value, *this);
  1364. return *this;
  1365. }
  1366. int128_t& int128_t::operator-=(const int128_t& value)
  1367. {
  1368. // OK for EA_INT128_USE_INT64
  1369. operatorMinus(*this, value, *this);
  1370. return *this;
  1371. }
  1372. int128_t& int128_t::operator*=(const int128_t& value)
  1373. {
  1374. // OK for EA_INT128_USE_INT64
  1375. *this = *this * value;
  1376. return *this;
  1377. }
  1378. int128_t& int128_t::operator/=(const int128_t& value)
  1379. {
  1380. // OK for EA_INT128_USE_INT64
  1381. *this = *this / value;
  1382. return *this;
  1383. }
  1384. int128_t& int128_t::operator%=(const int128_t& value)
  1385. {
  1386. // OK for EA_INT128_USE_INT64
  1387. *this = *this % value;
  1388. return *this;
  1389. }
  1390. // With rightward shifts of negative numbers, shift in zero from the left side.
  1391. int128_t int128_t::operator>>(int nShift) const
  1392. {
  1393. // OK for EA_INT128_USE_INT64
  1394. int128_t temp;
  1395. operatorShiftRight(*this, nShift, temp);
  1396. return temp;
  1397. }
  1398. // With rightward shifts of negative numbers, shift in zero from the left side.
  1399. int128_t int128_t::operator<<(int nShift) const
  1400. {
  1401. // OK for EA_INT128_USE_INT64
  1402. int128_t temp;
  1403. operatorShiftLeft(*this, nShift, temp);
  1404. return temp;
  1405. }
  1406. int128_t& int128_t::operator>>=(int nShift)
  1407. {
  1408. // OK for EA_INT128_USE_INT64
  1409. int128_t temp;
  1410. operatorShiftRight(*this, nShift, temp);
  1411. *this = temp;
  1412. return *this;
  1413. }
  1414. int128_t& int128_t::operator<<=(int nShift)
  1415. {
  1416. // OK for EA_INT128_USE_INT64
  1417. int128_t temp;
  1418. operatorShiftLeft(*this, nShift, temp);
  1419. *this = temp;
  1420. return *this;
  1421. }
  1422. int128_t operator^(const int128_t& value1, const int128_t& value2)
  1423. {
  1424. // OK for EA_INT128_USE_INT64
  1425. int128_t temp;
  1426. int128_t::operatorXOR(value1, value2, temp);
  1427. return temp;
  1428. }
  1429. int128_t operator|(const int128_t& value1, const int128_t& value2)
  1430. {
  1431. // OK for EA_INT128_USE_INT64
  1432. int128_t temp;
  1433. int128_t::operatorOR(value1, value2, temp);
  1434. return temp;
  1435. }
  1436. int128_t operator&(const int128_t& value1, const int128_t& value2)
  1437. {
  1438. // OK for EA_INT128_USE_INT64
  1439. int128_t temp;
  1440. int128_t::operatorAND(value1, value2, temp);
  1441. return temp;
  1442. }
  1443. int128_t& int128_t::operator^=(const int128_t& value)
  1444. {
  1445. // OK for EA_INT128_USE_INT64
  1446. operatorXOR(*this, value, *this);
  1447. return *this;
  1448. }
  1449. int128_t& int128_t::operator|=(const int128_t& value)
  1450. {
  1451. // OK for EA_INT128_USE_INT64
  1452. operatorOR(*this, value, *this);
  1453. return *this;
  1454. }
  1455. int128_t& int128_t::operator&=(const int128_t& value)
  1456. {
  1457. // OK for EA_INT128_USE_INT64
  1458. operatorAND(*this, value, *this);
  1459. return *this;
  1460. }
  1461. // This function forms the basis of all logical comparison functions.
  1462. // If value1 < value2, the return value is -1.
  1463. // If value1 == value2, the return value is 0.
  1464. // If value1 > value2, the return value is 1.
  1465. int compare(const int128_t& value1, const int128_t& value2)
  1466. {
  1467. // Cache some values. Positive means >= 0. Negative means < 0 and thus means '!positive'.
  1468. const bool bValue1IsPositive(value1.IsPositive());
  1469. const bool bValue2IsPositive(value2.IsPositive());
  1470. // Do positive/negative tests.
  1471. if(bValue1IsPositive != bValue2IsPositive)
  1472. return bValue1IsPositive ? 1 : -1;
  1473. // Compare individual parts. At this point, the two numbers have the same sign.
  1474. #if EA_INT128_USE_INT64
  1475. if(value1.mPart1 == value2.mPart1)
  1476. {
  1477. if(value1.mPart0 == value2.mPart0)
  1478. return 0;
  1479. else if(value1.mPart0 > value2.mPart0)
  1480. return 1;
  1481. // return -1; //Just fall through to the end.
  1482. }
  1483. else if(value1.mPart1 > value2.mPart1)
  1484. return 1;
  1485. return -1;
  1486. #else
  1487. if(value1.mPart3 == value2.mPart3)
  1488. {
  1489. if(value1.mPart2 == value2.mPart2)
  1490. {
  1491. if(value1.mPart1 == value2.mPart1)
  1492. {
  1493. if(value1.mPart0 == value2.mPart0)
  1494. return 0;
  1495. else if(value1.mPart0 > value2.mPart0)
  1496. return 1;
  1497. // return -1; //Just fall through to the end.
  1498. }
  1499. else if(value1.mPart1 > value2.mPart1)
  1500. return 1;
  1501. // return -1; //Just fall through to the end.
  1502. }
  1503. else if(value1.mPart2 > value2.mPart2)
  1504. return 1;
  1505. // return -1; //Just fall through to the end.
  1506. }
  1507. else if(value1.mPart3 > value2.mPart3)
  1508. return 1;
  1509. return -1;
  1510. #endif
  1511. }
  1512. bool operator==(const int128_t& value1, const int128_t& value2)
  1513. {
  1514. #if EA_INT128_USE_INT64
  1515. return (value1.mPart0 == value2.mPart0) && // Check mPart0 first as this will likely yield faster execution.
  1516. (value1.mPart1 == value2.mPart1);
  1517. #else
  1518. return (value1.mPart0 == value2.mPart0) && // Check mPart0 first as this will likely yield faster execution.
  1519. (value1.mPart1 == value2.mPart1) &&
  1520. (value1.mPart2 == value2.mPart2) &&
  1521. (value1.mPart3 == value2.mPart3);
  1522. #endif
  1523. }
  1524. bool operator!=(const int128_t& value1, const int128_t& value2)
  1525. {
  1526. #if EA_INT128_USE_INT64
  1527. return (value1.mPart0 != value2.mPart0) || // Check mPart0 first as this will likely yield faster execution.
  1528. (value1.mPart1 != value2.mPart1);
  1529. #else
  1530. return (value1.mPart0 != value2.mPart0) || // Check mPart0 first as this will likely yield faster execution.
  1531. (value1.mPart1 != value2.mPart1) ||
  1532. (value1.mPart2 != value2.mPart2) ||
  1533. (value1.mPart3 != value2.mPart3);
  1534. #endif
  1535. }
  1536. bool operator>(const int128_t& value1, const int128_t& value2)
  1537. {
  1538. // OK for EA_INT128_USE_INT64
  1539. return (compare(value1, value2) > 0);
  1540. }
  1541. bool operator>=(const int128_t& value1, const int128_t& value2)
  1542. {
  1543. // OK for EA_INT128_USE_INT64
  1544. return (compare(value1, value2) >= 0);
  1545. }
  1546. bool operator<(const int128_t& value1, const int128_t& value2)
  1547. {
  1548. // OK for EA_INT128_USE_INT64
  1549. return (compare(value1, value2) < 0);
  1550. }
  1551. bool operator<=(const int128_t& value1, const int128_t& value2)
  1552. {
  1553. // OK for EA_INT128_USE_INT64
  1554. return (compare(value1, value2) <= 0);
  1555. }
  1556. int8_t int128_t::AsInt8() const
  1557. {
  1558. // OK for EA_INT128_USE_INT64
  1559. if(IsNegative())
  1560. {
  1561. int128_t t(*this);
  1562. t.Negate();
  1563. return (int8_t)-t.AsInt8();
  1564. }
  1565. return (int8_t) mPart0;
  1566. }
  1567. int16_t int128_t::AsInt16() const
  1568. {
  1569. // OK for EA_INT128_USE_INT64
  1570. if(IsNegative())
  1571. {
  1572. int128_t t(*this);
  1573. t.Negate();
  1574. return (int16_t)-t.AsInt16();
  1575. }
  1576. return (int16_t) mPart0;
  1577. }
  1578. int32_t int128_t::AsInt32() const
  1579. {
  1580. // OK for EA_INT128_USE_INT64
  1581. if(IsNegative())
  1582. {
  1583. int128_t t(*this);
  1584. t.Negate();
  1585. return -t.AsInt32();
  1586. }
  1587. return (int32_t) mPart0;
  1588. }
  1589. int64_t int128_t::AsInt64() const
  1590. {
  1591. if(IsNegative())
  1592. {
  1593. int128_t t(*this);
  1594. t.Negate();
  1595. return -t.AsUint64(); // ensure mod2 behaviour
  1596. }
  1597. #if EA_INT128_USE_INT64
  1598. return (int64_t) mPart0;
  1599. #else
  1600. return (((int64_t) mPart1) << 32) + mPart0;
  1601. #endif
  1602. }
  1603. // I am not convinced that this is a reliable method of conversion.
  1604. float int128_t::AsFloat() const
  1605. {
  1606. if(IsNegative())
  1607. {
  1608. int128_t t(*this);
  1609. t.Negate();
  1610. return -t.AsFloat();
  1611. }
  1612. float fReturnValue(0);
  1613. #if EA_INT128_USE_INT64
  1614. if(mPart1)
  1615. fReturnValue += (mPart1 * 18446744073709551616.f);
  1616. if(mPart0)
  1617. fReturnValue += (float)mPart0;
  1618. #else
  1619. if(mPart3)
  1620. fReturnValue += (mPart3 * 79228162514264337593543950336.f);
  1621. if(mPart2)
  1622. fReturnValue += (mPart2 * 18446744073709551616.f);
  1623. if(mPart1)
  1624. fReturnValue += (mPart1 * 4294967296.f);
  1625. if(mPart0)
  1626. fReturnValue += (float)mPart0;
  1627. #endif
  1628. return fReturnValue;
  1629. }
  1630. // I am not convinced that this is a reliable method of conversion.
  1631. double int128_t::AsDouble() const
  1632. {
  1633. if(IsNegative())
  1634. {
  1635. int128_t t(*this);
  1636. t.Negate();
  1637. return -t.AsDouble();
  1638. }
  1639. double fReturnValue(0);
  1640. #if EA_INT128_USE_INT64
  1641. if(mPart1)
  1642. fReturnValue += (mPart1 * 18446744073709551616.0);
  1643. if(mPart0)
  1644. fReturnValue += (double)mPart0;
  1645. #else
  1646. if(mPart3)
  1647. fReturnValue += (mPart3 * 79228162514264337593543950336.0);
  1648. if(mPart2)
  1649. fReturnValue += (mPart2 * 18446744073709551616.0);
  1650. if(mPart1)
  1651. fReturnValue += (mPart1 * 4294967296.0);
  1652. if(mPart0)
  1653. fReturnValue += (double)mPart0;
  1654. #endif
  1655. return fReturnValue;
  1656. }
  1657. void int128_t::Negate()
  1658. {
  1659. // OK for EA_INT128_USE_INT64
  1660. if(IsPositive())
  1661. TwosComplement();
  1662. else
  1663. InverseTwosComplement();
  1664. }
  1665. bool int128_t::IsNegative() const
  1666. { // True if value < 0
  1667. #if EA_INT128_USE_INT64
  1668. return ((mPart1 & UINT64_C(0x8000000000000000)) != 0);
  1669. #else
  1670. return ((mPart3 & 0x80000000) != 0);
  1671. #endif
  1672. }
  1673. bool int128_t::IsPositive() const
  1674. { // True of value >= 0
  1675. #if EA_INT128_USE_INT64
  1676. return ((mPart1 & UINT64_C(0x8000000000000000)) == 0);
  1677. #else
  1678. return ((mPart3 & 0x80000000) == 0);
  1679. #endif
  1680. }
  1681. ///////////////////////////////////////////////////////////////////////////////
  1682. // Modulus
  1683. //
  1684. // This is a generic function that does both division modulus calculations.
  1685. //
  1686. void int128_t::Modulus(const int128_t& divisor, int128_t& quotient, int128_t& remainder) const
  1687. {
  1688. // OK for EA_INT128_USE_INT64
  1689. int128_t tempDividend(*this);
  1690. int128_t tempDivisor(divisor);
  1691. bool bDividendNegative = false;
  1692. bool bDivisorNegative = false;
  1693. if(tempDividend.IsNegative())
  1694. {
  1695. bDividendNegative = true;
  1696. tempDividend.Negate();
  1697. }
  1698. if(tempDivisor.IsNegative())
  1699. {
  1700. bDivisorNegative = true;
  1701. tempDivisor.Negate();
  1702. }
  1703. // Handle the special cases
  1704. if(tempDivisor.IsZero())
  1705. {
  1706. // Force a divide by zero exception.
  1707. // We know that tempDivisor.mPart0 is zero.
  1708. quotient.mPart0 /= tempDivisor.mPart0;
  1709. }
  1710. else if(tempDividend.IsZero())
  1711. {
  1712. quotient = int128_t((uint32_t)0);
  1713. remainder = int128_t((uint32_t)0);
  1714. }
  1715. else
  1716. {
  1717. remainder.SetZero();
  1718. for(int i(0); i < 128; i++)
  1719. {
  1720. remainder += (uint32_t)tempDividend.GetBit(127 - i);
  1721. const bool bBit(remainder >= tempDivisor);
  1722. quotient.SetBit(127 - i, bBit);
  1723. if(bBit)
  1724. remainder -= tempDivisor;
  1725. if((i != 127) && !remainder.IsZero())
  1726. remainder <<= 1;
  1727. }
  1728. }
  1729. if((bDividendNegative && !bDivisorNegative) || (!bDividendNegative && bDivisorNegative))
  1730. {
  1731. // Ensure the following formula applies for negative dividends
  1732. // dividend = divisor * quotient + remainder
  1733. quotient.Negate();
  1734. }
  1735. }
  1736. ///////////////////////////////////////////////////////////////////////////////
  1737. // StrToInt128
  1738. //
  1739. // Same as C runtime strtol function but for int128_t.
  1740. // This is probably the most general and useful of the C atoi family of functions.
  1741. //
  1742. int128_t int128_t::StrToInt128(const char* pValue, char** ppEnd, int nBase)
  1743. {
  1744. int128_t value((uint32_t)0); // Current value
  1745. const char* p = pValue; // Current position
  1746. const char* pBegin = NULL; // Where the digits start.
  1747. const char* pEnd = NULL; // Where the digits end. One-past the last digit.
  1748. char chSign('+'); // One of either '+' or '-'
  1749. // Skip leading whitespace
  1750. while(isspace((unsigned char)*p))
  1751. ++p;
  1752. // Check for sign.
  1753. if((*p == '-') || (*p == '+'))
  1754. chSign = *p++;
  1755. // Do checks on 'nBase'.
  1756. if((nBase < 0) || (nBase == 1) || (nBase > 36)){
  1757. if(ppEnd)
  1758. *ppEnd = (char*)pValue;
  1759. return value;
  1760. }
  1761. else if(nBase == 0){
  1762. // Auto detect one of base 2, 8, 10, or 16.
  1763. if(*p != '0')
  1764. nBase = 10;
  1765. else if((p[1] == 'x') || (p[1] == 'X')) // It's safe to read p[1] because p[0] is known to be '0'.
  1766. nBase = 16;
  1767. else if((p[1] == 'b') || (p[1] == 'B'))
  1768. nBase = 2;
  1769. else
  1770. nBase = 8;
  1771. }
  1772. if(nBase == 16){
  1773. // If there is a leading '0x', then skip past it.
  1774. if((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))
  1775. p += 2;
  1776. }
  1777. else if(nBase == 2){
  1778. // If there is a leading '0b', then skip past it.
  1779. if((*p == '0') && ((p[1] == 'b') || (p[1] == 'B')))
  1780. p += 2;
  1781. }
  1782. // Save the position where the digits start.
  1783. pBegin = p;
  1784. if(nBase == 2) // Binary
  1785. {
  1786. while((*p == '0') || (*p == '1'))
  1787. p++;
  1788. pEnd = p;
  1789. if(pEnd > pBegin + 128) // There can be at most 128 binary digits in the string.
  1790. {
  1791. pEnd = pBegin + 128;
  1792. p = pEnd;
  1793. }
  1794. for(int i(0); p > pBegin; ++i)
  1795. {
  1796. --p;
  1797. if(*p == '1')
  1798. value.SetBit(i, true);
  1799. }
  1800. }
  1801. else if(nBase == 10) // Decimal
  1802. {
  1803. while(isdigit((unsigned char)*p))
  1804. ++p;
  1805. pEnd = p;
  1806. if(pEnd > pBegin + 39) // With base 10, it is not enough to simply check against 39 digits,
  1807. { // as you can have 39 '9's and overflow. But 39 is the most you could have.
  1808. pEnd = pBegin + 39;
  1809. p = pEnd;
  1810. }
  1811. int128_t multiplier((uint32_t)1);
  1812. for(int i(0); p > pBegin; ++i)
  1813. {
  1814. const uint32_t c = (uint32_t)(*(--p) - '0');
  1815. if(c)
  1816. {
  1817. // This can be optimized for faster speed by doing the smaller orders
  1818. // of ten on value.mPart0 with an int multiplier instead of on value
  1819. // and a int128_t multiplier.
  1820. value += (multiplier * c);
  1821. }
  1822. multiplier *= (uint32_t)10;
  1823. }
  1824. }
  1825. else if(nBase == 16) // Hexadecimal
  1826. {
  1827. while(isxdigit((unsigned char)*p))
  1828. p++;
  1829. pEnd = p;
  1830. if(pEnd > pBegin + 32) // There can be at most 32 hexadecimal digits in the string.
  1831. {
  1832. pEnd = pBegin + 32;
  1833. p = pEnd;
  1834. }
  1835. // There can be as many as 32 characters.
  1836. for(int i(0); p > pBegin; i++)
  1837. {
  1838. #if EA_INT128_USE_INT64
  1839. const int nPart = (int)((pEnd - p) / 16);
  1840. uint64_t c = *(--p); // c is an integer in the range of [0,15].
  1841. #else
  1842. const int nPart = (int)((pEnd - p) / 8);
  1843. uint32_t c = *(--p); // c is an integer in the range of [0,15].
  1844. #endif
  1845. if(c >= '0' && c <= '9')
  1846. c = (c - '0');
  1847. else if(c >= 'a' && c <= 'f')
  1848. c = 10 + (c - 'a');
  1849. else
  1850. c = 10 + (c - 'A');
  1851. if(c)
  1852. {
  1853. #if EA_INT128_USE_INT64
  1854. c <<= ((i % 16) * 4);
  1855. if(nPart == 0)
  1856. value.mPart0 |= c;
  1857. else if(nPart == 1)
  1858. value.mPart1 |= c;
  1859. #else
  1860. c <<= ((i % 8) * 4);
  1861. if(nPart == 0)
  1862. value.mPart0 |= c;
  1863. else if(nPart == 1)
  1864. value.mPart1 |= c;
  1865. else if(nPart == 2)
  1866. value.mPart2 |= c;
  1867. else if(nPart == 3)
  1868. value.mPart3 |= c;
  1869. #endif
  1870. }
  1871. }
  1872. }
  1873. else
  1874. {
  1875. // EA_ASSERT(false); // For the time being, we handle only the above bases. But that's all that's required by the standard.
  1876. }
  1877. if(chSign == '-')
  1878. value.Negate();
  1879. if(ppEnd)
  1880. *ppEnd = (char*)pEnd;
  1881. return value;
  1882. }
  1883. ///////////////////////////////////////////////////////////////////////////////
  1884. // StrToInt128
  1885. //
  1886. // Same as C runtime strtol function but for int128_t.
  1887. // This is probably the most general and useful of the C atoi family of functions.
  1888. //
  1889. int128_t int128_t::StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int nBase)
  1890. {
  1891. // This is simply a copy and paste of the char version of StrToInt128, with minor
  1892. // modifications for wchar_t.
  1893. // To consider: Make an alternative implementation of this which converts the wchar_t
  1894. // buffer to char and uses the char version. Doing this properly would involve more
  1895. // than a trivial number of lines of code, and so for the time being we do the copy/paste.
  1896. int128_t value((uint32_t)0); // Current value
  1897. const wchar_t* p = pValue; // Current position
  1898. const wchar_t* pBegin = NULL; // Where the digits start.
  1899. const wchar_t* pEnd = NULL; // Where the digits end. One-past the last digit.
  1900. wchar_t chSign('+'); // One of either '+' or '-'
  1901. // Skip leading whitespace
  1902. while((*p > 0) && (*p < 127) && isspace((uint8_t)*p)) // Compare to < 127 because ctype functions will crash for higher values.
  1903. ++p;
  1904. // Check for sign.
  1905. if((*p == '-') || (*p == '+'))
  1906. chSign = *p++;
  1907. // Do checks on 'nBase'.
  1908. if((nBase < 0) || (nBase == 1) || (nBase > 36)){
  1909. if(ppEnd)
  1910. *ppEnd = (wchar_t*)pValue;
  1911. return value;
  1912. }
  1913. else if(nBase == 0){
  1914. // Auto detect one of base 2, 8, 10, or 16.
  1915. if(*p != '0')
  1916. nBase = 10;
  1917. else if((p[1] == 'x') || (p[1] == 'X'))
  1918. nBase = 16;
  1919. else if((p[1] == 'b') || (p[1] == 'B'))
  1920. nBase = 2;
  1921. else
  1922. nBase = 8;
  1923. }
  1924. if(nBase == 16){
  1925. // If there is a leading '0x', then skip past it.
  1926. if((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))
  1927. p += 2;
  1928. }
  1929. else if(nBase == 2){
  1930. // If there is a leading '0b', then skip past it.
  1931. if((*p == '0') && ((p[1] == 'b') || (p[1] == 'B')))
  1932. p += 2;
  1933. }
  1934. // Save the position where the digits start.
  1935. pBegin = p;
  1936. if(nBase == 2) // Binary
  1937. {
  1938. while((*p == '0') || (*p == '1'))
  1939. p++;
  1940. pEnd = p;
  1941. if(pEnd > pBegin + 128) // There can be at most 128 binary digits in the string.
  1942. {
  1943. pEnd = pBegin + 128;
  1944. p = pEnd;
  1945. }
  1946. for(int i(0); p > pBegin; ++i)
  1947. {
  1948. --p;
  1949. if(*p == '1')
  1950. value.SetBit(i, true);
  1951. }
  1952. }
  1953. else if(nBase == 10) // Decimal
  1954. {
  1955. while((*p > 0) && (*p < 127) && isdigit((uint8_t)*p)) // Compare to < 127 because ctype functions will crash for higher values.
  1956. ++p;
  1957. pEnd = p;
  1958. if(pEnd > pBegin + 39) // With base 10, it is not enough to simply check against 39 digits,
  1959. { // as you can have 39 '9's and overflow. But 39 is the most you could have.
  1960. pEnd = pBegin + 39;
  1961. p = pEnd;
  1962. }
  1963. int128_t multiplier((uint32_t)1);
  1964. for(int i(0); p > pBegin; ++i)
  1965. {
  1966. const uint32_t c = (uint32_t)(*(--p) - '0');
  1967. if(c)
  1968. {
  1969. // This can be optimized for faster speed by doing the smaller orders
  1970. // of ten on value.mPart0 with an int multiplier instead of on value
  1971. // and a int128_t multiplier.
  1972. value += (multiplier * c);
  1973. }
  1974. multiplier *= (uint32_t)10;
  1975. }
  1976. }
  1977. else if(nBase == 16) // Hexadecimal
  1978. {
  1979. while((*p > 0) && (*p < 127) && isxdigit(*p)) // Compare to < 127 because ctype functions will crash for higher values.
  1980. p++;
  1981. pEnd = p;
  1982. if(pEnd > pBegin + 32) // There can be at most 32 hexadecimal digits in the string.
  1983. {
  1984. pEnd = pBegin + 32;
  1985. p = pEnd;
  1986. }
  1987. // There can be as many as 32 characters.
  1988. for(int i(0); p > pBegin; i++)
  1989. {
  1990. #if EA_INT128_USE_INT64
  1991. const int nPart = (int)((pEnd - p) / 16);
  1992. uint64_t c = *(--p); // c is an integer in the range of [0,15].
  1993. #else
  1994. const int nPart = (int)((pEnd - p) / 8);
  1995. uint32_t c = *(--p); // c is an integer in the range of [0,15].
  1996. #endif
  1997. if(c >= '0' && c <= '9')
  1998. c = (c - '0');
  1999. else if(c >= 'a' && c <= 'f')
  2000. c = 10 + (c - 'a');
  2001. else
  2002. c = 10 + (c - 'A');
  2003. if(c)
  2004. {
  2005. #if EA_INT128_USE_INT64
  2006. c <<= ((i % 16) * 4);
  2007. if(nPart == 0)
  2008. value.mPart0 |= c;
  2009. else if(nPart == 1)
  2010. value.mPart1 |= c;
  2011. #else
  2012. c <<= ((i % 8) * 4);
  2013. if(nPart == 0)
  2014. value.mPart0 |= c;
  2015. else if(nPart == 1)
  2016. value.mPart1 |= c;
  2017. else if(nPart == 2)
  2018. value.mPart2 |= c;
  2019. else if(nPart == 3)
  2020. value.mPart3 |= c;
  2021. #endif
  2022. }
  2023. }
  2024. }
  2025. else
  2026. {
  2027. // EA_ASSERT(false); // For the time being, we handle only the above bases. But that's all that's required by the standard.
  2028. }
  2029. if(chSign == '-')
  2030. value.Negate();
  2031. if(ppEnd)
  2032. *ppEnd = (wchar_t*)pEnd;
  2033. return value;
  2034. }
  2035. ///////////////////////////////////////////////////////////////////////////////
  2036. // Int128ToStr
  2037. //
  2038. // Returned string has a NULL appended to it.
  2039. // Upon return, ppEnd points to the terminating NULL.
  2040. // Thus, ppEnd - pValue => string length.
  2041. //
  2042. // bPrefix applies only to base 2 (0b) and base 16 (0x).
  2043. //
  2044. void int128_t::Int128ToStr(char* pValue, char** ppEnd, int nBase, LeadingZeroes lz, Prefix prefix) const
  2045. {
  2046. if(nBase == 2)
  2047. {
  2048. bool bLeadingZeros = (lz == kLZEnable); // By default leading zeroes are disabled.
  2049. bool bPrefix = (prefix == kPrefixEnable); // By default prefix is disabled.
  2050. if(bPrefix)
  2051. {
  2052. *pValue++ = '0';
  2053. *pValue++ = 'b';
  2054. }
  2055. if(IsZero())
  2056. {
  2057. if(bLeadingZeros)
  2058. {
  2059. for(int i(0); i < 128; i++)
  2060. *pValue++ = '0';
  2061. }
  2062. else
  2063. *pValue++ = '0'; // This is all we need to write.
  2064. }
  2065. else
  2066. {
  2067. // Print out the text.
  2068. bool bNonZeroFound(false);
  2069. for(int i(127); i >= 0; --i)
  2070. {
  2071. const int bBitIsSet(GetBit(i));
  2072. if(bBitIsSet)
  2073. bNonZeroFound = true;
  2074. if(bLeadingZeros || bNonZeroFound)
  2075. *pValue++ = (bBitIsSet ? '1' : '0');
  2076. }
  2077. }
  2078. }
  2079. else if(nBase == 10)
  2080. {
  2081. // To do: Support leading zeroes and prefix for base 10.
  2082. if(*this == EASTDC_INT128_MIN)
  2083. {
  2084. // This code has a special pathway because negating EASTDC_INT128_MIN results
  2085. // in EASTDC_INT128_MIN and thus the code below can't work.
  2086. static const char* pINT128_MIN = "-170141183460469231731687303715884105728";
  2087. for(const char* pCurrent = pINT128_MIN; *pCurrent; ++pCurrent, ++pValue)
  2088. *pValue = *pCurrent;
  2089. }
  2090. else
  2091. {
  2092. int128_t value(*this);
  2093. char* pValueInitial = pValue;
  2094. const bool bNegative(IsNegative());
  2095. if(bNegative)
  2096. {
  2097. value.Negate();
  2098. *pValue++ = '-';
  2099. }
  2100. // This part here isn't particularly fast.
  2101. const int128_t ten((uint32_t)10);
  2102. while (value >= ten)
  2103. {
  2104. const int128_t remainder = (value % ten);
  2105. *pValue++ = (char)('0' + remainder.mPart0);
  2106. value /= (uint32_t)10;
  2107. }
  2108. *pValue++ = (char)('0' + value.mPart0);
  2109. // Reverse the string.
  2110. char* pEnd = pValue - 1;
  2111. if(bNegative)
  2112. ++pValueInitial;
  2113. while(pValueInitial < pEnd)
  2114. {
  2115. char temp = *pValueInitial;
  2116. *pValueInitial = *pEnd;
  2117. *pEnd = temp;
  2118. ++pValueInitial;
  2119. --pEnd;
  2120. }
  2121. }
  2122. }
  2123. else if(nBase == 16)
  2124. {
  2125. bool bLeadingZeros = (lz != kLZDisable); // By default leading zeroes are enabled.
  2126. bool bPrefix = (prefix != kPrefixDisable); // By default prefix is enabled.
  2127. static const char* const pHexCharTable = "0123456789abcdef";
  2128. if(bPrefix)
  2129. {
  2130. *pValue++ = '0';
  2131. *pValue++ = 'x';
  2132. }
  2133. if(IsZero())
  2134. {
  2135. if(bLeadingZeros)
  2136. {
  2137. for(int i(0); i < 32; i++) // 32 is equal to (128 / 16)
  2138. *pValue++ = '0';
  2139. }
  2140. else
  2141. *pValue++ = '0'; // This is all we need to write.
  2142. }
  2143. else
  2144. {
  2145. // Print out the text.
  2146. bool bNonZeroFound(false);
  2147. // Work on each part in turn, starting with the high part.
  2148. #if EA_INT128_USE_INT64
  2149. for(int i(1); i >= 0; --i)
  2150. {
  2151. const uint64_t* pCurrent;
  2152. if(i == 1)
  2153. pCurrent = &mPart1;
  2154. else
  2155. pCurrent = &mPart0;
  2156. // Work on each sub-part (4 bits) or the current part (64 bits), starting with the high sub-part.
  2157. for(int j(60); j >= 0; j -= 4)
  2158. {
  2159. const char c = pHexCharTable[(*pCurrent >> j) & 0x0F];
  2160. if(c != '0')
  2161. bNonZeroFound = true;
  2162. if(bLeadingZeros || bNonZeroFound)
  2163. *pValue++ = c;
  2164. }
  2165. }
  2166. #else
  2167. for(int i(3); i >= 0; --i)
  2168. {
  2169. const uint32_t* pCurrent;
  2170. if(i == 3)
  2171. pCurrent = &mPart3;
  2172. else if(i == 2)
  2173. pCurrent = &mPart2;
  2174. else if(i == 1)
  2175. pCurrent = &mPart1;
  2176. else
  2177. pCurrent = &mPart0;
  2178. // Work on each sub-part (4 bits) or the current part (32 bits), starting with the high sub-part.
  2179. for(int j(28); j >= 0; j -= 4)
  2180. {
  2181. const char c = pHexCharTable[(*pCurrent >> j) & 0x0F];
  2182. if(c != '0')
  2183. bNonZeroFound = true;
  2184. if(bLeadingZeros || bNonZeroFound)
  2185. *pValue++ = c;
  2186. }
  2187. }
  2188. #endif
  2189. }
  2190. }
  2191. else
  2192. {
  2193. // To do: Implement this in a generic way.
  2194. EA_FAIL(); // Base not supported.
  2195. }
  2196. if(ppEnd)
  2197. *ppEnd = pValue;
  2198. *pValue = 0;
  2199. }
  2200. void int128_t::Int128ToStr(wchar_t* pValue, wchar_t** ppEnd, int nBase, LeadingZeroes lz, Prefix prefix) const
  2201. {
  2202. char str8[130];
  2203. char* pEnd = str8;
  2204. Int128ToStr(str8, &pEnd, nBase, lz, prefix);
  2205. for(char* p = str8; p < pEnd;)
  2206. *pValue++ = (wchar_t)(uint8_t)*p++;
  2207. if(ppEnd)
  2208. *ppEnd = pValue;
  2209. *pValue = 0;
  2210. }
  2211. ///////////////////////////////////////////////////////////////////////////////
  2212. // uint128_t
  2213. ///////////////////////////////////////////////////////////////////////////////
  2214. uint128_t::uint128_t()
  2215. #if EA_INT128_USE_INT64
  2216. : int128_t_base(0, 0)
  2217. #else
  2218. : int128_t_base(0, 0, 0, 0)
  2219. #endif
  2220. {
  2221. }
  2222. uint128_t::uint128_t(uint32_t nPart0, uint32_t nPart1, uint32_t nPart2, uint32_t nPart3)
  2223. : int128_t_base(nPart0, nPart1, nPart2, nPart3) // OK for EA_INT128_USE_INT64
  2224. {
  2225. }
  2226. uint128_t::uint128_t(uint64_t nPart0, uint64_t nPart1)
  2227. : int128_t_base(nPart0, nPart1) // OK for EA_INT128_USE_INT64
  2228. {
  2229. }
  2230. uint128_t::uint128_t(uint8_t value)
  2231. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2232. {
  2233. }
  2234. uint128_t::uint128_t(uint16_t value)
  2235. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2236. {
  2237. }
  2238. uint128_t::uint128_t(uint32_t value)
  2239. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2240. {
  2241. }
  2242. #if defined(INT128_UINT_TYPE)
  2243. uint128_t::uint128_t(INT128_UINT_TYPE value)
  2244. : int128_t_base((uint64_t)value) // OK for EA_INT128_USE_INT64
  2245. {
  2246. }
  2247. #endif
  2248. uint128_t::uint128_t(uint64_t value)
  2249. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2250. {
  2251. }
  2252. uint128_t::uint128_t(int8_t value)
  2253. {
  2254. if(value < 0)
  2255. {
  2256. *this = uint128_t((uint8_t)-value);
  2257. TwosComplement();
  2258. }
  2259. else
  2260. {
  2261. #if EA_INT128_USE_INT64
  2262. mPart1 = 0;
  2263. mPart0 = value;
  2264. #else
  2265. mPart3 = 0;
  2266. mPart2 = 0;
  2267. mPart1 = 0;
  2268. mPart0 = value;
  2269. #endif
  2270. }
  2271. }
  2272. uint128_t::uint128_t(int16_t value)
  2273. {
  2274. if(value < 0)
  2275. {
  2276. *this = uint128_t((uint16_t)-value);
  2277. TwosComplement();
  2278. }
  2279. else
  2280. {
  2281. #if EA_INT128_USE_INT64
  2282. mPart1 = 0;
  2283. mPart0 = value;
  2284. #else
  2285. mPart3 = 0;
  2286. mPart2 = 0;
  2287. mPart1 = 0;
  2288. mPart0 = value;
  2289. #endif
  2290. }
  2291. }
  2292. uint128_t::uint128_t(int32_t value)
  2293. {
  2294. if(value < 0)
  2295. {
  2296. *this = uint128_t((uint32_t)-value);
  2297. TwosComplement();
  2298. }
  2299. else
  2300. {
  2301. #if EA_INT128_USE_INT64
  2302. mPart1 = 0;
  2303. mPart0 = value;
  2304. #else
  2305. mPart3 = 0;
  2306. mPart2 = 0;
  2307. mPart1 = 0;
  2308. mPart0 = (uint32_t)value;
  2309. #endif
  2310. }
  2311. }
  2312. #if defined(INT128_INT_TYPE)
  2313. uint128_t::uint128_t(INT128_INT_TYPE value)
  2314. {
  2315. operator=(uint128_t((int64_t)value));
  2316. }
  2317. #endif
  2318. uint128_t::uint128_t(int64_t value)
  2319. {
  2320. if(value < 0)
  2321. {
  2322. *this = uint128_t((uint64_t)-value);
  2323. TwosComplement();
  2324. }
  2325. else
  2326. {
  2327. #if EA_INT128_USE_INT64
  2328. mPart1 = 0;
  2329. mPart0 = (uint64_t) (value);
  2330. #else
  2331. mPart3 = 0;
  2332. mPart2 = 0;
  2333. mPart1 = (uint32_t) ((value >> 32) & 0xffffffff);
  2334. mPart0 = (uint32_t) (value & 0xffffffff);
  2335. #endif
  2336. }
  2337. }
  2338. uint128_t::uint128_t(const float value)
  2339. {
  2340. DoubleToUint128(value); // OK for EA_INT128_USE_INT64
  2341. }
  2342. uint128_t::uint128_t(const double value)
  2343. {
  2344. DoubleToUint128(value); // OK for EA_INT128_USE_INT64
  2345. }
  2346. uint128_t::uint128_t(const int128_t& value)
  2347. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2348. {
  2349. }
  2350. uint128_t::uint128_t(const uint128_t& value)
  2351. : int128_t_base(value) // OK for EA_INT128_USE_INT64
  2352. {
  2353. }
  2354. uint128_t::uint128_t(const char* pValue, int nBase){
  2355. // OK for EA_INT128_USE_INT64
  2356. const uint128_t value(StrToInt128(pValue, NULL, nBase));
  2357. operator=(value);
  2358. }
  2359. uint128_t::uint128_t(const wchar_t* pValue, int nBase){
  2360. // OK for EA_INT128_USE_INT64
  2361. wchar_t* pTextEnd(NULL);
  2362. const uint128_t value(StrToInt128(pValue, &pTextEnd, nBase));
  2363. operator=(value);
  2364. }
  2365. uint128_t& uint128_t::operator=(const int128_t_base& value)
  2366. {
  2367. // C++ requires operator= to be subclassed, even if the subclassed
  2368. // implementation is identical to the base implementation.
  2369. // OK for EA_INT128_USE_INT64
  2370. int128_t_base::operator=(value);
  2371. return *this;
  2372. }
  2373. uint128_t uint128_t::operator-() const
  2374. {
  2375. // OK for EA_INT128_USE_INT64
  2376. uint128_t returnValue(*this);
  2377. returnValue.Negate();
  2378. return returnValue;
  2379. }
  2380. uint128_t& uint128_t::operator++()
  2381. {
  2382. // OK for EA_INT128_USE_INT64
  2383. int128_t_base one((uint32_t)1);
  2384. operatorPlus(*this, one, *this);
  2385. return *this;
  2386. }
  2387. uint128_t& uint128_t::operator--()
  2388. {
  2389. // OK for EA_INT128_USE_INT64
  2390. int128_t_base one((uint32_t)1);
  2391. operatorMinus(*this, one, *this);
  2392. return *this;
  2393. }
  2394. uint128_t uint128_t::operator++(int)
  2395. {
  2396. // OK for EA_INT128_USE_INT64
  2397. uint128_t temp((uint32_t)1);
  2398. operatorPlus(*this, temp, temp);
  2399. return temp;
  2400. }
  2401. uint128_t uint128_t::operator--(int)
  2402. {
  2403. // OK for EA_INT128_USE_INT64
  2404. uint128_t temp((uint32_t)1);
  2405. operatorMinus(*this, temp, temp);
  2406. return temp;
  2407. }
  2408. uint128_t uint128_t::operator+() const
  2409. {
  2410. // OK for EA_INT128_USE_INT64
  2411. return *this;
  2412. }
  2413. uint128_t uint128_t::operator~() const
  2414. {
  2415. #if EA_INT128_USE_INT64
  2416. return uint128_t(~mPart0, ~mPart1);
  2417. #else
  2418. return uint128_t(~mPart0, ~mPart1, ~mPart2, ~mPart3);
  2419. #endif
  2420. }
  2421. uint128_t operator+(const uint128_t& value1, const uint128_t& value2)
  2422. {
  2423. // OK for EA_INT128_USE_INT64
  2424. uint128_t temp;
  2425. uint128_t::operatorPlus(value1, value2, temp);
  2426. return temp;
  2427. }
  2428. uint128_t operator-(const uint128_t& value1, const uint128_t& value2)
  2429. {
  2430. // OK for EA_INT128_USE_INT64
  2431. uint128_t temp;
  2432. uint128_t::operatorMinus(value1, value2, temp);
  2433. return temp;
  2434. }
  2435. ///////////////////////////////////////////////////////////////////////////////
  2436. // operator *
  2437. //
  2438. uint128_t operator*(const uint128_t& value1, const uint128_t& value2)
  2439. {
  2440. uint128_t returnValue;
  2441. int128_t_base::operatorMul(value1, value2, returnValue);
  2442. return returnValue;
  2443. }
  2444. uint128_t operator/(const uint128_t& value1, const uint128_t& value2)
  2445. {
  2446. // OK for EA_INT128_USE_INT64
  2447. uint128_t remainder;
  2448. uint128_t quotient;
  2449. value1.Modulus(value2, quotient, remainder);
  2450. return quotient;
  2451. }
  2452. uint128_t operator%(const uint128_t& value1, const uint128_t& value2)
  2453. {
  2454. // OK for EA_INT128_USE_INT64
  2455. uint128_t remainder;
  2456. uint128_t quotient;
  2457. value1.Modulus(value2, quotient, remainder);
  2458. return remainder;
  2459. }
  2460. uint128_t& uint128_t::operator+=(const uint128_t& value)
  2461. {
  2462. // OK for EA_INT128_USE_INT64
  2463. operatorPlus(*this, value, *this);
  2464. return *this;
  2465. }
  2466. uint128_t& uint128_t::operator-=(const uint128_t& value)
  2467. {
  2468. // OK for EA_INT128_USE_INT64
  2469. operatorMinus(*this, value, *this);
  2470. return *this;
  2471. }
  2472. uint128_t& uint128_t::operator*=(const uint128_t& value)
  2473. {
  2474. // OK for EA_INT128_USE_INT64
  2475. *this = *this * value;
  2476. return *this;
  2477. }
  2478. uint128_t& uint128_t::operator/=(const uint128_t& value)
  2479. {
  2480. // OK for EA_INT128_USE_INT64
  2481. *this = *this / value;
  2482. return *this;
  2483. }
  2484. uint128_t& uint128_t::operator%=(const uint128_t& value)
  2485. {
  2486. // OK for EA_INT128_USE_INT64
  2487. *this = *this % value;
  2488. return *this;
  2489. }
  2490. // With rightward shifts of negative numbers, shift in zero from the left side.
  2491. uint128_t uint128_t::operator>>(int nShift) const
  2492. {
  2493. // OK for EA_INT128_USE_INT64
  2494. uint128_t temp;
  2495. operatorShiftRight(*this, nShift, temp);
  2496. return temp;
  2497. }
  2498. // With rightward shifts of negative numbers, shift in zero from the left side.
  2499. uint128_t uint128_t::operator<<(int nShift) const
  2500. {
  2501. // OK for EA_INT128_USE_INT64
  2502. uint128_t temp;
  2503. operatorShiftLeft(*this, nShift, temp);
  2504. return temp;
  2505. }
  2506. uint128_t& uint128_t::operator>>=(int nShift)
  2507. {
  2508. // OK for EA_INT128_USE_INT64
  2509. uint128_t temp;
  2510. operatorShiftRight(*this, nShift, temp);
  2511. *this = temp;
  2512. return *this;
  2513. }
  2514. uint128_t& uint128_t::operator<<=(int nShift)
  2515. {
  2516. // OK for EA_INT128_USE_INT64
  2517. uint128_t temp;
  2518. operatorShiftLeft(*this, nShift, temp);
  2519. *this = temp;
  2520. return *this;
  2521. }
  2522. uint128_t operator^(const uint128_t& value1, const uint128_t& value2)
  2523. {
  2524. // OK for EA_INT128_USE_INT64
  2525. uint128_t temp;
  2526. uint128_t::operatorXOR(value1, value2, temp);
  2527. return temp;
  2528. }
  2529. uint128_t operator|(const uint128_t& value1, const uint128_t& value2)
  2530. {
  2531. // OK for EA_INT128_USE_INT64
  2532. uint128_t temp;
  2533. uint128_t::operatorOR(value1, value2, temp);
  2534. return temp;
  2535. }
  2536. uint128_t operator&(const uint128_t& value1, const uint128_t& value2)
  2537. {
  2538. // OK for EA_INT128_USE_INT64
  2539. uint128_t temp;
  2540. uint128_t::operatorAND(value1, value2, temp);
  2541. return temp;
  2542. }
  2543. uint128_t& uint128_t::operator^=(const uint128_t& value)
  2544. {
  2545. // OK for EA_INT128_USE_INT64
  2546. operatorXOR(*this, value, *this);
  2547. return *this;
  2548. }
  2549. uint128_t& uint128_t::operator|=(const uint128_t& value)
  2550. {
  2551. // EA_INT128_USE_INT64
  2552. operatorOR(*this, value, *this);
  2553. return *this;
  2554. }
  2555. uint128_t& uint128_t::operator&=(const uint128_t& value)
  2556. {
  2557. // OK for EA_INT128_USE_INT64
  2558. operatorAND(*this, value, *this);
  2559. return *this;
  2560. }
  2561. // This function forms the basis of all logical comparison functions.
  2562. // If value1 < value2, the return value is -1.
  2563. // If value1 == value2, the return value is 0.
  2564. // If value1 > value2, the return value is 1.
  2565. int compare(const uint128_t& value1, const uint128_t& value2)
  2566. {
  2567. // Compare individual parts. At this point, the two numbers have the same sign.
  2568. #if EA_INT128_USE_INT64
  2569. if(value1.mPart1 == value2.mPart1)
  2570. {
  2571. if(value1.mPart0 == value2.mPart0)
  2572. return 0;
  2573. else if(value1.mPart0 > value2.mPart0)
  2574. return 1;
  2575. // return -1; //Just fall through to the end.
  2576. }
  2577. else if(value1.mPart1 > value2.mPart1)
  2578. return 1;
  2579. return -1;
  2580. #else
  2581. if(value1.mPart3 == value2.mPart3)
  2582. {
  2583. if(value1.mPart2 == value2.mPart2)
  2584. {
  2585. if(value1.mPart1 == value2.mPart1)
  2586. {
  2587. if(value1.mPart0 == value2.mPart0)
  2588. return 0;
  2589. else if(value1.mPart0 > value2.mPart0)
  2590. return 1;
  2591. // return -1; //Just fall through to the end.
  2592. }
  2593. else if(value1.mPart1 > value2.mPart1)
  2594. return 1;
  2595. // return -1; //Just fall through to the end.
  2596. }
  2597. else if(value1.mPart2 > value2.mPart2)
  2598. return 1;
  2599. // return -1; //Just fall through to the end.
  2600. }
  2601. else if(value1.mPart3 > value2.mPart3)
  2602. return 1;
  2603. return -1;
  2604. #endif
  2605. }
  2606. bool operator==(const uint128_t& value1, const uint128_t& value2)
  2607. {
  2608. #if EA_INT128_USE_INT64
  2609. return (value1.mPart0 == value2.mPart0) && // Check mPart0 first as this will likely yield faster execution.
  2610. (value1.mPart1 == value2.mPart1);
  2611. #else
  2612. return (value1.mPart0 == value2.mPart0) && // Check mPart0 first as this will likely yield faster execution.
  2613. (value1.mPart1 == value2.mPart1) &&
  2614. (value1.mPart2 == value2.mPart2) &&
  2615. (value1.mPart3 == value2.mPart3);
  2616. #endif
  2617. }
  2618. bool operator!=(const uint128_t& value1, const uint128_t& value2)
  2619. {
  2620. #if EA_INT128_USE_INT64
  2621. return (value1.mPart0 != value2.mPart0) || // Check mPart0 first as this will likely yield faster execution.
  2622. (value1.mPart1 != value2.mPart1);
  2623. #else
  2624. return (value1.mPart0 != value2.mPart0) || // Check mPart0 first as this will likely yield faster execution.
  2625. (value1.mPart1 != value2.mPart1) ||
  2626. (value1.mPart2 != value2.mPart2) ||
  2627. (value1.mPart3 != value2.mPart3);
  2628. #endif
  2629. }
  2630. bool operator>(const uint128_t& value1, const uint128_t& value2)
  2631. {
  2632. // OK for EA_INT128_USE_INT64
  2633. return (compare(value1, value2) > 0);
  2634. }
  2635. bool operator>=(const uint128_t& value1, const uint128_t& value2)
  2636. {
  2637. // OK for EA_INT128_USE_INT64
  2638. return (compare(value1, value2) >= 0);
  2639. }
  2640. bool operator<(const uint128_t& value1, const uint128_t& value2)
  2641. {
  2642. // OK for EA_INT128_USE_INT64
  2643. return (compare(value1, value2) < 0);
  2644. }
  2645. bool operator<=(const uint128_t& value1, const uint128_t& value2)
  2646. {
  2647. // OK for EA_INT128_USE_INT64
  2648. return (compare(value1, value2) <= 0);
  2649. }
  2650. int8_t uint128_t::AsInt8() const
  2651. {
  2652. // OK for EA_INT128_USE_INT64
  2653. // The C++ Standard, section 4.7, paragraph 3 states that the results of
  2654. // conversion of an unsigned type to a signed type that cannot represent
  2655. // the unsigned type are implementation-defined.
  2656. return (int8_t)mPart0;
  2657. }
  2658. int16_t uint128_t::AsInt16() const
  2659. {
  2660. // OK for EA_INT128_USE_INT64
  2661. // The C++ Standard, section 4.7, paragraph 3 states that the results of
  2662. // conversion of an unsigned type to a signed type that cannot represent
  2663. // the unsigned type are implementation-defined.
  2664. return (int16_t)mPart0;
  2665. }
  2666. int32_t uint128_t::AsInt32() const
  2667. {
  2668. // OK for EA_INT128_USE_INT64
  2669. // The C++ Standard, section 4.7, paragraph 3 states that the results of
  2670. // conversion of an unsigned type to a signed type that cannot represent
  2671. // the unsigned type are implementation-defined.
  2672. return (int32_t)mPart0;
  2673. }
  2674. int64_t uint128_t::AsInt64() const
  2675. {
  2676. #if EA_INT128_USE_INT64
  2677. return (int64_t)mPart0;
  2678. #else
  2679. return (((int64_t) mPart1) << 32) + mPart0;
  2680. #endif
  2681. }
  2682. // I am not convinced that this is a reliable method of conversion.
  2683. float uint128_t::AsFloat() const
  2684. {
  2685. float fReturnValue(0);
  2686. #if EA_INT128_USE_INT64
  2687. if(mPart1)
  2688. fReturnValue += (mPart1 * 18446744073709551616.f);
  2689. if(mPart0)
  2690. fReturnValue += (float)mPart0;
  2691. #else
  2692. if(mPart3)
  2693. fReturnValue += (mPart3 * 79228162514264337593543950336.f);
  2694. if(mPart2)
  2695. fReturnValue += (mPart2 * 18446744073709551616.f);
  2696. if(mPart1)
  2697. fReturnValue += (mPart1 * 4294967296.f);
  2698. if(mPart0)
  2699. fReturnValue += (float)mPart0;
  2700. #endif
  2701. return fReturnValue;
  2702. }
  2703. // I am not convinced that this is a reliable method of conversion.
  2704. double uint128_t::AsDouble() const
  2705. {
  2706. double fReturnValue(0);
  2707. #if EA_INT128_USE_INT64
  2708. if(mPart1)
  2709. fReturnValue += (mPart1 * 18446744073709551616.0);
  2710. if(mPart0)
  2711. fReturnValue += (double)mPart0;
  2712. #else
  2713. if(mPart3)
  2714. fReturnValue += (mPart3 * 79228162514264337593543950336.0);
  2715. if(mPart2)
  2716. fReturnValue += (mPart2 * 18446744073709551616.0);
  2717. if(mPart1)
  2718. fReturnValue += (mPart1 * 4294967296.0);
  2719. if(mPart0)
  2720. fReturnValue += (double)mPart0;
  2721. #endif
  2722. return fReturnValue;
  2723. }
  2724. void uint128_t::Negate()
  2725. {
  2726. // OK for EA_INT128_USE_INT64
  2727. TwosComplement();
  2728. }
  2729. bool uint128_t::IsNegative() const
  2730. { // True if value < 0
  2731. // OK for EA_INT128_USE_INT64
  2732. return false;
  2733. }
  2734. bool uint128_t::IsPositive() const
  2735. {
  2736. // True of value >= 0
  2737. // OK for EA_INT128_USE_INT64
  2738. return true;
  2739. }
  2740. ///////////////////////////////////////////////////////////////////////////////
  2741. // Modulus
  2742. //
  2743. // This is a generic function that does both division modulus calculations.
  2744. //
  2745. void uint128_t::Modulus(const uint128_t& divisor, uint128_t& quotient, uint128_t& remainder) const
  2746. {
  2747. // OK for EA_INT128_USE_INT64
  2748. uint128_t tempDividend(*this);
  2749. uint128_t tempDivisor(divisor);
  2750. if(tempDivisor.IsZero())
  2751. {
  2752. // Force a divide by zero exception.
  2753. // We know that tempDivisor.mPart0 is zero.
  2754. quotient.mPart0 /= tempDivisor.mPart0;
  2755. }
  2756. else if(tempDividend.IsZero())
  2757. {
  2758. quotient = uint128_t((uint32_t)0);
  2759. remainder = uint128_t((uint32_t)0);
  2760. }
  2761. else
  2762. {
  2763. remainder.SetZero();
  2764. for(int i(0); i < 128; i++)
  2765. {
  2766. remainder += (uint32_t)tempDividend.GetBit(127 - i);
  2767. const bool bBit(remainder >= tempDivisor);
  2768. quotient.SetBit(127 - i, bBit);
  2769. if(bBit)
  2770. remainder -= tempDivisor;
  2771. if((i != 127) && !remainder.IsZero())
  2772. remainder <<= 1;
  2773. }
  2774. }
  2775. }
  2776. ///////////////////////////////////////////////////////////////////////////////
  2777. // StrToInt128
  2778. //
  2779. // Same as C runtime strtol function but for uint128_t.
  2780. // This is probably the most general and useful of the C atoi family of functions.
  2781. //
  2782. uint128_t uint128_t::StrToInt128(const char* pValue, char** ppEnd, int nBase)
  2783. {
  2784. uint128_t value((uint32_t)0); // Current value
  2785. const char* p = pValue; // Current position
  2786. const char* pBegin = NULL; // Where the digits start.
  2787. const char* pEnd = NULL; // Where the digits end. One-past the last digit.
  2788. char chSign('+'); // One of either '+' or '-'
  2789. // Skip leading whitespace
  2790. while(isspace((unsigned char)*p))
  2791. ++p;
  2792. // Check for sign.
  2793. if((*p == '-') || (*p == '+'))
  2794. chSign = *p++;
  2795. // Do checks on 'nBase'.
  2796. if((nBase < 0) || (nBase == 1) || (nBase > 36)){
  2797. if(ppEnd)
  2798. *ppEnd = (char*)pValue;
  2799. return value;
  2800. }
  2801. else if(nBase == 0){
  2802. // Auto detect one of base 2, 8, 10, or 16.
  2803. if(*p != '0')
  2804. nBase = 10;
  2805. else if((p[1] == 'x') || (p[1] == 'X'))
  2806. nBase = 16;
  2807. else if((p[1] == 'b') || (p[1] == 'B'))
  2808. nBase = 2;
  2809. else
  2810. nBase = 8;
  2811. }
  2812. if(nBase == 16){
  2813. // If there is a leading '0x', then skip past it.
  2814. if((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))
  2815. p += 2;
  2816. }
  2817. else if(nBase == 2){
  2818. // If there is a leading '0b', then skip past it.
  2819. if((*p == '0') && ((p[1] == 'b') || (p[1] == 'B')))
  2820. p += 2;
  2821. }
  2822. // Save the position where the digits start.
  2823. pBegin = p;
  2824. if(nBase == 2) // Binary
  2825. {
  2826. while((*p == '0') || (*p == '1'))
  2827. p++;
  2828. pEnd = p;
  2829. if(pEnd > pBegin + 128) // There can be at most 128 binary digits in the string.
  2830. {
  2831. pEnd = pBegin + 128;
  2832. p = pEnd;
  2833. }
  2834. for(int i(0); p > pBegin; ++i)
  2835. {
  2836. --p;
  2837. if(*p == '1')
  2838. value.SetBit(i, true);
  2839. }
  2840. }
  2841. else if(nBase == 10) // Decimal
  2842. {
  2843. while(isdigit((unsigned char)*p))
  2844. ++p;
  2845. pEnd = p;
  2846. if(pEnd > pBegin + 39) // With base 10, it is not enough to simply check against 39 digits,
  2847. { // as you can have 39 '9's and overflow. But 39 is the most you could have.
  2848. pEnd = pBegin + 39;
  2849. p = pEnd;
  2850. }
  2851. uint128_t multiplier((uint32_t)1);
  2852. for(int i(0); p > pBegin; ++i)
  2853. {
  2854. const uint32_t c = *(--p) - (uint32_t)'0';
  2855. if(c)
  2856. {
  2857. // This can be optimized for faster speed by doing the smaller orders
  2858. // of ten on value.mPart0 with an int multiplier instead of on value
  2859. // and a uint128_t multiplier.
  2860. value += (multiplier * c);
  2861. }
  2862. multiplier *= (uint32_t)10;
  2863. }
  2864. }
  2865. else if(nBase == 16) // Hexadecimal
  2866. {
  2867. while(isxdigit((unsigned char)*p))
  2868. p++;
  2869. pEnd = p;
  2870. if(pEnd > pBegin + 32) // There can be at most 32 hexadecimal digits in the string.
  2871. {
  2872. pEnd = pBegin + 32;
  2873. p = pEnd;
  2874. }
  2875. // There can be as many as 32 characters.
  2876. for(int i(0); p > pBegin; i++)
  2877. {
  2878. #if EA_INT128_USE_INT64
  2879. const int nPart = (int)((pEnd - p) / 16);
  2880. uint64_t c = *(--p);
  2881. #else
  2882. const int nPart = (int)((pEnd - p) / 8);
  2883. uint32_t c = *(--p);
  2884. #endif
  2885. if(c >= '0' && c <= '9')
  2886. c = (c - '0');
  2887. else if(c >= 'a' && c <= 'f')
  2888. c = 10 + (c - 'a');
  2889. else
  2890. c = 10 + (c - 'A');
  2891. if(c)
  2892. {
  2893. #if EA_INT128_USE_INT64
  2894. c <<= ((i % 16) * 4);
  2895. if(nPart == 0)
  2896. value.mPart0 |= c;
  2897. else if(nPart == 1)
  2898. value.mPart1 |= c;
  2899. #else
  2900. c <<= ((i % 8) * 4);
  2901. if(nPart == 0)
  2902. value.mPart0 |= c;
  2903. else if(nPart == 1)
  2904. value.mPart1 |= c;
  2905. else if(nPart == 2)
  2906. value.mPart2 |= c;
  2907. else if(nPart == 3)
  2908. value.mPart3 |= c;
  2909. #endif
  2910. }
  2911. }
  2912. }
  2913. else
  2914. {
  2915. // EA_ASSERT(false); // For the time being, we handle only the above bases.
  2916. }
  2917. if(chSign == '-')
  2918. value.Negate();
  2919. if(ppEnd)
  2920. *ppEnd = (char*)pEnd;
  2921. return value;
  2922. }
  2923. ///////////////////////////////////////////////////////////////////////////////
  2924. // StrToInt128
  2925. //
  2926. // Same as C runtime strtol function but for uint128_t.
  2927. // This is probably the most general and useful of the C atoi family of functions.
  2928. //
  2929. uint128_t uint128_t::StrToInt128(const wchar_t* pValue, wchar_t** ppEnd, int nBase)
  2930. {
  2931. // This is simply a copy and paste of the char version of StrToInt128, with minor
  2932. // modifications for wchar_t.
  2933. uint128_t value((uint32_t)0); // Current value
  2934. const wchar_t* p = pValue; // Current position
  2935. const wchar_t* pBegin = NULL; // Where the digits start.
  2936. const wchar_t* pEnd = NULL; // Where the digits end. One-past the last digit.
  2937. wchar_t chSign('+'); // One of either '+' or '-'
  2938. // Skip leading whitespace
  2939. while((*p > 0) && (*p < 127) && isspace((uint8_t)*p)) // Compare to < 127 because ctype functions will crash for higher values.
  2940. ++p;
  2941. // Check for sign.
  2942. if((*p == '-') || (*p == '+'))
  2943. chSign = *p++;
  2944. // Do checks on 'nBase'.
  2945. if((nBase < 0) || (nBase == 1) || (nBase > 36)){
  2946. if(ppEnd)
  2947. *ppEnd = (wchar_t*)pValue;
  2948. return value;
  2949. }
  2950. else if(nBase == 0){
  2951. // Auto detect one of base 2, 8, 10, or 16.
  2952. if(*p != '0')
  2953. nBase = 10;
  2954. else if((p[1] == 'x') || (p[1] == 'X'))
  2955. nBase = 16;
  2956. else if((p[1] == 'b') || (p[1] == 'B'))
  2957. nBase = 2;
  2958. else
  2959. nBase = 8;
  2960. }
  2961. if(nBase == 16){
  2962. // If there is a leading '0x', then skip past it.
  2963. if((*p == '0') && ((p[1] == 'x') || (p[1] == 'X')))
  2964. p += 2;
  2965. }
  2966. else if(nBase == 2){
  2967. // If there is a leading '0b', then skip past it.
  2968. if((*p == '0') && ((p[1] == 'b') || (p[1] == 'B')))
  2969. p += 2;
  2970. }
  2971. // Save the position where the digits start.
  2972. pBegin = p;
  2973. if(nBase == 2) // Binary
  2974. {
  2975. while((*p == '0') || (*p == '1'))
  2976. p++;
  2977. pEnd = p;
  2978. if(pEnd > pBegin + 128) // There can be at most 128 binary digits in the string.
  2979. {
  2980. pEnd = pBegin + 128;
  2981. p = pEnd;
  2982. }
  2983. for(int i(0); p > pBegin; ++i)
  2984. {
  2985. --p;
  2986. if(*p == '1')
  2987. value.SetBit(i, true);
  2988. }
  2989. }
  2990. else if(nBase == 10) // Decimal
  2991. {
  2992. while((*p > 0) && (*p < 127) && isdigit((uint8_t)*p)) // Compare to < 127 because ctype functions will crash for higher values.
  2993. ++p;
  2994. pEnd = p;
  2995. if(pEnd > pBegin + 39) // With base 10, it is not enough to simply check against 39 digits,
  2996. { // as you can have 39 '9's and overflow. But 39 is the most you could have.
  2997. pEnd = pBegin + 39;
  2998. p = pEnd;
  2999. }
  3000. uint128_t multiplier((uint32_t)1);
  3001. for(int i(0); p > pBegin; ++i)
  3002. {
  3003. const uint32_t c = *(--p) - (uint32_t)'0';
  3004. if(c)
  3005. {
  3006. // This can be optimized for faster speed by doing the smaller orders
  3007. // of ten on value.mPart0 with an int multiplier instead of on value
  3008. // and a uint128_t multiplier.
  3009. value += (multiplier * c);
  3010. }
  3011. multiplier *= (uint32_t)10;
  3012. }
  3013. }
  3014. else if(nBase == 16) // Hexadecimal
  3015. {
  3016. while((*p > 0) && (*p < 127) && isxdigit((uint8_t)*p)) // Compare to < 127 because ctype functions will crash for higher values.
  3017. p++;
  3018. pEnd = p;
  3019. if(pEnd > pBegin + 32) // There can be at most 32 hexadecimal digits in the string.
  3020. {
  3021. pEnd = pBegin + 32;
  3022. p = pEnd;
  3023. }
  3024. // There can be as many as 32 characters.
  3025. for(int i(0); p > pBegin; i++)
  3026. {
  3027. #if EA_INT128_USE_INT64
  3028. const int nPart = (int)((pEnd - p) / 16);
  3029. uint64_t c = *(--p);
  3030. #else
  3031. const int nPart = (int)((pEnd - p) / 8);
  3032. uint32_t c = *(--p);
  3033. #endif
  3034. if(c >= '0' && c <= '9')
  3035. c = (c - '0');
  3036. else if(c >= 'a' && c <= 'f')
  3037. c = 10 + (c - 'a');
  3038. else
  3039. c = 10 + (c - 'A');
  3040. if(c)
  3041. {
  3042. #if EA_INT128_USE_INT64
  3043. c <<= ((i % 16) * 4);
  3044. if(nPart == 0)
  3045. value.mPart0 |= c;
  3046. else if(nPart == 1)
  3047. value.mPart1 |= c;
  3048. #else
  3049. c <<= ((i % 8) * 4);
  3050. if(nPart == 0)
  3051. value.mPart0 |= c;
  3052. else if(nPart == 1)
  3053. value.mPart1 |= c;
  3054. else if(nPart == 2)
  3055. value.mPart2 |= c;
  3056. else if(nPart == 3)
  3057. value.mPart3 |= c;
  3058. #endif
  3059. }
  3060. }
  3061. }
  3062. else
  3063. {
  3064. // EA_ASSERT(false); // For the time being, we handle only the above bases.
  3065. }
  3066. if(chSign == '-')
  3067. value.Negate();
  3068. if(ppEnd)
  3069. *ppEnd = (wchar_t*)pEnd;
  3070. return value;
  3071. }
  3072. ///////////////////////////////////////////////////////////////////////////////
  3073. // Int128ToStr
  3074. //
  3075. // Returned string has a NULL appended to it.
  3076. // Upon return, ppEnd points to the terminating NULL.
  3077. // Thus, ppEnd - pValue => string length.
  3078. //
  3079. // bPrefix applies only to base 2 (0b) and base 16 (0x).
  3080. //
  3081. void uint128_t::Int128ToStr(char* pValue, char** ppEnd, int nBase, LeadingZeroes lz, Prefix prefix) const
  3082. {
  3083. if(nBase == 2)
  3084. {
  3085. bool bLeadingZeros = (lz == kLZEnable); // By default leading zeroes are disabled.
  3086. bool bPrefix = (prefix == kPrefixEnable); // By default prefix is disabled.
  3087. if(bPrefix)
  3088. {
  3089. *pValue++ = '0';
  3090. *pValue++ = 'b';
  3091. }
  3092. if(IsZero())
  3093. {
  3094. if(bLeadingZeros)
  3095. {
  3096. for(int i(0); i < 128; i++)
  3097. *pValue++ = '0';
  3098. }
  3099. else
  3100. *pValue++ = '0'; // This is all we need to write.
  3101. }
  3102. else
  3103. {
  3104. // Print out the text.
  3105. bool bNonZeroFound(false);
  3106. for(int i(127); i >= 0; --i)
  3107. {
  3108. const int bBitIsSet(GetBit(i));
  3109. if(bBitIsSet)
  3110. bNonZeroFound = true;
  3111. if(bLeadingZeros || bNonZeroFound)
  3112. *pValue++ = (bBitIsSet ? '1' : '0');
  3113. }
  3114. }
  3115. }
  3116. else if(nBase == 10)
  3117. {
  3118. // To do: Support leading zeroes and prefix for base 10.
  3119. uint128_t value(*this);
  3120. char* pValueInitial = pValue;
  3121. // This part here isn't particularly fast.
  3122. const uint128_t ten((uint32_t)10);
  3123. while (value >= ten)
  3124. {
  3125. const uint128_t remainder = (value % ten);
  3126. *pValue++ = (char)('0' + remainder.mPart0);
  3127. value /= (uint32_t)10;
  3128. }
  3129. *pValue++ = (char)('0' + value.mPart0);
  3130. // Reverse the string.
  3131. char* pEnd = pValue - 1;
  3132. while(pValueInitial < pEnd)
  3133. {
  3134. char temp = *pValueInitial;
  3135. *pValueInitial = *pEnd;
  3136. *pEnd = temp;
  3137. ++pValueInitial;
  3138. --pEnd;
  3139. }
  3140. }
  3141. else if(nBase == 16)
  3142. {
  3143. bool bLeadingZeros = (lz != kLZDisable); // By default leading zeroes are enabled.
  3144. bool bPrefix = (prefix != kPrefixDisable); // By default prefix is enabled.
  3145. static const char* const pHexCharTable = "0123456789abcdef";
  3146. if(bPrefix)
  3147. {
  3148. *pValue++ = '0';
  3149. *pValue++ = 'x';
  3150. }
  3151. if(IsZero())
  3152. {
  3153. if(bLeadingZeros)
  3154. {
  3155. for(int i(0); i < 32; i++) // 32 is equal to (128 / 16)
  3156. *pValue++ = '0';
  3157. }
  3158. else
  3159. *pValue++ = '0'; // This is all we need to write.
  3160. }
  3161. else
  3162. {
  3163. // Print out the text.
  3164. bool bNonZeroFound(false);
  3165. // Work on each part in turn, starting with the high part.
  3166. #if EA_INT128_USE_INT64
  3167. for(int i(1); i >= 0; --i)
  3168. {
  3169. const uint64_t* pCurrent;
  3170. if(i == 1)
  3171. pCurrent = &mPart1;
  3172. else
  3173. pCurrent = &mPart0;
  3174. // Work on each sub-part (4 bits) or the current part (64 bits), starting with the high sub-part.
  3175. for(int j(60); j >= 0; j -= 4)
  3176. {
  3177. const char c = pHexCharTable[(*pCurrent >> j) & 0x0F];
  3178. if(c != '0')
  3179. bNonZeroFound = true;
  3180. if(bLeadingZeros || bNonZeroFound)
  3181. *pValue++ = c;
  3182. }
  3183. }
  3184. #else
  3185. for(int i(3); i >= 0; --i)
  3186. {
  3187. const uint32_t* pCurrent;
  3188. if(i == 3)
  3189. pCurrent = &mPart3;
  3190. else if(i == 2)
  3191. pCurrent = &mPart2;
  3192. else if(i == 1)
  3193. pCurrent = &mPart1;
  3194. else
  3195. pCurrent = &mPart0;
  3196. // Work on each sub-part (4 bits) or the current part (32 bits), starting with the high sub-part.
  3197. for(int j(28); j >= 0; j -= 4)
  3198. {
  3199. const char c = pHexCharTable[(*pCurrent >> j) & 0x0F];
  3200. if(c != '0')
  3201. bNonZeroFound = true;
  3202. if(bLeadingZeros || bNonZeroFound)
  3203. *pValue++ = c;
  3204. }
  3205. }
  3206. #endif
  3207. }
  3208. }
  3209. else
  3210. {
  3211. // To do: Implement this in a generic way.
  3212. EA_FAIL(); // Base not supported.
  3213. }
  3214. if(ppEnd)
  3215. *ppEnd = pValue;
  3216. *pValue++ = 0;
  3217. }
  3218. void uint128_t::Int128ToStr(wchar_t* pValue, wchar_t** ppEnd, int nBase, LeadingZeroes lz, Prefix prefix) const
  3219. {
  3220. char str8[130];
  3221. char* pEnd = str8;
  3222. Int128ToStr(str8, &pEnd, nBase, lz, prefix);
  3223. for(char* p = str8; p < pEnd;)
  3224. *pValue++ = (wchar_t)(uint8_t)*p++;
  3225. if(ppEnd)
  3226. *ppEnd = pValue;
  3227. *pValue = 0;
  3228. }
  3229. } // namespace StdC
  3230. } // namespace EA
  3231. #ifdef _MSC_VER
  3232. #pragma warning(pop)
  3233. #endif