floff.hpp 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044
  1. // Copyright 2020-2022 Junekey Jeon
  2. //
  3. // The contents of this file may be used under the terms of
  4. // the Apache License v2.0 with LLVM Exceptions.
  5. //
  6. // (See accompanying file LICENSE-Apache or copy at
  7. // https://llvm.org/foundation/relicensing/LICENSE.txt)
  8. //
  9. // Alternatively, the contents of this file may be used under the terms of
  10. // the Boost Software License, Version 1.0.
  11. // (See accompanying file LICENSE-Boost or copy at
  12. // https://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. // Unless required by applicable law or agreed to in writing, this software
  15. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. // KIND, either express or implied.
  17. //
  18. // Some parts are copied from Dragonbox project.
  19. //
  20. // Copyright 2023 Matt Borland
  21. // Distributed under the Boost Software License, Version 1.0.
  22. // https://www.boost.org/LICENSE_1_0.txt
  23. #ifndef BOOST_CHARCONV_DETAIL_FLOFF
  24. #define BOOST_CHARCONV_DETAIL_FLOFF
  25. #include <boost/charconv/detail/config.hpp>
  26. #include <boost/charconv/detail/bit_layouts.hpp>
  27. #include <boost/charconv/detail/emulated128.hpp>
  28. #include <boost/charconv/detail/dragonbox/dragonbox_common.hpp>
  29. #include <boost/charconv/detail/to_chars_result.hpp>
  30. #include <boost/charconv/chars_format.hpp>
  31. #include <boost/core/bit.hpp>
  32. #include <type_traits>
  33. #include <limits>
  34. #include <cstdint>
  35. #include <cstring>
  36. #include <cstddef>
  37. #include <climits>
  38. #ifdef BOOST_MSVC
  39. # pragma warning(push)
  40. # pragma warning(disable: 4127) // Extensive use of BOOST_IF_CONSTEXPR emits warnings under C++11 and 14
  41. # pragma warning(disable: 4554) // parentheses are used be warning is still emitted
  42. #endif
  43. namespace boost { namespace charconv { namespace detail {
  44. #ifdef BOOST_MSVC
  45. # pragma warning(push)
  46. # pragma warning(disable: 4702) // use of BOOST_IF_CONSTEXPR can result in unreachable code if max_blocks is 3
  47. // Other older compilers will emit warnings if the unreachable code is wrapped
  48. // in an else block (e.g. no return statment)
  49. #endif
  50. template <std::size_t max_blocks>
  51. struct fixed_point_calculator
  52. {
  53. static_assert(1 < max_blocks, "Max blocks must be greater than 1");
  54. // Multiply multiplier to the fractional blocks and take the resulting integer part.
  55. // The fractional blocks are updated.
  56. template <typename MultiplierType>
  57. BOOST_FORCEINLINE static MultiplierType generate(MultiplierType multiplier,
  58. std::uint64_t* blocks_ptr,
  59. std::size_t number_of_blocks) noexcept
  60. {
  61. BOOST_CHARCONV_ASSERT(0 < number_of_blocks && number_of_blocks <= max_blocks);
  62. BOOST_IF_CONSTEXPR (max_blocks == 3)
  63. {
  64. uint128 mul_result;
  65. std::uint64_t carry = 0;
  66. switch (number_of_blocks)
  67. {
  68. case 3:
  69. mul_result = umul128(blocks_ptr[2], multiplier);
  70. blocks_ptr[2] = mul_result.low;
  71. carry = mul_result.high;
  72. BOOST_FALLTHROUGH;
  73. case 2:
  74. mul_result = umul128(blocks_ptr[1], multiplier);
  75. mul_result += carry;
  76. blocks_ptr[1] = mul_result.low;
  77. carry = mul_result.high;
  78. BOOST_FALLTHROUGH;
  79. case 1:
  80. mul_result = umul128(blocks_ptr[0], multiplier);
  81. mul_result += carry;
  82. blocks_ptr[0] = mul_result.low;
  83. return mul_result.high;
  84. default:
  85. BOOST_UNREACHABLE_RETURN(carry); // NOLINT : Macro for unreachable can expand to be empty
  86. }
  87. }
  88. auto mul_result = umul128(blocks_ptr[number_of_blocks - 1], multiplier);
  89. blocks_ptr[number_of_blocks - 1] = mul_result.low;
  90. auto carry = mul_result.high;
  91. for (std::size_t i = 1; i < number_of_blocks; ++i)
  92. {
  93. mul_result = umul128(blocks_ptr[number_of_blocks - i - 1], multiplier);
  94. mul_result += carry;
  95. blocks_ptr[number_of_blocks - i - 1] = mul_result.low;
  96. carry = mul_result.high;
  97. }
  98. return MultiplierType(carry);
  99. }
  100. // Multiply multiplier to the fractional blocks and discard the resulting integer part.
  101. // The fractional blocks are updated.
  102. template <typename MultiplierType>
  103. BOOST_FORCEINLINE static void discard_upper(MultiplierType multiplier,
  104. std::uint64_t* blocks_ptr,
  105. std::size_t number_of_blocks) noexcept
  106. {
  107. BOOST_CHARCONV_ASSERT(0 < number_of_blocks && number_of_blocks <= max_blocks);
  108. blocks_ptr[0] *= multiplier;
  109. if (number_of_blocks > 1)
  110. {
  111. BOOST_IF_CONSTEXPR (max_blocks == 3)
  112. {
  113. uint128 mul_result;
  114. std::uint64_t carry = 0;
  115. if (number_of_blocks > 2)
  116. {
  117. mul_result = umul128(multiplier, blocks_ptr[2]);
  118. blocks_ptr[2] = mul_result.low;
  119. carry = mul_result.high;
  120. }
  121. mul_result = umul128(multiplier, blocks_ptr[1]);
  122. mul_result += carry;
  123. blocks_ptr[1] = mul_result.low;
  124. blocks_ptr[0] += mul_result.high;
  125. }
  126. else
  127. {
  128. auto mul_result = umul128(multiplier, blocks_ptr[number_of_blocks - 1]);
  129. blocks_ptr[number_of_blocks - 1] = mul_result.low;
  130. auto carry = mul_result.high;
  131. for (std::size_t i = 2; i < number_of_blocks; ++i)
  132. {
  133. mul_result = umul128(multiplier, blocks_ptr[number_of_blocks - i]);
  134. mul_result += carry;
  135. blocks_ptr[number_of_blocks - i] = mul_result.low;
  136. carry = mul_result.high;
  137. }
  138. blocks_ptr[0] += carry;
  139. }
  140. }
  141. }
  142. // Multiply multiplier to the fractional blocks and take the resulting integer part.
  143. // Don't care about what happens to the fractional blocks.
  144. template <typename MultiplierType>
  145. BOOST_FORCEINLINE static MultiplierType
  146. generate_and_discard_lower(MultiplierType multiplier, std::uint64_t* blocks_ptr,
  147. std::size_t number_of_blocks) noexcept
  148. {
  149. BOOST_CHARCONV_ASSERT(0 < number_of_blocks && number_of_blocks <= max_blocks);
  150. BOOST_IF_CONSTEXPR (max_blocks == 3)
  151. {
  152. uint128 mul_result;
  153. std::uint64_t carry = 0;
  154. switch (number_of_blocks)
  155. {
  156. case 3:
  157. mul_result = umul128(blocks_ptr[2], static_cast<std::uint64_t>(multiplier));
  158. carry = mul_result.high;
  159. BOOST_FALLTHROUGH;
  160. case 2:
  161. mul_result = umul128(blocks_ptr[1], static_cast<std::uint64_t>(multiplier));
  162. mul_result += carry;
  163. carry = mul_result.high;
  164. BOOST_FALLTHROUGH;
  165. case 1:
  166. mul_result = umul128(blocks_ptr[0], static_cast<std::uint64_t>(multiplier));
  167. mul_result += carry;
  168. return static_cast<MultiplierType>(mul_result.high);
  169. default:
  170. BOOST_UNREACHABLE_RETURN(carry); // NOLINT
  171. }
  172. }
  173. auto mul_result = umul128(blocks_ptr[number_of_blocks - 1], static_cast<std::uint64_t>(multiplier));
  174. auto carry = mul_result.high;
  175. for (std::size_t i = 1; i < number_of_blocks; ++i)
  176. {
  177. mul_result = umul128(blocks_ptr[number_of_blocks - i - 1], static_cast<std::uint64_t>(multiplier));
  178. mul_result += carry;
  179. carry = mul_result.high;
  180. }
  181. return static_cast<MultiplierType>(carry);
  182. }
  183. };
  184. #ifdef BOOST_MSVC
  185. # pragma warning(pop)
  186. #endif
  187. template <bool b>
  188. struct additional_static_data_holder_impl
  189. {
  190. static constexpr char radix_100_table[] = {
  191. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', //
  192. '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', //
  193. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', //
  194. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', //
  195. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', //
  196. '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', //
  197. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', //
  198. '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', //
  199. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', //
  200. '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', //
  201. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', //
  202. '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', //
  203. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', //
  204. '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', //
  205. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', //
  206. '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', //
  207. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', //
  208. '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', //
  209. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', //
  210. '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' //
  211. };
  212. static constexpr std::uint32_t fractional_part_rounding_thresholds32[] = {
  213. UINT32_C(2576980378), UINT32_C(2190433321), UINT32_C(2151778616), UINT32_C(2147913145),
  214. UINT32_C(2147526598), UINT32_C(2147487943), UINT32_C(2147484078), UINT32_C(2147483691)
  215. };
  216. static constexpr std::uint64_t fractional_part_rounding_thresholds64[] = {
  217. UINT64_C(11068046444225730970), UINT64_C(9407839477591871325), UINT64_C(9241818780928485360),
  218. UINT64_C(9225216711262146764), UINT64_C(9223556504295512904), UINT64_C(9223390483598849518),
  219. UINT64_C(9223373881529183179), UINT64_C(9223372221322216546), UINT64_C(9223372055301519882),
  220. UINT64_C(9223372038699450216), UINT64_C(9223372037039243249), UINT64_C(9223372036873222553),
  221. UINT64_C(9223372036856620483), UINT64_C(9223372036854960276), UINT64_C(9223372036854794255),
  222. UINT64_C(9223372036854777653), UINT64_C(9223372036854775993), UINT64_C(9223372036854775827)
  223. };
  224. };
  225. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES) && (!defined(BOOST_MSVC) || BOOST_MSVC != 1900)
  226. template <bool b> constexpr char additional_static_data_holder_impl<b>::radix_100_table[];
  227. template <bool b> constexpr std::uint32_t additional_static_data_holder_impl<b>::fractional_part_rounding_thresholds32[];
  228. template <bool b> constexpr std::uint64_t additional_static_data_holder_impl<b>::fractional_part_rounding_thresholds64[];
  229. #endif
  230. using additional_static_data_holder = additional_static_data_holder_impl<true>;
  231. struct compute_mul_result
  232. {
  233. std::uint64_t result;
  234. bool is_integer;
  235. };
  236. // Load the necessary bits into blocks_ptr and then return the number of cache blocks
  237. // loaded. The most significant block is loaded into blocks_ptr[0].
  238. template <typename ExtendedCache, bool zero_out,
  239. typename CacheBlockType = typename std::decay<decltype(ExtendedCache::cache[0])>::type,
  240. typename std::enable_if<(ExtendedCache::constant_block_count), bool>::type = true>
  241. inline std::uint8_t cache_block_count_helper(CacheBlockType*, int, int, std::uint32_t) noexcept
  242. {
  243. return static_cast<std::uint8_t>(ExtendedCache::max_cache_blocks);
  244. }
  245. template <typename ExtendedCache, bool zero_out,
  246. typename CacheBlockType = typename std::decay<decltype(ExtendedCache::cache[0])>::type,
  247. typename std::enable_if<!(ExtendedCache::constant_block_count), bool>::type = true>
  248. inline std::uint8_t cache_block_count_helper(CacheBlockType*, int e, int, std::uint32_t multiplier_index) noexcept
  249. {
  250. const auto mul_info = ExtendedCache::multiplier_index_info_table[multiplier_index];
  251. const auto cache_block_count_index =
  252. mul_info.cache_block_count_index_offset +
  253. static_cast<std::uint32_t>(e - ExtendedCache::e_min) / ExtendedCache::collapse_factor -
  254. ExtendedCache::cache_block_count_offset_base;
  255. BOOST_IF_CONSTEXPR (ExtendedCache::max_cache_blocks < 3)
  256. {
  257. // 1-bit packing.
  258. return static_cast<std::uint8_t>(
  259. (ExtendedCache::cache_block_counts[cache_block_count_index /
  260. 8] >>
  261. (cache_block_count_index % 8)) &
  262. 0x1) +
  263. 1;
  264. }
  265. else BOOST_IF_CONSTEXPR (ExtendedCache::max_cache_blocks < 4)
  266. {
  267. // 2-bit packing.
  268. return static_cast<std::uint8_t>(
  269. (ExtendedCache::cache_block_counts[cache_block_count_index / 4] >>
  270. (2 * (cache_block_count_index % 4))) &
  271. 0x3);
  272. }
  273. else
  274. {
  275. // 4-bit packing.
  276. return std::uint8_t(
  277. (ExtendedCache::cache_block_counts[cache_block_count_index / 2] >>
  278. (4 * (cache_block_count_index % 2))) &
  279. 0xf);
  280. }
  281. }
  282. template <typename ExtendedCache, bool zero_out,
  283. typename CacheBlockType = typename std::decay<decltype(ExtendedCache::cache[0])>::type>
  284. BOOST_FORCEINLINE std::uint8_t load_extended_cache(CacheBlockType* blocks_ptr, int e, int k,
  285. std::uint32_t multiplier_index) noexcept
  286. {
  287. BOOST_IF_CONSTEXPR (zero_out)
  288. {
  289. std::memset(blocks_ptr, 0, sizeof(CacheBlockType) * ExtendedCache::max_cache_blocks);
  290. }
  291. const auto mul_info = ExtendedCache::multiplier_index_info_table[multiplier_index];
  292. std::uint32_t number_of_leading_zero_blocks;
  293. std::uint32_t first_cache_block_index;
  294. std::uint32_t bit_offset;
  295. std::uint32_t excessive_bits_to_left;
  296. std::uint32_t excessive_bits_to_right;
  297. std::uint8_t cache_block_count = cache_block_count_helper<ExtendedCache, zero_out, CacheBlockType>(blocks_ptr, e, k, multiplier_index);
  298. // The request window starting/ending positions.
  299. auto start_bit_index = static_cast<int>(mul_info.cache_bit_index_offset) + e - ExtendedCache::cache_bit_index_offset_base;
  300. auto end_bit_index = start_bit_index + cache_block_count * static_cast<int>(ExtendedCache::cache_bits_unit);
  301. // The source window starting/ending positions.
  302. const auto src_start_bit_index = static_cast<int>(mul_info.first_cache_bit_index);
  303. const auto src_end_bit_index = static_cast<int>(ExtendedCache::multiplier_index_info_table[multiplier_index + 1].first_cache_bit_index);
  304. // If the request window goes further than the left boundary of the source window,
  305. if (start_bit_index < src_start_bit_index)
  306. {
  307. number_of_leading_zero_blocks =
  308. static_cast<std::uint32_t>(src_start_bit_index - start_bit_index) /
  309. static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit);
  310. excessive_bits_to_left = static_cast<std::uint32_t>(src_start_bit_index - start_bit_index) %
  311. static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit);
  312. BOOST_IF_CONSTEXPR (!zero_out)
  313. {
  314. std::memset(blocks_ptr, 0, number_of_leading_zero_blocks * sizeof(CacheBlockType));
  315. }
  316. start_bit_index += static_cast<int>(number_of_leading_zero_blocks * ExtendedCache::cache_bits_unit);
  317. const auto src_start_block_index =
  318. static_cast<int>(static_cast<std::uint32_t>(src_start_bit_index) /
  319. static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit));
  320. const auto src_start_block_bit_index =
  321. src_start_block_index * static_cast<int>(ExtendedCache::cache_bits_unit);
  322. first_cache_block_index = static_cast<std::uint32_t>(src_start_block_index);
  323. if (start_bit_index < src_start_block_bit_index)
  324. {
  325. auto shift_amount = src_start_block_bit_index - start_bit_index;
  326. BOOST_CHARCONV_ASSERT(shift_amount >= 0 && shift_amount < static_cast<int>(ExtendedCache::cache_bits_unit));
  327. blocks_ptr[number_of_leading_zero_blocks] =
  328. ((ExtendedCache::cache[src_start_block_index] >> shift_amount) &
  329. (CacheBlockType(CacheBlockType(0) - CacheBlockType(1)) >>
  330. excessive_bits_to_left));
  331. ++number_of_leading_zero_blocks;
  332. bit_offset = static_cast<std::uint32_t>(static_cast<int>(ExtendedCache::cache_bits_unit) - shift_amount);
  333. excessive_bits_to_left = 0;
  334. }
  335. else
  336. {
  337. bit_offset = static_cast<std::uint32_t>(start_bit_index - src_start_block_bit_index);
  338. }
  339. }
  340. else
  341. {
  342. number_of_leading_zero_blocks = 0;
  343. first_cache_block_index =
  344. static_cast<std::uint32_t>(start_bit_index) / static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit);
  345. bit_offset =
  346. static_cast<std::uint32_t>(start_bit_index) % static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit);
  347. excessive_bits_to_left = 0;
  348. }
  349. // If the request window goes further than the right boundary of the source window,
  350. if (end_bit_index > src_end_bit_index)
  351. {
  352. const std::uint8_t number_of_trailing_zero_blocks =
  353. static_cast<std::uint8_t>(end_bit_index - src_end_bit_index) / ExtendedCache::cache_bits_unit;
  354. excessive_bits_to_right = static_cast<std::uint32_t>(end_bit_index - src_end_bit_index) %
  355. static_cast<std::uint32_t>(ExtendedCache::cache_bits_unit);
  356. cache_block_count -= number_of_trailing_zero_blocks;
  357. }
  358. else
  359. {
  360. excessive_bits_to_right = 0;
  361. }
  362. // Load blocks.
  363. const auto number_of_blocks_to_load = cache_block_count - number_of_leading_zero_blocks;
  364. auto* const dst_ptr = blocks_ptr + number_of_leading_zero_blocks;
  365. if (bit_offset == 0)
  366. {
  367. BOOST_IF_CONSTEXPR (ExtendedCache::max_cache_blocks == 3)
  368. {
  369. switch (number_of_blocks_to_load)
  370. {
  371. case 3:
  372. std::memcpy(dst_ptr, ExtendedCache::cache + first_cache_block_index, 3 * sizeof(CacheBlockType));
  373. break;
  374. case 2:
  375. std::memcpy(dst_ptr, ExtendedCache::cache + first_cache_block_index, 2 * sizeof(CacheBlockType));
  376. break;
  377. case 1:
  378. std::memcpy(dst_ptr, ExtendedCache::cache + first_cache_block_index, 1 * sizeof(CacheBlockType));
  379. break;
  380. case 0:
  381. break;
  382. default:
  383. BOOST_UNREACHABLE_RETURN(dst_ptr); // NOLINT
  384. }
  385. }
  386. else
  387. {
  388. std::memcpy(dst_ptr, ExtendedCache::cache + first_cache_block_index, number_of_blocks_to_load * sizeof(CacheBlockType));
  389. }
  390. }
  391. else
  392. {
  393. BOOST_IF_CONSTEXPR (ExtendedCache::max_cache_blocks == 3)
  394. {
  395. switch (number_of_blocks_to_load)
  396. {
  397. case 3:
  398. *(dst_ptr + 2) =
  399. (ExtendedCache::cache[first_cache_block_index + 2] << bit_offset) |
  400. (ExtendedCache::cache[first_cache_block_index + 3] >>
  401. (ExtendedCache::cache_bits_unit - bit_offset));
  402. BOOST_FALLTHROUGH;
  403. case 2:
  404. *(dst_ptr + 1) =
  405. (ExtendedCache::cache[first_cache_block_index + 1] << bit_offset) |
  406. (ExtendedCache::cache[first_cache_block_index + 2] >>
  407. (ExtendedCache::cache_bits_unit - bit_offset));
  408. BOOST_FALLTHROUGH;
  409. case 1:
  410. *dst_ptr = (ExtendedCache::cache[first_cache_block_index] << bit_offset) |
  411. (ExtendedCache::cache[first_cache_block_index + 1] >>
  412. (ExtendedCache::cache_bits_unit - bit_offset));
  413. case 0:
  414. break;
  415. default:
  416. BOOST_UNREACHABLE_RETURN(dst_ptr); // NOLINT
  417. }
  418. }
  419. else
  420. {
  421. for (std::uint8_t i = 0; i < number_of_blocks_to_load; ++i)
  422. {
  423. *(dst_ptr + i) =
  424. (ExtendedCache::cache[first_cache_block_index + i] << bit_offset) |
  425. (ExtendedCache::cache[first_cache_block_index + i + 1] >>
  426. (ExtendedCache::cache_bits_unit - bit_offset));
  427. }
  428. }
  429. }
  430. // Remove possible flooding bits from adjacent entries.
  431. *dst_ptr &= (CacheBlockType(CacheBlockType(0) - CacheBlockType(1)) >> excessive_bits_to_left);
  432. blocks_ptr[cache_block_count - 1] &= (CacheBlockType(CacheBlockType(0) - CacheBlockType(1)) << excessive_bits_to_right);
  433. // To compute ceil(2^Q * x / D), we need to check if
  434. // 2^Q * x / D = 2^(Q + e + k - eta - 1) * 5^(k - eta) is an integer or not.
  435. if (k < ExtendedCache::segment_length ||
  436. e + k + static_cast<int>(cache_block_count * ExtendedCache::cache_bits_unit) -
  437. static_cast<int>(excessive_bits_to_right) <
  438. ExtendedCache::segment_length + 1) {
  439. blocks_ptr[cache_block_count - 1] += (CacheBlockType(1) << excessive_bits_to_right);
  440. BOOST_CHARCONV_ASSERT(blocks_ptr[cache_block_count - 1] != 0);
  441. }
  442. return cache_block_count;
  443. }
  444. template <bool constant_block_count, std::uint8_t max_cache_blocks>
  445. struct cache_block_count_t;
  446. template <std::uint8_t max_cache_blocks>
  447. struct cache_block_count_t<false, max_cache_blocks>
  448. {
  449. std::uint8_t value;
  450. operator std::uint8_t() const noexcept { return value; } // NOLINT : implicit conversions are ok for block count
  451. cache_block_count_t& operator=(std::uint8_t new_value) noexcept
  452. {
  453. value = new_value;
  454. return *this;
  455. }
  456. };
  457. template <std::uint8_t max_cache_blocks>
  458. struct cache_block_count_t<true, max_cache_blocks>
  459. {
  460. static constexpr std::uint8_t value = max_cache_blocks;
  461. operator std::uint8_t() const noexcept { return value; } // NOLINT : implicit conversions are ok for block count
  462. cache_block_count_t& operator=(std::uint8_t) noexcept
  463. {
  464. // Don't do anything.
  465. return *this;
  466. }
  467. };
  468. template <unsigned n>
  469. struct uconst
  470. {
  471. constexpr uconst() {}; // NOLINT : Clang 3.x does not support = default
  472. static constexpr unsigned value = n;
  473. };
  474. BOOST_INLINE_VARIABLE constexpr uconst<0> uconst0;
  475. BOOST_INLINE_VARIABLE constexpr uconst<1> uconst1;
  476. BOOST_INLINE_VARIABLE constexpr uconst<6> uconst6;
  477. BOOST_INLINE_VARIABLE constexpr uconst<9> uconst9;
  478. BOOST_INLINE_VARIABLE constexpr uconst<14> uconst14;
  479. BOOST_INLINE_VARIABLE constexpr uconst<16> uconst16;
  480. #ifdef __clang__
  481. # pragma clang diagnostic push
  482. # pragma clang diagnostic ignored "-Wsign-conversion"
  483. #elif defined(__GNUC__)
  484. # pragma GCC diagnostic push
  485. # pragma GCC diagnostic ignored "-Wsign-conversion"
  486. #elif defined(BOOST_MSVC)
  487. # pragma warning(push)
  488. # pragma warning(disable: 4365 4267)
  489. #endif
  490. template <unsigned digits, bool dummy = (digits <= 9)>
  491. struct uint_with_known_number_of_digits;
  492. template <unsigned digits_>
  493. struct uint_with_known_number_of_digits<digits_, true>
  494. {
  495. static constexpr auto digits = digits_;
  496. std::uint32_t value;
  497. };
  498. template <unsigned digits_>
  499. struct uint_with_known_number_of_digits<digits_, false>
  500. {
  501. static constexpr auto digits = digits_;
  502. std::uint64_t value;
  503. };
  504. template <typename HasFurtherDigits, typename... Args, typename std::enable_if<std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  505. static BOOST_FORCEINLINE bool check_rounding_condition_inside_subsegment(
  506. std::uint32_t current_digits, std::uint32_t fractional_part,
  507. int remaining_digits_in_the_current_subsegment, HasFurtherDigits has_further_digits,
  508. Args...) noexcept
  509. {
  510. if (fractional_part >= additional_static_data_holder::fractional_part_rounding_thresholds32[remaining_digits_in_the_current_subsegment - 1])
  511. {
  512. return true;
  513. }
  514. return ((fractional_part >> 31) & ((current_digits & 1) | has_further_digits)) != 0;
  515. }
  516. template <typename HasFurtherDigits, typename... Args,
  517. typename std::enable_if<!std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  518. static BOOST_FORCEINLINE bool check_rounding_condition_inside_subsegment(
  519. std::uint32_t current_digits, std::uint32_t fractional_part,
  520. int remaining_digits_in_the_current_subsegment, HasFurtherDigits has_further_digits,
  521. Args... args) noexcept
  522. {
  523. if (fractional_part >= additional_static_data_holder::fractional_part_rounding_thresholds32[remaining_digits_in_the_current_subsegment - 1])
  524. {
  525. return true;
  526. }
  527. return fractional_part >= 0x80000000 && ((current_digits & 1) != 0 || has_further_digits(args...));
  528. }
  529. template <typename HasFurtherDigits, typename... Args,
  530. typename std::enable_if<std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  531. static BOOST_FORCEINLINE bool check_rounding_condition_with_next_bit(std::uint32_t current_digits, bool next_bit,
  532. HasFurtherDigits has_further_digits, Args...) noexcept
  533. {
  534. if (!next_bit)
  535. {
  536. return false;
  537. }
  538. return ((current_digits & 1) | has_further_digits) != 0;
  539. }
  540. template <typename HasFurtherDigits, typename... Args,
  541. typename std::enable_if<!std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  542. static BOOST_FORCEINLINE bool check_rounding_condition_with_next_bit(std::uint32_t current_digits, bool next_bit,
  543. HasFurtherDigits has_further_digits, Args... args) noexcept
  544. {
  545. if (!next_bit)
  546. {
  547. return false;
  548. }
  549. return (current_digits & 1) != 0 || has_further_digits(args...);
  550. }
  551. template <typename UintWithKnownDigits, typename HasFurtherDigits, typename... Args,
  552. typename std::enable_if<std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  553. static BOOST_FORCEINLINE bool check_rounding_condition_subsegment_boundary_with_next_subsegment(
  554. std::uint32_t current_digits, UintWithKnownDigits next_subsegment,
  555. HasFurtherDigits has_further_digits, Args...) noexcept
  556. {
  557. if (next_subsegment.value > power_of_10[decltype(next_subsegment)::digits] / 2)
  558. {
  559. return true;
  560. }
  561. return next_subsegment.value == power_of_10[decltype(next_subsegment)::digits] / 2 &&
  562. ((current_digits & 1) | has_further_digits) != 0;
  563. }
  564. template <typename UintWithKnownDigits, typename HasFurtherDigits, typename... Args,
  565. typename std::enable_if<!std::is_same<HasFurtherDigits, bool>::value, bool>::type = true>
  566. static BOOST_FORCEINLINE bool check_rounding_condition_subsegment_boundary_with_next_subsegment(
  567. std::uint32_t current_digits, UintWithKnownDigits next_subsegment,
  568. HasFurtherDigits has_further_digits, Args... args) noexcept
  569. {
  570. if (next_subsegment.value > power_of_10[decltype(next_subsegment)::digits] / 2)
  571. {
  572. return true;
  573. }
  574. return next_subsegment.value == power_of_10[decltype(next_subsegment)::digits] / 2 &&
  575. ((current_digits & 1) != 0 || has_further_digits(args...));
  576. }
  577. #ifdef __clang__
  578. # pragma clang diagnostic pop
  579. #elif defined(__GNUC__)
  580. # pragma GCC diagnostic pop
  581. #elif defined(BOOST_MSVC)
  582. # pragma warning(pop)
  583. #endif
  584. #ifdef BOOST_MSVC
  585. # pragma warning(push)
  586. # pragma warning(disable: 4307) // MSVC 14.1 emits warnings for uint64_t constants
  587. #endif
  588. namespace has_further_digits_impl {
  589. template <int k_right_threshold, int additional_neg_exp_of_2>
  590. bool no_neg_k_can_be_integer(int k, int exp2_base) noexcept
  591. {
  592. return k < k_right_threshold || exp2_base + k < additional_neg_exp_of_2;
  593. }
  594. template <int k_left_threshold, int k_right_threshold, int additional_neg_exp_of_2, int min_neg_exp_of_5, typename SignificandType>
  595. bool only_one_neg_k_can_be_integer(int k, int exp2_base, SignificandType significand) noexcept
  596. {
  597. // Supposed to be k - additional_neg_exp_of_5_v < -min_neg_exp_of_5 || ...
  598. if (k < k_left_threshold || exp2_base + k < additional_neg_exp_of_2)
  599. {
  600. return true;
  601. }
  602. // Supposed to be k - additional_neg_exp_of_5_v >= 0.
  603. if (k >= k_right_threshold)
  604. {
  605. return false;
  606. }
  607. BOOST_CXX14_CONSTEXPR std::uint64_t mod_inv = compute_power(UINT64_C(0xcccccccccccccccd), static_cast<unsigned>(min_neg_exp_of_5));
  608. BOOST_CXX14_CONSTEXPR std::uint64_t max_quot = UINT64_C(0xffffffffffffffff) / compute_power(UINT64_C(5), static_cast<unsigned>(min_neg_exp_of_5));
  609. return (significand * mod_inv) > max_quot;
  610. }
  611. template <int k_left_threshold, int k_middle_threshold, int k_right_threshold,
  612. int additional_neg_exp_of_2, int min_neg_exp_of_5, int segment_length,
  613. typename SignificandType>
  614. bool only_two_neg_k_can_be_integer(int k, int exp2_base,
  615. SignificandType significand) noexcept {
  616. // Supposed to be k - additional_neg_exp_of_5_v < -min_neg_exp_of_5 - segment_length
  617. // || ...
  618. if (k < k_left_threshold || exp2_base + k < additional_neg_exp_of_2) {
  619. return true;
  620. }
  621. // Supposed to be k - additional_neg_exp_of_5_v >= 0.
  622. if (k >= k_right_threshold) {
  623. return false;
  624. }
  625. if (k >= k_middle_threshold) {
  626. BOOST_CXX14_CONSTEXPR std::uint64_t mod_inv =
  627. compute_power(UINT64_C(0xcccccccccccccccd), static_cast<unsigned>(min_neg_exp_of_5));
  628. BOOST_CXX14_CONSTEXPR std::uint64_t max_quot =
  629. UINT64_C(0xffffffffffffffff) /
  630. compute_power(UINT64_C(5), static_cast<unsigned>(min_neg_exp_of_5));
  631. return (significand * mod_inv) > max_quot;
  632. }
  633. else {
  634. BOOST_CXX14_CONSTEXPR std::uint64_t mod_inv = compute_power(
  635. UINT64_C(0xcccccccccccccccd), static_cast<unsigned>(min_neg_exp_of_5 + segment_length));
  636. BOOST_CXX14_CONSTEXPR std::uint64_t max_quot =
  637. UINT64_C(0xffffffffffffffff) /
  638. compute_power(UINT64_C(5),
  639. static_cast<unsigned>(min_neg_exp_of_5 + segment_length));
  640. return (significand * mod_inv) > max_quot;
  641. }
  642. }
  643. } // Namespace has_further_digits_impl
  644. #ifdef BOOST_MSVC
  645. #pragma warning(pop)
  646. #endif
  647. inline void print_1_digit(std::uint32_t n, char* buffer) noexcept
  648. {
  649. *buffer = char('0' + n);
  650. }
  651. inline void print_2_digits(std::uint32_t n, char* buffer) noexcept
  652. {
  653. std::memcpy(buffer, additional_static_data_holder::radix_100_table + n * 2, 2);
  654. }
  655. inline void print_6_digits(std::uint32_t n, char* buffer) noexcept
  656. {
  657. // 429497 = ceil(2^32/10^4)
  658. auto prod = (n * UINT64_C(429497)) + 1;
  659. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  660. for (int i = 0; i < 2; ++i)
  661. {
  662. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  663. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer + 2 + i * 2);
  664. }
  665. }
  666. inline void print_7_digits(std::uint32_t n, char* buffer) noexcept
  667. {
  668. // 17592187 = ceil(2^(32+12)/10^6)
  669. auto prod = ((n * UINT64_C(17592187)) >> 12) + 1;
  670. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  671. for (int i = 0; i < 3; ++i)
  672. {
  673. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  674. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer + 1 + i * 2);
  675. }
  676. }
  677. inline void print_8_digits(std::uint32_t n, char* buffer) noexcept
  678. {
  679. // 140737489 = ceil(2^(32+15)/10^6)
  680. auto prod = ((n * UINT64_C(140737489)) >> 15) + 1;
  681. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  682. for (int i = 0; i < 3; ++i)
  683. {
  684. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  685. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer + 2 + i * 2);
  686. }
  687. }
  688. inline void print_9_digits(std::uint32_t n, char* buffer) noexcept
  689. {
  690. // 1441151881 = ceil(2^(32+25)/10^8)
  691. auto prod = ((n * UINT64_C(1441151881)) >> 25) + 1;
  692. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  693. for (int i = 0; i < 4; ++i)
  694. {
  695. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  696. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer + 1 + i * 2);
  697. }
  698. }
  699. struct main_cache_full
  700. {
  701. template <typename FloatFormat>
  702. static constexpr typename main_cache_holder::cache_entry_type get_cache(int k) noexcept
  703. {
  704. return main_cache_holder::cache[std::size_t(k - main_cache_holder::min_k)];
  705. }
  706. };
  707. struct main_cache_compressed
  708. {
  709. template <typename FloatFormat>
  710. static BOOST_CHARCONV_CXX14_CONSTEXPR typename main_cache_holder::cache_entry_type get_cache(int k) noexcept
  711. {
  712. BOOST_CHARCONV_ASSERT(k >= main_cache_holder::min_k && k <= main_cache_holder::max_k);
  713. BOOST_IF_CONSTEXPR (std::is_same<FloatFormat, ieee754_binary64>::value)
  714. {
  715. // Compute the base index.
  716. const auto cache_index =
  717. static_cast<int>(static_cast<std::uint32_t>(k - main_cache_holder::min_k) /
  718. compressed_cache_detail::compression_ratio);
  719. const auto kb = cache_index * compressed_cache_detail::compression_ratio +
  720. main_cache_holder::min_k;
  721. const auto offset = k - kb;
  722. // Get the base cache.
  723. const auto base_cache = compressed_cache_detail::cache_holder_t::table[cache_index];
  724. if (offset == 0)
  725. {
  726. return base_cache;
  727. }
  728. else
  729. {
  730. // Compute the required amount of bit-shift.
  731. const auto alpha = log::floor_log2_pow10(kb + offset) - log::floor_log2_pow10(kb) - offset;
  732. BOOST_CHARCONV_ASSERT(alpha > 0 && alpha < 64);
  733. // Try to recover the real cache.
  734. const auto pow5 = compressed_cache_detail::pow5_holder_t::table[offset];
  735. auto recovered_cache = umul128(base_cache.high, pow5);
  736. const auto middle_low = umul128(base_cache.low, pow5);
  737. recovered_cache += middle_low.high;
  738. const auto high_to_middle = recovered_cache.high << (64 - alpha);
  739. const auto middle_to_low = recovered_cache.low << (64 - alpha);
  740. recovered_cache = uint128{(recovered_cache.low >> alpha) | high_to_middle, ((middle_low.low >> alpha) | middle_to_low)};
  741. BOOST_CHARCONV_ASSERT(recovered_cache.low + 1 != 0);
  742. recovered_cache = uint128(recovered_cache.high, recovered_cache.low + 1);
  743. return recovered_cache;
  744. }
  745. }
  746. else
  747. {
  748. // Just use the full cache for anything other than binary64
  749. return main_cache_holder::cache[std::size_t(k - main_cache_holder::min_k)];
  750. }
  751. }
  752. };
  753. template <bool b>
  754. struct extended_cache_long_impl
  755. {
  756. static constexpr std::size_t max_cache_blocks = 3;
  757. static constexpr std::size_t cache_bits_unit = 64;
  758. static constexpr int segment_length = 22;
  759. static constexpr bool constant_block_count = true;
  760. static constexpr int e_min = -1074;
  761. static constexpr int k_min = -272;
  762. static constexpr int cache_bit_index_offset_base = 977;
  763. static constexpr std::uint64_t cache[] = {
  764. 0xa37fce126597973c, 0xe50ff107bab528a0, 0x8f1ba3f17395a391, 0xd56bdc876cdb4648,
  765. 0x6ca000bdd9e33bd4, 0x23cf34bbf983f78b, 0x8737d87296e93f5d, 0xa2824ba6d9df301d,
  766. 0x8ce3eccf7cfb42ab, 0xe5ecdc0b78109f00, 0xa620c9995c9c5c3a, 0xa0f79c97ac210943,
  767. 0x64dfb5636985915f, 0xc12f542e4c7ea6ee, 0x34de81232784ea17, 0xd0cbde7fac4643f2,
  768. 0x5d9400de8fef7552, 0x81214f68696d9af2, 0xb7d0e0a2ccaccf20, 0x5c4ed9243f16193d,
  769. 0xf71838486e60b926, 0x48892047ec1a8bf4, 0x14ff2faa9c32befa, 0x666fbaa24ddbb8e9,
  770. 0x436682c807652a58, 0xed98ddaee19068c7, 0x63badd624dd9b095, 0x72dbb637d5b77493,
  771. 0xd01998fb8d9e8861, 0xacb39418dce017b9, 0x8db8f2f13eed81cf, 0xfd699fbb7d0a737a,
  772. 0x011cd67160923d91, 0x9a66fd7732c14d98, 0x235857d065a52d18, 0x895288951dab0d8e,
  773. 0x59041cb66e4f0e68, 0x5e7c68240249e750, 0x8881a2a6ab00987b, 0x5fc8c32c863aaeac,
  774. 0x3bafbe662a7f81a8, 0xd47692705ae76b64, 0xeb1cc7d99143fb53, 0xcf8be24f7b0fc499,
  775. 0x6a276e8f0fbf33eb, 0x63b2d61966fa7243, 0x0970327d2cc58011, 0x43ff09410ec24aae,
  776. 0x0bdb6f345ea1851d, 0x409c37132c5836ff, 0xf3150f74a6190324, 0x5c358d6c07453d23,
  777. 0x7207012ad7846ba7, 0x61ad5d0772604733, 0x19a20a6e21c2018d, 0x5f568fd497ef18b2,
  778. 0xeda5815eed00749f, 0x029531461bc483d8, 0xb8789d7784875911, 0x6fc40572236f2ba5,
  779. 0x9c2a50a76ace3168, 0xbf4815c2bea56741, 0xf84e8f2fe9b211f5, 0x689033182d2ea7ed,
  780. 0x5bcb3a3230a68f47, 0xa848403d116805ef, 0xfaeaa73623b79604, 0x31d76828d2181b64,
  781. 0x7c4eabddc7dd634b, 0xc2b13231eeff6fda, 0x8094743db32bf251, 0x2df07391bde052d2,
  782. 0xffd9bdbf321ad8ae, 0x06b2c6d1cf6cf742, 0xf32a54ce1598fe8f, 0x1cc2e3082d28897e,
  783. 0x0485f2e46b488584, 0xe3f6965b145a49cb, 0x406eaa1217aefe69, 0x0777373638de456b,
  784. 0xcde91853b592212b, 0x3faf7b46d7f79c18, 0x558d83afb7127381, 0x5f490259c7957aeb,
  785. 0x76e6540e246d73cc, 0x5098a935a866dc75, 0xc50d9c29002d9e73, 0xcc8f8252faac0b7f,
  786. 0xb759afb688f8251d, 0x6a2934d3036c85d3, 0x570eb3ce4c86407f, 0x036f2b68794754af,
  787. 0x57661a5d6993fe2c, 0x6d07b7fabe546a80, 0x38efe4029259743c, 0x548f417ebaa61c6c,
  788. 0xb0c31fa64a3fcc9e, 0x7dab825964fb7100, 0xd0c92ae8207d6f22, 0xf1e38a8a9c541144,
  789. 0x2139951c68d0385b, 0x9d9e22c42f139287, 0x4fea4d670876b800, 0x35f293a9a62252d4,
  790. 0x4b606b26f1922c5c, 0x8e5660b37505cb11, 0x868138391855da81, 0x6e95f6c9b45c7aa2,
  791. 0x425ff75e14fc31a1, 0x258379a94d028d18, 0xdf2ccd1fe00a03b6, 0x398471c1ff970f83,
  792. 0x8c36b2214a3db8e7, 0x431dd42c3fe7f4fb, 0xb09bcf0fffb5b849, 0xc47dd13da60fb5a1,
  793. 0x8fdad56516fe9d75, 0xc317e1025a7e1c63, 0x9ddcb98cbb384fda, 0x80adccda993bf70e,
  794. 0x667f1622e4052ae4, 0xa41598d58f777363, 0x704b93d675808501, 0xaf046d3fd448aaf3,
  795. 0x1dc4611873bf3f70, 0x834acdae9f0f4f53, 0x4f5d60585a5f1c1a, 0x3ced1b4be0d415c1,
  796. 0x5d57f4de8ec12376, 0x51c0e7e72f799542, 0x46f7604940e6a510, 0x1a546a0f9345ed75,
  797. 0x0df4097cab773ca2, 0x72b122774e4029e6, 0xae4a55b99aebd424, 0x04163a291bad2fa3,
  798. 0x86ad58be322a49aa, 0x98f051614696e839, 0x64d08f241fc4ec58, 0xae41f23dca90dd5d,
  799. 0x68bbd62f5af3107a, 0x7025f39ef241c56c, 0xd2e7c72fa9be33ac, 0x0aece66fd3e29a7d,
  800. 0xd91241cebf3bd47c, 0x3ed7bfdee19ba2f6, 0x4bdf483194c7444e, 0xc99d83c931e8ab87,
  801. 0x1732f416dbf7381f, 0x2ac88e244de13b96, 0x2cab688bd86c8bf8, 0x9f209787bb47d6b8,
  802. 0x4c0678c5dbd23a49, 0xa0612c3c5ce15e55, 0x4dccc6ca29b3e9df, 0x0dc079c918022212,
  803. 0x26be55a64c249495, 0x4da2c9789dd268b0, 0xe975528c76435158, 0xa6cb8a4d2356f9cf,
  804. 0xdcafd2279c77d987, 0xaa9aff7904228690, 0xfb44d2f05d0842fb, 0x118fc9c217a1d2b2,
  805. 0x04b3d9686f55b572, 0xbd9cb3625ef1cfc3, 0x2eba0e25e938e6c3, 0x1f48eaf234ad3a21,
  806. 0xf2dc02fad2890f79, 0xace340325d4a7f9b, 0xe9e051f540b239dc, 0x221091f05abb8687,
  807. 0x7e08deb014db8afe, 0x4711e1e9d9a094cc, 0x0b2d79bd90a9ef61, 0xb93d19bd45b82515,
  808. 0x45e9e31d63c1afe1, 0x2c5f0a596005c216, 0xe687cc2331b14a12, 0x51963a2412b6f60c,
  809. 0x91aeb77c8fe68eaa, 0xd6e18e8cc6841d68, 0x9391085cc2c933d9, 0x6e184be07e68df49,
  810. 0x4fe4e52edb0dce60, 0x6cda31e8617f0ca2, 0xf8b9374fda7e7c95, 0x8032c603725e774d,
  811. 0x222b6aa27e007612, 0xf7b7f47cf096afad, 0xe6a9fbafee77e77a, 0x3776ee406e63fbaa,
  812. 0xde147932fcf78be6, 0x2ab9e031ffaa071e, 0x2169ad0e8a9b1256, 0xe33358135938b76a,
  813. 0xcaec07e7a5373835, 0xef2863090a97c3ec, 0x6ccfb95f69c3adcc, 0x173e00da427cee4b,
  814. 0x20f4ed58fcfb3040, 0x16f6fb326a60c32c, 0x2968fa04270ed545, 0x70673adfac0eabc4,
  815. 0x6ff3c9364ff4e873, 0xde09ed35f13325d3, 0x2396e863b18c500f, 0xe22d253cc031e3ff,
  816. 0x756d97a61247798d, 0xc9fc8d937e43c880, 0x0759ba59c08e14c7, 0xcd7aad86a4a45810,
  817. 0x9f91c21c571dbe84, 0xd52d936f44abe8a3, 0xd5b48c100959d9d0, 0xb6cc856b3adc93b6,
  818. 0x7aea8f8e067d2c8d, 0x04bc177f7b4287a6, 0xe3fcda36fa3b3342, 0xeaeb442e15d45095,
  819. 0x2f4dd1ca5e89b18b, 0x602368385bb19cb1, 0x4bdfc434d3028181, 0x0b5a92cb80ac8150,
  820. 0xb95953a97b1578ab, 0x46e6a18b01781b92, 0xdfd31585f38d7433, 0x0b1084b96009370b,
  821. 0x9a81808e52462ba3, 0xff83368ace4af235, 0xb4e5d8a647e05e95, 0xf848cfc90df4b231,
  822. 0x9919c68cf3576038, 0x1e89dad8a6790435, 0x7ac9361379139511, 0x7b5f9b6b937a7760,
  823. 0x6e42e395fde0c1f7, 0x430cef1679799f8f, 0x0ad21cc1b4828074, 0x8982577d0ea42349,
  824. 0xb1aca6185a7d0d0d, 0x4085c6db106c3d74, 0xba6f7a86e728a418, 0x0325a28758a974d2,
  825. 0x57ea317f731817ed, 0xbd1e8e00b215a6eb, 0xb39f323742948e87, 0x9f9b0f873784cef4,
  826. 0xa8c83d26585c5377, 0x837ba337bfcf893c, 0x0a7eeca62a23b805, 0xba4925a9e7f7346f,
  827. 0xa574eebb90c8da6d, 0x5db7ff0e8d0b8d2d, 0x1562834c52c048d8, 0x0b2e577a853bcafc,
  828. 0xdecef97a3524ff97, 0xeec053c8fd537066, 0xeaf2b1df83d600e4, 0x5be8b9ab7717eccf,
  829. 0x05905b91ecbba038, 0xabacba5b373029ed, 0x22fb2283c0ee1267, 0x9c32b2ec3634c580,
  830. 0x5186c586b6e5611c, 0x71eb0de5e91bb0a0, 0x89e969b42975ef08, 0x2ba0958bc44e322f,
  831. 0x626d033cb828ba7d, 0xe5fbb65c7776509d, 0xb1403ae51ae9bc82, 0x5d773f0d9753a966,
  832. 0x4a06feadd4ec8585, 0xda58a710fccd7b76, 0x6061ba4cd3d80d59, 0xf4824f5cfa2ba71c,
  833. 0xfce622bba0ece756, 0x7d9c738486bc6842, 0x5f629d33c99db969, 0x855ff7c9b79362e6,
  834. 0x892188a87c7de231, 0x85fea7caf30e2b5e, 0xbefeb221543782c5, 0x769ca33d280842f6,
  835. 0x3974ebaf71353e52, 0xed0577283980f0cb, 0x7c37d689ab6b0662, 0x5037aeffcd3db52d,
  836. 0x11bb0a5f64fbdcb5, 0xf5fd5aa5f2b7e974, 0xe1aa07ba7074367b, 0x4b5c14aa1c6a0d28,
  837. 0xe9fc8c9c36f73953, 0x2609ad2cd0f99b76, 0x8d4f1d6bb589844f, 0xde09f066714fa909,
  838. 0xe004c5d7adad3747, 0xd5ac81a94dfdefe3, 0xfd3e0083658a13c2, 0xf5512f25dd6e39a7,
  839. 0xeb7204042ffa181d, 0x046d9254242d06e3, 0x91a5ca94f8706fab, 0xf5c58cc57af63c98,
  840. 0x04e7ff1e23474908, 0xe4a9bec5c5818324, 0x1edfb105cc3084dd, 0x82431ec76e72a87a,
  841. 0xe0b215be32c51083, 0x0d9942e3b5245098, 0xa49f1aad5723fd7e, 0xad45edba25a4bde8,
  842. 0x241f0adc0cd56771, 0xf09bf2de59df3274, 0x090db856bbc020f2, 0x6aa4efb2d2ecb9bb,
  843. 0xc6be4224ba04c233, 0x557a1760bde90850, 0x23090117938cb921, 0xcbec34da23f3e9c2,
  844. 0xdfe2d55daad85c54, 0xa7932be700067f48, 0xfb7874535e2d76a4, 0x5161ba088056e74f,
  845. 0xc275a8435be6cdb2, 0x05fcb771cab5aa15, 0x7f18a4382c9565a8, 0x4244c2cb833d6710,
  846. 0x884e2b7a4a3db4d0, 0x08ded459d3edf2c2, 0x1616df531fee90cd, 0x9531c65800a97aaa,
  847. 0x881ba77ab7e5d63a, 0x606d27428df4edd3, 0x294063ed78e305c7, 0x7de2b12f8a8cceb5,
  848. 0xe6b01cc54a494437, 0x0cdecbe5ac90907c, 0xb88496c657d3e644, 0xf3eecf996f9c6b13,
  849. 0x24aad7949edcde03, 0x304ca88ebfeaa534, 0x7b68a7bd3ef1916b, 0x3cc307a784d9060c,
  850. 0x5dca03f19b213efd, 0xa380539c235f80c3, 0xf39756fc01d75bd7, 0x39ac6c7281739adb,
  851. 0x4b606dc4aa036fda, 0x97126cd02a23b97c, 0x98c1e6906230aead, 0xe12d0f696a6bbc36,
  852. 0x657a202bb6a89a33, 0x6421a07bda47e13d, 0x8d9d21b3c6b1dbee, 0x1f110f3744f13e0d,
  853. 0x04d86fccb6e77ee8, 0x8c92852d9c9c14b3, 0x56be3cef19b19446, 0x57ceef0e2ebcbcf7,
  854. 0x230a9328be0144bf, 0x3c1949b98a92aebc, 0x7ed2db80a62003f2, 0x84e609d13c7594f4,
  855. 0xf8e81b9a9f35b4e8, 0xc2982fde1a087e4b, 0x84b0713cb3b18147, 0x3582530578d1ff08,
  856. 0x0e5b6538cd61fce4, 0x46867abf4b6e72bc, 0x4fe9652832325e89, 0x7d141d065654745f,
  857. 0x9bd5c0479188a53d, 0x4ccd47925108c00b, 0xfd3f6c8d961d47e3, 0x9c5c18a96093d2ad,
  858. 0xa7d91bf008a358c3, 0x3ea3e5629f977d55, 0x80f0fed6a5f06003, 0x21f390e377ee4d68,
  859. 0x73ed055ec082526b, 0x28482600c10f6ce2, 0x2bff1aaf94c11fe9, 0xde29cb7a943801b8,
  860. 0x045b0493dd35af0e, 0xaeae25ff7a431c16, 0x78c9d3348f5364b7, 0xf973d1af84bc2476,
  861. 0x4d2303e11baf18f3, 0xacebdb3fe5efbc7b, 0xd274a5cf5be50678, 0x2d60c40fdf53ac67,
  862. 0x109592b606139855, 0x612f472a9c09925f, 0x701a035ccd4e7ab0, 0xac881f0db121a709,
  863. 0xe1ed47438368366d, 0xde2faff8eeb2810a, 0x8eb2188044342ef9, 0x0e3c1aa7b6851548,
  864. 0x7ce94a6ba4fd843f, 0x0da503676ee5ebb2, 0xf3bc7bb2cb8669e8, 0xd4b9e44de392fe64,
  865. 0x81e470ebf207fdea, 0xdd53b09d49a0e5b5, 0xf78e23167a350d5a, 0x706470fc2d84423b,
  866. 0x816ee82b19a29476, 0x35a9d218ba7cd4a1, 0xf590f12fb09b3fe3, 0x5e574140b302f8b7,
  867. 0x6cb237a2021f77c3, 0x30a29037231a861e, 0xff4bb07af553a606, 0x831412ee2690d92c,
  868. 0xf6d2d725ef14ff67, 0x2f79f810928a40ff, 0x2857d91ea9b04f71, 0xd063066f0ed78f3c,
  869. 0xbf4b8dbc8a34017d, 0x6230f319f8b1f9c4, 0x061b0e25d8899834, 0x4071de32ef7ff0bf,
  870. 0xbc546a0793fcfcd3, 0xd5881f5d968cf898, 0x0e21c0674cdda190, 0x0000000000000000};
  871. struct multiplier_index_info
  872. {
  873. std::uint16_t first_cache_bit_index;
  874. std::uint16_t cache_bit_index_offset;
  875. };
  876. static constexpr multiplier_index_info multiplier_index_info_table[] = {
  877. {0, 0}, {171, 244}, {419, 565}, {740, 959}, {1135, 1427},
  878. {1604, 1969}, {2141, 2579}, {2750, 3261}, {3434, 4019}, {4191, 4849},
  879. {5019, 5750}, {5924, 6728}, {6904, 7781}, {7922, 8872}, {8993, 10016},
  880. {9026, 10122}, {9110, 10279}, {9245, 10487}, {9431, 10746}, {9668, 11056},
  881. {9956, 11418}, {10296, 11831}, {10687, 12295}, {11129, 12810}, {11622, 13376},
  882. {12166, 13993}, {12761, 14661}, {13407, 15380}, {14104, 16150}, {14852, 16902},
  883. {15582, 17627}, {16285, 18332}, {16968, 19019}, {17633, 19683}, {18275, 20326},
  884. {18896, 20947}, {19495, 21546}, {20072, 22122}, {20626, 22669}, {21151, 23202},
  885. {21662, 23713}, {22151, 24202}, {22618, 24669}, {23063, 25114}, {23486, 25535},
  886. {23885, 25936}, {24264, 26313}, {24619, 26670}, {24954, 27004}, {25266, 27316},
  887. {25556, 27603}, {25821, 27870}, {26066, 28117}, {26291, 28340}, {26492, 28543},
  888. {26673, 28723}, {26831, 28881}, {26967, 29018}, {27082, 29133}, {27175, 29225},
  889. {27245, 29296}, {27294, 29344}, {27320, 29370}, {27324, 0}};
  890. };
  891. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES) && (!defined(BOOST_MSVC) || BOOST_MSVC != 1900)
  892. template <bool b> constexpr std::size_t extended_cache_long_impl<b>::max_cache_blocks;
  893. template <bool b> constexpr std::size_t extended_cache_long_impl<b>::cache_bits_unit;
  894. template <bool b> constexpr int extended_cache_long_impl<b>::segment_length;
  895. template <bool b> constexpr bool extended_cache_long_impl<b>::constant_block_count;
  896. template <bool b> constexpr int extended_cache_long_impl<b>::e_min;
  897. template <bool b> constexpr int extended_cache_long_impl<b>::k_min;
  898. template <bool b> constexpr int extended_cache_long_impl<b>::cache_bit_index_offset_base;
  899. template <bool b> constexpr std::uint64_t extended_cache_long_impl<b>::cache[];
  900. template <bool b> constexpr typename extended_cache_long_impl<b>::multiplier_index_info extended_cache_long_impl<b>::multiplier_index_info_table[];
  901. #endif
  902. using extended_cache_long = extended_cache_long_impl<true>;
  903. struct extended_cache_compact
  904. {
  905. static constexpr std::size_t max_cache_blocks = 6;
  906. static constexpr std::size_t cache_bits_unit = 64;
  907. static constexpr int segment_length = 80;
  908. static constexpr bool constant_block_count = false;
  909. static constexpr int collapse_factor = 64;
  910. static constexpr int e_min = -1074;
  911. static constexpr int k_min = -211;
  912. static constexpr int cache_bit_index_offset_base = 967;
  913. static constexpr int cache_block_count_offset_base = 27;
  914. static constexpr std::uint64_t cache[] = {
  915. 0x9faacf3df73609b1, 0x77b191618c54e9ac, 0xcbc0fe19cae9528c, 0x8164d034592c3d4e,
  916. 0x04c42d46c9d7a229, 0x7ee39007a5bc8cc3, 0x5469cf7bb8b25e57, 0x2effce010198cb81,
  917. 0x642eb5bc0d8169e0, 0x91356aed1f5cd514, 0xe1c8f30156868b8c, 0xd1201a2b857f5cc5,
  918. 0x15c07ee55715eff8, 0x8530360cd386f94f, 0xeb706c10ea02c329, 0x3cb22680f921f59e,
  919. 0x3231912d5bf60e61, 0x0e1fff697ed6c695, 0xa8bed97c2f3b63fc, 0xda96e93c07538a6d,
  920. 0xc1c4e34ccd6fdbc5, 0x85c09fd1d0f79834, 0x485f3a5d03622bba, 0xe640b09cca5b9d50,
  921. 0x19a80913a40927a9, 0x4d82d751a5cf886d, 0x325c9cd793b9977b, 0x4896c18501fb9e0c,
  922. 0xa9993bfdf3ea7275, 0xcb7d257a3ee7c9d8, 0xcbf8fdb78849a5f9, 0x6de98520472bdd03,
  923. 0x36efd14b69b311de, 0x694fa387dcf3e78f, 0xdccfbfc61d1662ef, 0xbe3a4d4104fb75a2,
  924. 0x289ccaebae5c6d2d, 0x436915952987fa63, 0x830446728505ab75, 0x3ad8772923e4e0c0,
  925. 0xca946600436f3894, 0x0faae7895e3885f0, 0xadf6b773b1ebf8e0, 0x52473dd5e8218647,
  926. 0x5e6b5121ca3b747c, 0x217399923cd80bc0, 0x0a56ced144bb2f9f, 0xb856e82eea863c1f,
  927. 0x5cdae42f9562104d, 0x3fa421962c8c4241, 0x63451ff73769a3d2, 0xb0895649e11affd6,
  928. 0xe5dd7be415e5d3ef, 0x282a242e818f1668, 0xc8a86da5faf0b5cc, 0xf5176ecc7cbb19db,
  929. 0x2a9a282e49b4da0e, 0x59e22f9ed2cb3a4b, 0xc010afa26505a7e7, 0xee47b3ab83a99c3e,
  930. 0xc7eafae5fa385ec2, 0x3ec747e06293a148, 0x4b8a8260baf424a7, 0x63079a1ac7709a4e,
  931. 0x7fd0cd567aa4a0fa, 0x6909d0e0cfc6ce8d, 0xe0c965770d1491dd, 0xa6d4449e3a3e13ea,
  932. 0x73e06d2253c6b584, 0x9f95a4b69679998d, 0x0cc8cc76a8234060, 0xd3da311bb4fc0aae,
  933. 0x670614382f45f33c, 0x21f68425f4189fbf, 0x557ce28d58d9a8bd, 0x1f16d908907d0a0e,
  934. 0x929415f993b9a2c2, 0x95e0878748988052, 0xc4a104701f794a31, 0xe7d2d2b0c3c31b19,
  935. 0x1e6a68d5574b3d9d, 0x5727ec70c7681154, 0xe4b2adae8ac5259e, 0x1cefff5ed639205f,
  936. 0xf9410ba5daeb3af5, 0x21b0ad30acb4b8d2, 0xd324604028bf6fac, 0x349a5d2dc4bdc6e0,
  937. 0xc77223714aff22d9, 0x5b18ce4aabb5b369, 0xb8a6d609b15ecab7, 0x2111dbce86023643,
  938. 0x2a5717a571b96b6c, 0x8039783af28427bf, 0x5bbadd6a1a3fb931, 0xe8564a7a3e3ff2dc,
  939. 0xd0868939e541158e, 0xc57d0b8a8af06dde, 0xf1706d329def96c1, 0xbe74f435713bb7d5,
  940. 0x8dcdaef5bfb0242c, 0x73b5a1c8c8ec33c7, 0x4ab726d9dac95550, 0x210cf3b3ddfa00ae,
  941. 0x559d5e65eefbfa04, 0xe5d1f67c5f9de0ec, 0x6ad4699ea2d0efd6, 0x9590c0f05024f29a,
  942. 0x917d5715e6e20913, 0xb13124a40bffe5ba, 0x5248ce22e40406e5, 0xb844b16596551ded,
  943. 0xad4c4c5140496c58, 0x458562ae335689b6, 0x269441e13a195ad3, 0x7a5e32a8baf53ea8,
  944. 0x6d1469edb474b5f6, 0xe87b554829f6ee5b, 0xbf824a42bae3bdef, 0xed12ec6937744feb,
  945. 0x2ca544e624e048f9, 0x1bab8d5ee0c61285, 0x8863eaef018d32d9, 0x98f37ac46669f7ea,
  946. 0xa9a0573cb5501b2b, 0xf25c3a8e08a5694d, 0x42355a8000000000, 0x0000000000000000};
  947. struct multiplier_index_info
  948. {
  949. std::uint16_t first_cache_bit_index;
  950. std::uint16_t cache_bit_index_offset;
  951. std::uint16_t cache_block_count_index_offset;
  952. };
  953. static constexpr multiplier_index_info multiplier_index_info_table[] = {
  954. {0, 0, 0}, {377, 643, 9}, {1020, 1551, 22}, {1924, 2721, 39},
  955. {3046, 4109, 60}, {3114, 4443, 70}, {3368, 4962, 84}, {3807, 5667, 98},
  956. {4432, 6473, 111}, {5158, 7199, 123}, {5804, 7845, 134}, {6370, 8411, 143},
  957. {6856, 8896, 151}, {7261, 9302, 158}, {7587, 9628, 164}, {7833, 9874, 168},
  958. {7999, 10039, 171}, {8084, 10124, 173}, {8089, 0, 0}};
  959. static constexpr std::uint8_t cache_block_counts[] = {
  960. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  961. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x56, 0x34, 0x12, 0x66,
  962. 0x66, 0x45, 0x23, 0x61, 0x66, 0x66, 0x66, 0x45, 0x23, 0x61, 0x66, 0x66, 0x66,
  963. 0x56, 0x34, 0x12, 0x66, 0x66, 0x66, 0x56, 0x34, 0x12, 0x66, 0x66, 0x66, 0x45,
  964. 0x23, 0x61, 0x66, 0x56, 0x34, 0x12, 0x66, 0x56, 0x34, 0x12, 0x66, 0x45, 0x23,
  965. 0x61, 0x45, 0x23, 0x41, 0x23, 0x31, 0x12, 0x12, 0x01};
  966. };
  967. #ifdef BOOST_CXX17_INLINE_VARIABLES
  968. constexpr std::size_t extended_cache_compact::max_cache_blocks;
  969. constexpr std::size_t extended_cache_compact::cache_bits_unit;
  970. constexpr int extended_cache_compact::segment_length;
  971. constexpr bool extended_cache_compact::constant_block_count;
  972. constexpr int extended_cache_compact::collapse_factor;
  973. constexpr int extended_cache_compact::e_min;
  974. constexpr int extended_cache_compact::k_min;
  975. constexpr int extended_cache_compact::cache_bit_index_offset_base;
  976. constexpr int extended_cache_compact::cache_block_count_offset_base;
  977. constexpr extended_cache_compact::multiplier_index_info extended_cache_compact::multiplier_index_info_table[];
  978. constexpr std::uint8_t extended_cache_compact::cache_block_counts[];
  979. #endif
  980. struct extended_cache_super_compact
  981. {
  982. static constexpr std::size_t max_cache_blocks = 15;
  983. static constexpr std::size_t cache_bits_unit = 64;
  984. static constexpr int segment_length = 252;
  985. static constexpr bool constant_block_count = false;
  986. static constexpr int collapse_factor = 128;
  987. static constexpr int e_min = -1074;
  988. static constexpr int k_min = -65;
  989. static constexpr int cache_bit_index_offset_base = 1054;
  990. static constexpr int cache_block_count_offset_base = 10;
  991. static constexpr std::uint64_t cache[] = {
  992. 0xf712b443bbd52b7b, 0xa5e9ec7501d523e4, 0x6f99ee8b281c132a, 0x1c7262e905287f33,
  993. 0xbf4f71a69f411989, 0xe95fb0bf35d5c518, 0x00d875ffe81c1457, 0x31f0fcb03c200323,
  994. 0x6f64d6af592895a0, 0x45c073ee14c78fb0, 0x8744404cbdba226c, 0x8dbe2386885f0c74,
  995. 0x279b6693e94ab813, 0x6df0a4a86ccbb52e, 0xa94baea98e947129, 0xfc2b4e9bb4cbe9a4,
  996. 0x73bbc273e753c4ad, 0xc70c8ff8c19c1059, 0xb7da754b6db8b578, 0x5214cf7f2274988c,
  997. 0x39b5c4db3b36b321, 0xda6f355441d9f234, 0x01ab018d850bd7e2, 0x36517c3f140b3bcf,
  998. 0xd0e52375d8d125a7, 0xaf9709f49f3b8404, 0x022dd12dd219aa3f, 0x46e2ecebe43f459e,
  999. 0xa428ebddeecd6636, 0x3a7d11bff7e2a722, 0xd35d40e9d3b97c7d, 0x60ef65c4478901f1,
  1000. 0x945301feb0da841a, 0x2028c054ab187f51, 0xbe94b1f686a8b684, 0x09c13fdc1c4868c9,
  1001. 0xf2325ac2bf88a4ce, 0x92980d8fa53b6888, 0x8f6e17c7572a3359, 0x2964c5bfdd7761f2,
  1002. 0xf60269fc4910b562, 0x3ca164c4a2183ab0, 0x13f4f9e5a06a95c9, 0xf75022e39380598a,
  1003. 0x0d3f3c870002ab76, 0x24a4beb4780b78ef, 0x17a59a8f5696d625, 0x0ad76de884cb489d,
  1004. 0x559d3d0681553d6a, 0x813dcf205788af76, 0xf42f9c3ad707bf72, 0x770d63ceb129026c,
  1005. 0xa604d413fc14c7c2, 0x3cfc19e01239c784, 0xec7ef19965cedd56, 0x7303dcb3b300b6fd,
  1006. 0x118059e1139c0f3c, 0x97097186308c91f7, 0x2ad91d77379dce42, 0xad396c61acbe15ec,
  1007. 0x728518461b5722b6, 0xb85c5bb1ed805ecd, 0x816abc04592a4974, 0x1866b17c7cfbd0d0,
  1008. 0x0000000000000000};
  1009. struct multiplier_index_info
  1010. {
  1011. std::uint16_t first_cache_bit_index;
  1012. std::uint16_t cache_bit_index_offset;
  1013. std::uint16_t cache_block_count_index_offset;
  1014. };
  1015. static constexpr multiplier_index_info multiplier_index_info_table[] = {
  1016. {0, 0, 0}, {860, 1698, 13}, {2506, 4181, 29}, {2941, 5069, 36},
  1017. {3577, 5705, 41}, {3961, 6088, 44}, {4092, 0, 0}};
  1018. static constexpr std::uint8_t cache_block_counts[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xee,
  1019. 0xee, 0xee, 0xee, 0xee, 0xac, 0x68,
  1020. 0x24, 0x8a, 0x46, 0x62, 0x24, 0x13};
  1021. };
  1022. #ifdef BOOST_CXX17_INLINE_VARIABLES
  1023. constexpr std::size_t extended_cache_super_compact::max_cache_blocks;
  1024. constexpr std::size_t extended_cache_super_compact::cache_bits_unit;
  1025. constexpr int extended_cache_super_compact::segment_length;
  1026. constexpr bool extended_cache_super_compact::constant_block_count;
  1027. constexpr int extended_cache_super_compact::collapse_factor;
  1028. constexpr int extended_cache_super_compact::e_min;
  1029. constexpr int extended_cache_super_compact::k_min;
  1030. constexpr int extended_cache_super_compact::cache_bit_index_offset_base;
  1031. constexpr int extended_cache_super_compact::cache_block_count_offset_base;
  1032. constexpr std::uint64_t extended_cache_super_compact::cache[];
  1033. constexpr extended_cache_super_compact::multiplier_index_info extended_cache_super_compact::multiplier_index_info_table[];
  1034. constexpr std::uint8_t extended_cache_super_compact::cache_block_counts[];
  1035. #endif
  1036. #ifdef BOOST_MSVC
  1037. # pragma warning(push)
  1038. # pragma warning(disable: 4100) // MSVC 14.0 warning of unused formal parameter is incorrect
  1039. #endif
  1040. template <unsigned v1, unsigned v2, typename ExtendedCache>
  1041. bool has_further_digits(std::uint64_t significand, int exp2_base, int& k, boost::charconv::detail::uconst<v1> additional_neg_exp_of_2_c, boost::charconv::detail::uconst<v2> additional_neg_exp_of_10_c) noexcept
  1042. {
  1043. constexpr auto additional_neg_exp_of_2_v = static_cast<int>(decltype(additional_neg_exp_of_2_c)::value +
  1044. decltype(additional_neg_exp_of_10_c)::value);
  1045. constexpr auto additional_neg_exp_of_5_v = static_cast<int>(decltype(additional_neg_exp_of_10_c)::value);
  1046. constexpr auto min_neg_exp_of_5 = (-ExtendedCache::k_min + additional_neg_exp_of_5_v) % ExtendedCache::segment_length;
  1047. // k >= k_right_threshold iff k - k1 >= 0.
  1048. static_assert(additional_neg_exp_of_5_v + ExtendedCache::segment_length >= 1 + ExtendedCache::k_min,
  1049. "additional_neg_exp_of_5_v + ExtendedCache::segment_length >= 1 + ExtendedCache::k_min");
  1050. constexpr auto k_right_threshold = ExtendedCache::k_min +
  1051. ((additional_neg_exp_of_5_v + ExtendedCache::segment_length - 1 -
  1052. ExtendedCache::k_min) /
  1053. ExtendedCache::segment_length) *
  1054. ExtendedCache::segment_length;
  1055. // When the smallest absolute value of negative exponent for 5 is too big,
  1056. // so whenever the exponent for 5 is negative, the result cannot be an
  1057. // integer.
  1058. BOOST_IF_CONSTEXPR (min_neg_exp_of_5 > 23)
  1059. {
  1060. return boost::charconv::detail::has_further_digits_impl::no_neg_k_can_be_integer<
  1061. k_right_threshold, additional_neg_exp_of_2_v>(k, exp2_base);
  1062. }
  1063. // When the smallest absolute value of negative exponent for 5 is big enough, so
  1064. // the only negative exponent for 5 that allows the result to be an integer is the
  1065. // smallest one.
  1066. else BOOST_IF_CONSTEXPR (min_neg_exp_of_5 + ExtendedCache::segment_length > 23)
  1067. {
  1068. // k < k_left_threshold iff k - k1 < -min_neg_exp_of_5.
  1069. static_assert(additional_neg_exp_of_5_v + ExtendedCache::segment_length >= min_neg_exp_of_5 + 1 + ExtendedCache::k_min,
  1070. "additional_neg_exp_of_5_v + ExtendedCache::segment_length >= min_neg_exp_of_5 + 1 + ExtendedCache::k_min");
  1071. constexpr auto k_left_threshold =
  1072. ExtendedCache::k_min +
  1073. ((additional_neg_exp_of_5_v - min_neg_exp_of_5 +
  1074. ExtendedCache::segment_length - 1 - ExtendedCache::k_min) /
  1075. ExtendedCache::segment_length) *
  1076. ExtendedCache::segment_length;
  1077. return boost::charconv::detail::has_further_digits_impl::only_one_neg_k_can_be_integer<
  1078. k_left_threshold, k_right_threshold, additional_neg_exp_of_2_v,
  1079. min_neg_exp_of_5>(k, exp2_base, significand);
  1080. }
  1081. // When the smallest absolute value of negative exponent for 5 is big enough, so
  1082. // the only negative exponents for 5 that allows the result to be an integer are the
  1083. // smallest one and the next smallest one.
  1084. else
  1085. {
  1086. static_assert(min_neg_exp_of_5 + 2 * ExtendedCache::segment_length > 23,
  1087. "min_neg_exp_of_5 + 2 * ExtendedCache::segment_length > 23");
  1088. constexpr auto k_left_threshold =
  1089. ExtendedCache::k_min +
  1090. ((additional_neg_exp_of_5_v - min_neg_exp_of_5 - 1 - ExtendedCache::k_min) /
  1091. ExtendedCache::segment_length) *
  1092. ExtendedCache::segment_length;
  1093. constexpr auto k_middle_threshold =
  1094. ExtendedCache::k_min +
  1095. ((additional_neg_exp_of_5_v - min_neg_exp_of_5 +
  1096. ExtendedCache::segment_length - 1 - ExtendedCache::k_min) /
  1097. ExtendedCache::segment_length) *
  1098. ExtendedCache::segment_length;
  1099. return boost::charconv::detail::has_further_digits_impl::only_two_neg_k_can_be_integer<
  1100. k_left_threshold, k_middle_threshold, k_right_threshold,
  1101. additional_neg_exp_of_2_v, min_neg_exp_of_5, ExtendedCache::segment_length>(
  1102. k, exp2_base, significand);
  1103. }
  1104. }
  1105. template <unsigned v1, unsigned v2, typename ExtendedCache>
  1106. inline bool has_further_digits(std::uint64_t significand, int exp2_base, int& k)
  1107. {
  1108. boost::charconv::detail::uconst<v1> additional_neg_exp_of_2_c;
  1109. boost::charconv::detail::uconst<v2> additional_neg_exp_of_10_c;
  1110. return has_further_digits<v1, v2, ExtendedCache>(significand, exp2_base, k, additional_neg_exp_of_2_c, additional_neg_exp_of_10_c);
  1111. }
  1112. template <unsigned additional_neg_exp_of_2, unsigned additional_neg_exp_of_10, typename ExtendedCache>
  1113. bool compute_has_further_digits(unsigned remaining_subsegment_pairs, std::uint64_t significand, int exp2_base, int& k) noexcept
  1114. {
  1115. #define BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(n) \
  1116. case n: \
  1117. return has_further_digits<additional_neg_exp_of_2, additional_neg_exp_of_10 + (n - 1) * 18, ExtendedCache>(significand, exp2_base, k)
  1118. switch (remaining_subsegment_pairs) {
  1119. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(1);
  1120. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(2);
  1121. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(3);
  1122. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(4);
  1123. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(5);
  1124. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(6);
  1125. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(7);
  1126. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(8);
  1127. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(9);
  1128. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(10);
  1129. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(11);
  1130. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(12);
  1131. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(13);
  1132. BOOST_CHARCONV_252_HAS_FURTHER_DIGITS(14);
  1133. default:
  1134. BOOST_UNREACHABLE_RETURN(remaining_subsegment_pairs); // NOLINT
  1135. }
  1136. #undef BOOST_CHARCONV_252_HAS_FURTHER_DIGITS
  1137. BOOST_UNREACHABLE_RETURN(false); // NOLINT
  1138. }
  1139. #ifdef BOOST_MSVC
  1140. # pragma warning(pop)
  1141. #endif
  1142. // Print 0.000...0 where precision is the number of 0's after the decimal dot.
  1143. inline to_chars_result print_zero_fixed(char* buffer, std::size_t buffer_size, const int precision) noexcept
  1144. {
  1145. // No trailing decimal dot.
  1146. if (precision == 0)
  1147. {
  1148. *buffer = '0';
  1149. return {buffer + 1, std::errc()};
  1150. }
  1151. if (buffer_size < static_cast<std::size_t>(precision) + 2U)
  1152. {
  1153. return {buffer + buffer_size, std::errc::value_too_large};
  1154. }
  1155. std::memcpy(buffer, "0.", 2); // NOLINT : Specifically not null-terminating
  1156. std::memset(buffer + 2, '0', static_cast<std::size_t>(precision)); // NOLINT : Specifically not null-terminating
  1157. return {buffer + 2 + precision, std::errc()};
  1158. }
  1159. // precision means the number of decimal significand digits minus 1.
  1160. // Assumes round-to-nearest, tie-to-even rounding.
  1161. template <typename MainCache = main_cache_full, typename ExtendedCache>
  1162. BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision, char* first, char* last,
  1163. boost::charconv::chars_format fmt) noexcept
  1164. {
  1165. if (first >= last)
  1166. {
  1167. return {last, std::errc::value_too_large};
  1168. }
  1169. auto buffer_size = static_cast<std::size_t>(last - first);
  1170. auto buffer = first;
  1171. BOOST_CHARCONV_ASSERT(precision >= 0);
  1172. using namespace detail;
  1173. std::uint64_t br = default_float_traits<double>::float_to_carrier(x);
  1174. bool is_negative = ((br >> 63) != 0);
  1175. br <<= 1;
  1176. int e = static_cast<int>(br >> (ieee754_binary64::significand_bits + 1));
  1177. auto significand = (br & ((UINT64_C(1) << (ieee754_binary64::significand_bits + 1)) - 1)); // shifted by 1-bit.
  1178. if (is_negative)
  1179. {
  1180. *buffer = '-';
  1181. ++buffer;
  1182. --buffer_size;
  1183. if (buffer_size == 0)
  1184. {
  1185. return {buffer, std::errc::value_too_large};
  1186. }
  1187. }
  1188. // Infinities or NaN
  1189. if (e == ((UINT32_C(1) << ieee754_binary64::exponent_bits) - 1))
  1190. {
  1191. if (significand == 0)
  1192. {
  1193. constexpr std::size_t inf_chars = 3;
  1194. if (buffer_size < inf_chars)
  1195. {
  1196. return {last, std::errc::value_too_large};
  1197. }
  1198. std::memcpy(buffer, "inf", inf_chars); // NOLINT : Specifically not null-terminating
  1199. return {buffer + inf_chars, std::errc()};
  1200. }
  1201. else
  1202. {
  1203. // Significand values for NaN by type
  1204. // qNaN = 4503599627370496
  1205. // sNaN = 2251799813685248
  1206. //
  1207. if (significand == UINT64_C(4503599627370496))
  1208. {
  1209. if (!is_negative)
  1210. {
  1211. constexpr std::size_t nan_chars = 3;
  1212. if (buffer_size < nan_chars)
  1213. {
  1214. return {last, std::errc::value_too_large};
  1215. }
  1216. std::memcpy(buffer, "nan", nan_chars); // NOLINT : Specifically not null-terminating
  1217. return {buffer + nan_chars, std::errc()};
  1218. }
  1219. else
  1220. {
  1221. constexpr std::size_t neg_nan_chars = 8;
  1222. if (buffer_size < neg_nan_chars)
  1223. {
  1224. return {last, std::errc::value_too_large};
  1225. }
  1226. std::memcpy(buffer, "nan(ind)", neg_nan_chars); // NOLINT : Specifically not null-terminating
  1227. return {buffer + neg_nan_chars, std::errc()};
  1228. }
  1229. }
  1230. else
  1231. {
  1232. constexpr std::size_t snan_chars = 9;
  1233. if (buffer_size < snan_chars)
  1234. {
  1235. return {last, std::errc::value_too_large};
  1236. }
  1237. std::memcpy(buffer, "nan(snan)", snan_chars); // NOLINT : Specifically not null-terminating
  1238. return {buffer + snan_chars, std::errc()};
  1239. }
  1240. }
  1241. }
  1242. else
  1243. {
  1244. // Normal numbers.
  1245. if (e != 0)
  1246. {
  1247. significand |= (decltype(significand)(1) << (ieee754_binary64::significand_bits + 1));
  1248. e += (ieee754_binary64::exponent_bias - ieee754_binary64::significand_bits);
  1249. }
  1250. // Subnormal numbers.
  1251. else
  1252. {
  1253. // Zero
  1254. if (significand == 0)
  1255. {
  1256. if (fmt == boost::charconv::chars_format::general)
  1257. {
  1258. // For the case of chars_format::general, 0 is always printed as 0.
  1259. *buffer = '0';
  1260. return {buffer + 1, std::errc()};
  1261. }
  1262. else if (fmt == boost::charconv::chars_format::fixed)
  1263. {
  1264. return print_zero_fixed(buffer, buffer_size, precision);
  1265. }
  1266. // For the case of chars_format::scientific, print as many 0's as requested after the decimal dot, and then print e+00.
  1267. if (precision == 0)
  1268. {
  1269. constexpr std::size_t zero_chars = 5;
  1270. if (buffer_size < zero_chars)
  1271. {
  1272. return {last, std::errc::value_too_large};
  1273. }
  1274. std::memcpy(buffer, "0e+00", zero_chars);
  1275. return {buffer + zero_chars, std::errc()};
  1276. }
  1277. else
  1278. {
  1279. if (buffer_size < static_cast<std::size_t>(precision) + 6U)
  1280. {
  1281. return {last, std::errc::value_too_large};
  1282. }
  1283. std::memcpy(buffer, "0.", 2); // NOLINT : Specifically not null-terminating
  1284. std::memset(buffer + 2, '0', static_cast<std::size_t>(precision)); // NOLINT : Specifically not null-terminating
  1285. std::memcpy(buffer + 2 + precision, "e+00", 4); // NOLINT : Specifically not null-terminating
  1286. return {buffer + precision + 6, std::errc()};
  1287. }
  1288. }
  1289. // Nonzero
  1290. e = ieee754_binary64::min_exponent - ieee754_binary64::significand_bits;
  1291. }
  1292. }
  1293. constexpr int kappa = 2;
  1294. int k = kappa - log::floor_log10_pow2(e);
  1295. std::uint32_t current_digits {};
  1296. char* const buffer_starting_pos = buffer;
  1297. char* decimal_dot_pos = buffer; // decimal_dot_pos == buffer_starting_pos indicates that there should be no decimal dot.
  1298. int decimal_exponent_normalized {};
  1299. // Number of digits to be printed.
  1300. int remaining_digits {};
  1301. /////////////////////////////////////////////////////////////////////////////////////////////////
  1302. /// Phase 1 - Print the first digit segment computed with the Dragonbox table.
  1303. /////////////////////////////////////////////////////////////////////////////////////////////////
  1304. {
  1305. // Compute the first digit segment.
  1306. const auto main_cache = MainCache::template get_cache<ieee754_binary64>(k);
  1307. const int beta = e + log::floor_log2_pow10(k);
  1308. // Integer check is okay for binary64.
  1309. //auto [first_segment, has_more_segments]
  1310. compute_mul_result segments = [&] {
  1311. const auto r = umul192_upper128(significand << beta, main_cache);
  1312. return compute_mul_result{r.high, r.low == 0};
  1313. }();
  1314. auto first_segment = segments.result;
  1315. auto has_more_segments = !segments.is_integer;
  1316. // The first segment can be up to 19 digits. It is in fact always of either 18 or 19
  1317. // digits except when the input is a subnormal number. For subnormal numbers, the
  1318. // smallest possible value of the first segment is 10^kappa, so it is of at least
  1319. // kappa+1 digits (i.e., 3 in this case).
  1320. int first_segment_length = 19;
  1321. auto first_segment_aligned = first_segment; // Aligned to have 19 digits.
  1322. while (first_segment_aligned < UINT64_C(10000000000000000))
  1323. {
  1324. first_segment_aligned *= 100;
  1325. first_segment_length -= 2;
  1326. }
  1327. if (first_segment_aligned < UINT64_C(1000000000000000000))
  1328. {
  1329. first_segment_aligned *= 10;
  1330. first_segment_length -= 1;
  1331. }
  1332. // The decimal exponent when written as X.XXXX.... x 10^XX.
  1333. decimal_exponent_normalized = first_segment_length - k - 1;
  1334. // Figure out the correct value of remaining_digits.
  1335. if (fmt == boost::charconv::chars_format::scientific)
  1336. {
  1337. remaining_digits = precision + 1;
  1338. int exponent_print_length =
  1339. decimal_exponent_normalized >= 100 ? 5 :
  1340. decimal_exponent_normalized <= -100 ? 6 :
  1341. decimal_exponent_normalized >= 0 ? 4 : 5;
  1342. // No trailing decimal dot.
  1343. auto minimum_required_buffer_size =
  1344. static_cast<std::size_t>(remaining_digits + exponent_print_length + (precision != 0 ? 1 : 0));
  1345. if (buffer_size < minimum_required_buffer_size)
  1346. {
  1347. return {last, std::errc::value_too_large};
  1348. }
  1349. if (precision != 0)
  1350. {
  1351. // Reserve a place for the decimal dot.
  1352. *buffer = '0';
  1353. ++buffer;
  1354. ++decimal_dot_pos;
  1355. }
  1356. }
  1357. else if (fmt == boost::charconv::chars_format::fixed)
  1358. {
  1359. if (decimal_exponent_normalized >= 0)
  1360. {
  1361. remaining_digits = precision + decimal_exponent_normalized + 1;
  1362. // No trailing decimal dot.
  1363. auto minimum_required_buffer_size =
  1364. static_cast<std::size_t>(remaining_digits + (precision != 0 ? 1 : 0));
  1365. // We need one more space if the rounding changes the exponent,
  1366. // but since we don't know at this point if that will actually happen, handle such a case later.
  1367. if (buffer_size < minimum_required_buffer_size)
  1368. {
  1369. return {last, std::errc::value_too_large};
  1370. }
  1371. if (precision != 0)
  1372. {
  1373. // Reserve a place for the decimal dot.
  1374. *buffer = '0';
  1375. ++buffer;
  1376. decimal_dot_pos += decimal_exponent_normalized + 1;
  1377. }
  1378. }
  1379. else
  1380. {
  1381. int number_of_leading_zeros = -decimal_exponent_normalized - 1;
  1382. // When there are more than precision number of leading zeros,
  1383. // all the digits we need to print are 0.
  1384. if (number_of_leading_zeros > precision)
  1385. {
  1386. return print_zero_fixed(buffer, buffer_size, precision);
  1387. }
  1388. // When the number of leading zeros is exactly precision,
  1389. // then we might need to print 1 at the last digit due to rounding.
  1390. if (number_of_leading_zeros == precision)
  1391. {
  1392. // Since the last digit before rounding is 0,
  1393. // according to the "round-to-nearest, tie-to-even" rule, we round-up
  1394. // if and only if the input is strictly larger than the midpoint.
  1395. bool round_up = (first_segment_aligned + (has_more_segments ? 1 : 0)) > UINT64_C(5000000000000000000);
  1396. if (!round_up)
  1397. {
  1398. return print_zero_fixed(buffer, buffer_size, precision);
  1399. }
  1400. // No trailing decimal dot.
  1401. if (precision == 0)
  1402. {
  1403. *buffer = '1';
  1404. return {buffer + 1, std::errc()};
  1405. }
  1406. if (buffer_size < static_cast<std::size_t>(precision) + 2U)
  1407. {
  1408. return {buffer + buffer_size, std::errc::value_too_large};
  1409. }
  1410. std::memcpy(buffer, "0.", 2); // NOLINT : Specifically not null-terminating
  1411. std::memset(buffer + 2, '0', static_cast<std::size_t>(precision - 1)); // NOLINT : Specifically not null-terminating
  1412. buffer[1 + precision] = '1';
  1413. return {buffer + 2 + precision, std::errc()};
  1414. }
  1415. remaining_digits = precision - number_of_leading_zeros;
  1416. // Always have decimal dot.
  1417. BOOST_CHARCONV_ASSERT(precision > 0);
  1418. auto minimum_required_buffer_size = static_cast<std::size_t>(precision + 2);
  1419. if (buffer_size < minimum_required_buffer_size)
  1420. {
  1421. return {last, std::errc::value_too_large};
  1422. }
  1423. // Print leading zeros.
  1424. std::memset(buffer, '0', static_cast<std::size_t>(number_of_leading_zeros + 2));
  1425. buffer += number_of_leading_zeros + 2;
  1426. ++decimal_dot_pos;
  1427. }
  1428. }
  1429. else
  1430. {
  1431. // fmt == boost::charconv::chars_format::general
  1432. if (precision == 0)
  1433. {
  1434. // For general format, precision = 0 is interpreted as precision = 1.
  1435. precision = 1;
  1436. }
  1437. remaining_digits = precision;
  1438. // Use scientific format if decimal_exponent_normalized <= -6 or decimal_exponent_normalized >= precision.
  1439. // Use fixed format if -4 <= decimal_exponent_normalized <= precision - 2.
  1440. // If decimal_exponent_normalized == -5, use fixed format if and only if the rounding increases the exponent.
  1441. // If decimal_exponent_normalized == precision - 1, use scientific format if and only if the rounding increases the exponent.
  1442. // Since we cannot reliably decide which format to use, necessary corrections will be made in the last phase.
  1443. // We may end up not printing the decimal dot if fixed format is chosen, but reserve a place anyway.
  1444. *buffer = '0';
  1445. ++buffer;
  1446. decimal_dot_pos += (0 < decimal_exponent_normalized && decimal_exponent_normalized < precision)
  1447. ? decimal_exponent_normalized + 1 : 1;
  1448. }
  1449. if (remaining_digits <= 2)
  1450. {
  1451. uint128 prod;
  1452. std::uint64_t fractional_part64;
  1453. std::uint64_t fractional_part_rounding_threshold64;
  1454. // Convert to fixed-point form with 64/32-bit boundary for the fractional part.
  1455. if (remaining_digits == 1)
  1456. {
  1457. prod = umul128(first_segment_aligned, UINT64_C(1329227995784915873));
  1458. // ceil(2^63 + 2^64/10^18)
  1459. fractional_part_rounding_threshold64 = additional_static_data_holder::fractional_part_rounding_thresholds64[17];
  1460. }
  1461. else
  1462. {
  1463. prod = umul128(first_segment_aligned, UINT64_C(13292279957849158730));
  1464. // ceil(2^63 + 2^64/10^17)
  1465. fractional_part_rounding_threshold64 = additional_static_data_holder::
  1466. fractional_part_rounding_thresholds64[16];
  1467. }
  1468. fractional_part64 = (prod.low >> 56) | (prod.high << 8);
  1469. current_digits = static_cast<std::uint32_t>(prod.high >> 56);
  1470. // Perform rounding, print the digit, and return.
  1471. if (remaining_digits == 1)
  1472. {
  1473. if (fractional_part64 >= fractional_part_rounding_threshold64 ||
  1474. ((fractional_part64 >> 63) & (has_more_segments | (current_digits & 1))) != 0)
  1475. {
  1476. goto round_up_one_digit;
  1477. }
  1478. print_1_digit(current_digits, buffer);
  1479. ++buffer;
  1480. }
  1481. else
  1482. {
  1483. if (fractional_part64 >= fractional_part_rounding_threshold64 ||
  1484. ((fractional_part64 >> 63) & (has_more_segments | (current_digits & 1))) != 0)
  1485. {
  1486. goto round_up_two_digits;
  1487. }
  1488. print_2_digits(current_digits, buffer);
  1489. buffer += 2;
  1490. }
  1491. goto insert_decimal_dot;
  1492. } // remaining_digits <= 2
  1493. // At this point, there are at least 3 digits to print.
  1494. // We split the segment into three chunks, each consisting of 9 digits, 8 digits,
  1495. // and 2 digits.
  1496. // MSVC doesn't know how to do Grandlund-Montgomery for large 64-bit integers.
  1497. // 7922816251426433760 = ceil(2^96/10^10) = floor(2^96*(10^9/(10^19 - 1)))
  1498. const auto first_subsegment =
  1499. static_cast<std::uint32_t>(umul128_upper64(first_segment, UINT64_C(7922816251426433760)) >> 32);
  1500. const auto second_third_subsegments =
  1501. first_segment - first_subsegment * UINT64_C(10000000000);
  1502. BOOST_CHARCONV_ASSERT(first_subsegment < UINT64_C(1000000000));
  1503. BOOST_CHARCONV_ASSERT(second_third_subsegments < UINT64_C(10000000000));
  1504. int remaining_digits_in_the_current_subsegment;
  1505. std::uint64_t prod; // holds intermediate values for digit generation.
  1506. // Print the first subsegment.
  1507. if (first_subsegment != 0)
  1508. {
  1509. // 9 digits (19 digits in total).
  1510. if (first_subsegment >= 100000000)
  1511. {
  1512. // 1441151882 = ceil(2^57 / 10^8) + 1
  1513. prod = first_subsegment * UINT64_C(1441151882);
  1514. prod >>= 25;
  1515. remaining_digits_in_the_current_subsegment = 8;
  1516. }
  1517. // 7 or 8 digits (17 or 18 digits in total).
  1518. else if (first_subsegment >= 1000000)
  1519. {
  1520. // 281474978 = ceil(2^48 / 10^6) + 1
  1521. prod = first_subsegment * UINT64_C(281474978);
  1522. prod >>= 16;
  1523. remaining_digits_in_the_current_subsegment = 6;
  1524. }
  1525. // 5 or 6 digits (15 or 16 digits in total).
  1526. else if (first_subsegment >= 10000)
  1527. {
  1528. // 429497 = ceil(2^32 / 10^4)
  1529. prod = first_subsegment * UINT64_C(429497);
  1530. remaining_digits_in_the_current_subsegment = 4;
  1531. }
  1532. // 3 or 4 digits (13 or 14 digits in total).
  1533. else if (first_subsegment >= 100)
  1534. {
  1535. // 42949673 = ceil(2^32 / 10^2)
  1536. prod = first_subsegment * UINT64_C(42949673);
  1537. remaining_digits_in_the_current_subsegment = 2;
  1538. }
  1539. // 1 or 2 digits (11 or 12 digits in total).
  1540. else
  1541. {
  1542. prod = std::uint64_t(first_subsegment) << 32;
  1543. remaining_digits_in_the_current_subsegment = 0;
  1544. }
  1545. const auto initial_digits = static_cast<std::uint32_t>(prod >> 32);
  1546. buffer -= (initial_digits < 10 && buffer != first ? 1 : 0);
  1547. remaining_digits -= (2 - (initial_digits < 10 ? 1 : 0));
  1548. // Avoid the situation where we have a leading 0 that we don't need
  1549. // Typically used to account for inserting a decimal, but we know
  1550. // we won't need that in the 0 precision case
  1551. if (precision == 0 && initial_digits < 10)
  1552. {
  1553. print_1_digit(initial_digits, buffer);
  1554. ++buffer;
  1555. }
  1556. else
  1557. {
  1558. print_2_digits(initial_digits, buffer);
  1559. buffer += 2;
  1560. }
  1561. if (remaining_digits > remaining_digits_in_the_current_subsegment)
  1562. {
  1563. remaining_digits -= remaining_digits_in_the_current_subsegment;
  1564. for (; remaining_digits_in_the_current_subsegment > 0; remaining_digits_in_the_current_subsegment -= 2)
  1565. {
  1566. // Write next two digits.
  1567. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1568. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1569. buffer += 2;
  1570. }
  1571. }
  1572. else
  1573. {
  1574. for (int i = 0; i < (remaining_digits - 1) / 2; ++i)
  1575. {
  1576. // Write next two digits.
  1577. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1578. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1579. buffer += 2;
  1580. }
  1581. // Distinguish two cases of rounding.
  1582. if (remaining_digits_in_the_current_subsegment > remaining_digits)
  1583. {
  1584. if ((remaining_digits & 1) != 0)
  1585. {
  1586. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  1587. }
  1588. else
  1589. {
  1590. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1591. }
  1592. current_digits = static_cast<std::uint32_t>(prod >> 32);
  1593. if (check_rounding_condition_inside_subsegment(
  1594. current_digits, static_cast<std::uint32_t>(prod),
  1595. remaining_digits_in_the_current_subsegment - remaining_digits,
  1596. second_third_subsegments != 0 || has_more_segments))
  1597. {
  1598. goto round_up;
  1599. }
  1600. goto print_last_digits;
  1601. }
  1602. else
  1603. {
  1604. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1605. current_digits = static_cast<std::uint32_t>(prod >> 32);
  1606. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  1607. current_digits,
  1608. uint_with_known_number_of_digits<10>{second_third_subsegments},
  1609. has_more_segments))
  1610. {
  1611. goto round_up_two_digits;
  1612. }
  1613. goto print_last_two_digits;
  1614. }
  1615. }
  1616. }
  1617. // Print the second subsegment.
  1618. // The second subsegment cannot be zero even for subnormal numbers.
  1619. if (remaining_digits <= 2)
  1620. {
  1621. // In this case the first subsegment must be nonzero.
  1622. if (remaining_digits == 1)
  1623. {
  1624. const auto prod128 = umul128(second_third_subsegments, UINT64_C(18446744074));
  1625. current_digits = static_cast<std::uint32_t>(prod128.high);
  1626. const auto fractional_part64 = prod128.low + 1;
  1627. // 18446744074 is even, so prod.low cannot be equal to 2^64 - 1.
  1628. BOOST_CHARCONV_ASSERT(fractional_part64 != 0);
  1629. if (fractional_part64 >= additional_static_data_holder::fractional_part_rounding_thresholds64[8] ||
  1630. ((fractional_part64 >> 63) & (has_more_segments | (current_digits & 1))) != 0)
  1631. {
  1632. goto round_up_one_digit;
  1633. }
  1634. goto print_last_one_digit;
  1635. } // remaining_digits == 1
  1636. else
  1637. {
  1638. const auto prod128 = umul128(second_third_subsegments, UINT64_C(184467440738));
  1639. current_digits = static_cast<std::uint32_t>(prod128.high);
  1640. const auto fractional_part64 = prod128.low + 1;
  1641. // 184467440738 is even, so prod.low cannot be equal to 2^64 - 1.
  1642. BOOST_CHARCONV_ASSERT(fractional_part64 != 0);
  1643. if (fractional_part64 >= additional_static_data_holder::fractional_part_rounding_thresholds64[7] ||
  1644. ((fractional_part64 >> 63) & (has_more_segments | (current_digits & 1))) != 0)
  1645. {
  1646. goto round_up_two_digits;
  1647. }
  1648. goto print_last_two_digits;
  1649. }
  1650. } // remaining_digits <= 2
  1651. // Compilers are not aware of how to leverage the maximum value of
  1652. // second_third_subsegments to find out a better magic number which allows us to
  1653. // eliminate an additional shift.
  1654. // 184467440737095517 = ceil(2^64/100) < floor(2^64*(10^8/(10^10 - 1))).
  1655. const auto second_subsegment = static_cast<std::uint32_t>(
  1656. umul128_upper64(second_third_subsegments, UINT64_C(184467440737095517)));
  1657. // Since the final result is of 2 digits, we can do the computation in 32-bits.
  1658. const auto third_subsegment =
  1659. static_cast<std::uint32_t>(second_third_subsegments) - second_subsegment * 100;
  1660. BOOST_CHARCONV_ASSERT(second_subsegment < 100000000);
  1661. BOOST_CHARCONV_ASSERT(third_subsegment < 100);
  1662. {
  1663. std::uint32_t initial_digits;
  1664. if (first_subsegment != 0)
  1665. {
  1666. prod = ((second_subsegment * UINT64_C(281474977)) >> 16) + 1;
  1667. remaining_digits_in_the_current_subsegment = 6;
  1668. initial_digits = static_cast<std::uint32_t>(prod >> 32);
  1669. remaining_digits -= 2;
  1670. }
  1671. else
  1672. {
  1673. // 7 or 8 digits (9 or 10 digits in total).
  1674. if (second_subsegment >= 1000000)
  1675. {
  1676. prod = (second_subsegment * UINT64_C(281474978)) >> 16;
  1677. remaining_digits_in_the_current_subsegment = 6;
  1678. }
  1679. // 5 or 6 digits (7 or 8 digits in total).
  1680. else if (second_subsegment >= 10000)
  1681. {
  1682. prod = second_subsegment * UINT64_C(429497);
  1683. remaining_digits_in_the_current_subsegment = 4;
  1684. }
  1685. // 3 or 4 digits (5 or 6 digits in total).
  1686. else if (second_subsegment >= 100)
  1687. {
  1688. prod = second_subsegment * UINT64_C(42949673);
  1689. remaining_digits_in_the_current_subsegment = 2;
  1690. }
  1691. // 1 or 2 digits (3 or 4 digits in total).
  1692. else
  1693. {
  1694. prod = std::uint64_t(second_subsegment) << 32;
  1695. remaining_digits_in_the_current_subsegment = 0;
  1696. }
  1697. initial_digits = static_cast<std::uint32_t>(prod >> 32);
  1698. buffer -= (initial_digits < 10 ? 1 : 0);
  1699. remaining_digits -= (2 - (initial_digits < 10 ? 1 : 0));
  1700. }
  1701. print_2_digits(initial_digits, buffer);
  1702. buffer += 2;
  1703. if (remaining_digits > remaining_digits_in_the_current_subsegment)
  1704. {
  1705. remaining_digits -= remaining_digits_in_the_current_subsegment;
  1706. for (; remaining_digits_in_the_current_subsegment > 0; remaining_digits_in_the_current_subsegment -= 2)
  1707. {
  1708. // Write next two digits.
  1709. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1710. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1711. buffer += 2;
  1712. }
  1713. }
  1714. else
  1715. {
  1716. for (int i = 0; i < (remaining_digits - 1) / 2; ++i)
  1717. {
  1718. // Write next two digits.
  1719. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1720. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1721. buffer += 2;
  1722. }
  1723. // Distinguish two cases of rounding.
  1724. if (remaining_digits_in_the_current_subsegment > remaining_digits)
  1725. {
  1726. if ((remaining_digits & 1) != 0)
  1727. {
  1728. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  1729. }
  1730. else
  1731. {
  1732. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1733. }
  1734. current_digits = static_cast<std::uint32_t>(prod >> 32);
  1735. if (check_rounding_condition_inside_subsegment(
  1736. current_digits, static_cast<std::uint32_t>(prod),
  1737. remaining_digits_in_the_current_subsegment - remaining_digits,
  1738. third_subsegment != 0 || has_more_segments))
  1739. {
  1740. goto round_up;
  1741. }
  1742. goto print_last_digits;
  1743. }
  1744. else
  1745. {
  1746. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1747. current_digits = static_cast<std::uint32_t>(prod >> 32);
  1748. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  1749. current_digits,
  1750. uint_with_known_number_of_digits<2>{third_subsegment},
  1751. has_more_segments))
  1752. {
  1753. goto round_up_two_digits;
  1754. }
  1755. goto print_last_two_digits;
  1756. }
  1757. }
  1758. }
  1759. // Print the third subsegment.
  1760. {
  1761. if (remaining_digits > 2)
  1762. {
  1763. print_2_digits(third_subsegment, buffer);
  1764. buffer += 2;
  1765. remaining_digits -= 2;
  1766. // If there is no more segment, then fill remaining digits with 0's and return.
  1767. if (!has_more_segments)
  1768. {
  1769. goto fill_remaining_digits_with_0s;
  1770. }
  1771. }
  1772. else if (remaining_digits == 1)
  1773. {
  1774. prod = third_subsegment * UINT64_C(429496730);
  1775. current_digits = static_cast<std::uint32_t>(prod >> 32);
  1776. if (check_rounding_condition_inside_subsegment(
  1777. current_digits, static_cast<std::uint32_t>(prod), 1, has_more_segments))
  1778. {
  1779. goto round_up_one_digit;
  1780. }
  1781. goto print_last_one_digit;
  1782. }
  1783. else
  1784. {
  1785. // remaining_digits == 2.
  1786. // If there is no more segment, then print the current two digits and return.
  1787. if (!has_more_segments)
  1788. {
  1789. print_2_digits(third_subsegment, buffer);
  1790. buffer += 2;
  1791. goto insert_decimal_dot;
  1792. }
  1793. // Otherwise, for performing the rounding, we have to wait until the next
  1794. // segment becomes available. This state can be detected afterward by
  1795. // inspecting if remaining_digits == 0.
  1796. remaining_digits = 0;
  1797. current_digits = third_subsegment;
  1798. }
  1799. }
  1800. }
  1801. /////////////////////////////////////////////////////////////////////////////////////////////////
  1802. /// Phase 2 - Print further digit segments computed with the extended cache table.
  1803. /////////////////////////////////////////////////////////////////////////////////////////////////
  1804. {
  1805. auto multiplier_index =
  1806. static_cast<std::uint32_t>(k + ExtendedCache::segment_length - ExtendedCache::k_min) /
  1807. static_cast<std::uint32_t>(ExtendedCache::segment_length);
  1808. int digits_in_the_second_segment;
  1809. {
  1810. const auto new_k =
  1811. ExtendedCache::k_min + static_cast<int>(multiplier_index) * ExtendedCache::segment_length;
  1812. digits_in_the_second_segment = new_k - k;
  1813. k = new_k;
  1814. }
  1815. const auto exp2_base = e + boost::core::countr_zero(significand);
  1816. using cache_block_type = typename std::decay<decltype(ExtendedCache::cache[0])>::type;
  1817. cache_block_type blocks[ExtendedCache::max_cache_blocks];
  1818. cache_block_count_t<ExtendedCache::constant_block_count, ExtendedCache::max_cache_blocks> cache_block_count;
  1819. // Deal with the second segment. The second segment is special because it can have
  1820. // overlapping digits with the first segment. Note that we cannot just move the buffer
  1821. // pointer backward and print the whole segment from there, because it may contain
  1822. // leading zeros.
  1823. {
  1824. cache_block_count =
  1825. load_extended_cache<ExtendedCache, ExtendedCache::constant_block_count>(
  1826. blocks, e, k, multiplier_index);
  1827. // Compute nm mod 2^Q.
  1828. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(significand, blocks, cache_block_count);
  1829. BOOST_CHARCONV_IF_CONSTEXPR (ExtendedCache::segment_length == 22)
  1830. {
  1831. // No rounding, continue.
  1832. if (remaining_digits > digits_in_the_second_segment)
  1833. {
  1834. remaining_digits -= digits_in_the_second_segment;
  1835. if (digits_in_the_second_segment <= 2)
  1836. {
  1837. BOOST_CHARCONV_ASSERT(digits_in_the_second_segment != 0);
  1838. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(
  1839. power_of_10[19], blocks, cache_block_count);
  1840. auto subsegment =
  1841. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  1842. generate_and_discard_lower(power_of_10[3], blocks,
  1843. cache_block_count);
  1844. if (digits_in_the_second_segment == 1)
  1845. {
  1846. auto prod = subsegment * UINT64_C(429496730);
  1847. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  1848. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  1849. ++buffer;
  1850. }
  1851. else
  1852. {
  1853. auto prod = subsegment * UINT64_C(42949673);
  1854. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1855. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1856. buffer += 2;
  1857. }
  1858. } // digits_in_the_second_segment <= 2
  1859. else if (digits_in_the_second_segment <= 16)
  1860. {
  1861. BOOST_CHARCONV_ASSERT(22 - digits_in_the_second_segment <= 19);
  1862. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(
  1863. compute_power(UINT64_C(10), 22 - digits_in_the_second_segment),
  1864. blocks, cache_block_count);
  1865. // When there are at most 9 digits, we can store them in 32-bits.
  1866. if (digits_in_the_second_segment <= 9)
  1867. {
  1868. // The number of overlapping digits is in the range 13 ~ 19.
  1869. const auto subsegment =
  1870. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  1871. generate_and_discard_lower(power_of_10[9], blocks,
  1872. cache_block_count);
  1873. std::uint64_t prod;
  1874. if ((digits_in_the_second_segment & 1) != 0)
  1875. {
  1876. prod = ((subsegment * UINT64_C(720575941)) >> 24) + 1;
  1877. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  1878. ++buffer;
  1879. }
  1880. else
  1881. {
  1882. prod = ((subsegment * UINT64_C(450359963)) >> 20) + 1;
  1883. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1884. buffer += 2;
  1885. }
  1886. for (; digits_in_the_second_segment > 2; digits_in_the_second_segment -= 2)
  1887. {
  1888. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1889. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1890. buffer += 2;
  1891. }
  1892. } // digits_in_the_second_segment <= 9
  1893. else
  1894. {
  1895. // The number of digits in the segment is in the range 10 ~ 16.
  1896. const auto first_second_subsegments =
  1897. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  1898. generate_and_discard_lower(power_of_10[16], blocks,
  1899. cache_block_count);
  1900. // The first segment is of 8 digits, and the second segment is of
  1901. // 2 ~ 8 digits.
  1902. // ceil(2^(64+14)/10^8) = 3022314549036573
  1903. // = floor(2^(64+14)*(10^8/(10^16 - 1)))
  1904. const auto first_subsegment =
  1905. static_cast<std::uint32_t>(umul128_upper64(first_second_subsegments,
  1906. UINT64_C(3022314549036573)) >>
  1907. 14);
  1908. const auto second_subsegment =
  1909. static_cast<std::uint32_t>(first_second_subsegments) -
  1910. UINT32_C(100000000) * first_subsegment;
  1911. // Print the first subsegment.
  1912. print_8_digits(first_subsegment, buffer);
  1913. buffer += 8;
  1914. // Print the second subsegment.
  1915. // There are at least 2 digits in the second subsegment.
  1916. auto prod = ((second_subsegment * UINT64_C(140737489)) >> 15) + 1;
  1917. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1918. buffer += 2;
  1919. digits_in_the_second_segment -= 10;
  1920. for (; digits_in_the_second_segment > 1; digits_in_the_second_segment -= 2)
  1921. {
  1922. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1923. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1924. buffer += 2;
  1925. }
  1926. if (digits_in_the_second_segment != 0)
  1927. {
  1928. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  1929. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  1930. ++buffer;
  1931. }
  1932. }
  1933. } // digits_in_the_second_segment <= 16
  1934. else
  1935. {
  1936. // The number of digits in the segment is in the range 17 ~ 22.
  1937. const auto first_subsegment =
  1938. fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(
  1939. power_of_10[6], blocks, cache_block_count);
  1940. const auto second_third_subsegments =
  1941. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  1942. generate_and_discard_lower(power_of_10[16], blocks,
  1943. cache_block_count);
  1944. // ceil(2^(64+14)/10^8) = 3022314549036573
  1945. // = floor(2^(64+14)*(10^8/(10^16 - 1)))
  1946. const auto second_subsegment =
  1947. static_cast<std::uint32_t>(umul128_upper64(second_third_subsegments,
  1948. UINT64_C(3022314549036573)) >>
  1949. 14);
  1950. const auto third_subsegment = static_cast<std::uint32_t>(second_third_subsegments) -
  1951. UINT32_C(100000000) * second_subsegment;
  1952. // Print the first subsegment (1 ~ 6 digits).
  1953. std::uint64_t prod {};
  1954. auto remaining_digits_in_the_current_subsegment =
  1955. digits_in_the_second_segment - 16;
  1956. switch (remaining_digits_in_the_current_subsegment)
  1957. {
  1958. case 1:
  1959. prod = first_subsegment * UINT64_C(429496730);
  1960. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_odd_remaining;
  1961. case 2:
  1962. prod = first_subsegment * UINT64_C(42949673);
  1963. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_even_remaining;
  1964. case 3:
  1965. prod = first_subsegment * UINT64_C(4294968);
  1966. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_odd_remaining;
  1967. case 4:
  1968. prod = first_subsegment * UINT64_C(429497);
  1969. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_even_remaining;
  1970. case 5:
  1971. prod = ((first_subsegment * UINT64_C(687195)) >> 4) + 1;
  1972. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_odd_remaining;
  1973. case 6:
  1974. prod = first_subsegment * UINT64_C(429497);
  1975. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1976. buffer += 2;
  1977. remaining_digits_in_the_current_subsegment = 4;
  1978. goto second_segment22_more_than_16_digits_first_subsegment_no_rounding_even_remaining;
  1979. default:
  1980. BOOST_UNREACHABLE_RETURN(prod); // NOLINT
  1981. }
  1982. second_segment22_more_than_16_digits_first_subsegment_no_rounding_odd_remaining
  1983. :
  1984. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  1985. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  1986. ++buffer;
  1987. second_segment22_more_than_16_digits_first_subsegment_no_rounding_even_remaining
  1988. :
  1989. for (; remaining_digits_in_the_current_subsegment > 1;
  1990. remaining_digits_in_the_current_subsegment -= 2)
  1991. {
  1992. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  1993. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  1994. buffer += 2;
  1995. }
  1996. // Print the second and third subsegments (8 digits each).
  1997. print_8_digits(second_subsegment, buffer);
  1998. print_8_digits(third_subsegment, buffer + 8);
  1999. buffer += 16;
  2000. }
  2001. } // remaining_digits > digits_in_the_second_segment
  2002. // Perform rounding and return.
  2003. else
  2004. {
  2005. if (digits_in_the_second_segment <= 2)
  2006. {
  2007. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(
  2008. power_of_10[19], blocks, cache_block_count);
  2009. // Get one more bit for potential rounding on the segment boundary.
  2010. auto subsegment =
  2011. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  2012. generate_and_discard_lower(2000, blocks, cache_block_count);
  2013. bool segment_boundary_rounding_bit = ((subsegment & 1) != 0);
  2014. subsegment >>= 1;
  2015. if (digits_in_the_second_segment == 2)
  2016. {
  2017. // Convert subsegment into fixed-point fractional form where the
  2018. // integer part is of one digit. The integer part is ignored.
  2019. // 42949673 = ceil(2^32/10^2)
  2020. auto prod = static_cast<std::uint64_t>(subsegment) * UINT64_C(42949673);
  2021. if (remaining_digits == 1)
  2022. {
  2023. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2024. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2025. const bool has_further_digits_v = has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0);
  2026. if (check_rounding_condition_inside_subsegment(current_digits, static_cast<std::uint32_t>(prod), 1, has_further_digits_v))
  2027. {
  2028. goto round_up_one_digit;
  2029. }
  2030. goto print_last_one_digit;
  2031. }
  2032. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2033. const auto next_digits = static_cast<std::uint32_t>(prod >> 32);
  2034. if (remaining_digits == 0)
  2035. {
  2036. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2037. current_digits,
  2038. uint_with_known_number_of_digits<2>{next_digits},
  2039. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  2040. {
  2041. goto round_up_two_digits;
  2042. }
  2043. goto print_last_two_digits;
  2044. }
  2045. current_digits = next_digits;
  2046. BOOST_CHARCONV_ASSERT(remaining_digits == 2);
  2047. }
  2048. else
  2049. {
  2050. BOOST_CHARCONV_ASSERT(digits_in_the_second_segment == 1);
  2051. // Convert subsegment into fixed-point fractional form where the
  2052. // integer part is of two digits. The integer part is ignored.
  2053. // 429496730 = ceil(2^32/10^1)
  2054. auto prod = static_cast<std::uint64_t>(subsegment) * UINT64_C(429496730);
  2055. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2056. const auto next_digits = static_cast<std::uint32_t>(prod >> 32);
  2057. if (remaining_digits == 0)
  2058. {
  2059. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2060. current_digits,
  2061. uint_with_known_number_of_digits<1>{next_digits},
  2062. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  2063. {
  2064. goto round_up_two_digits;
  2065. }
  2066. goto print_last_two_digits;
  2067. }
  2068. current_digits = next_digits;
  2069. BOOST_CHARCONV_ASSERT(remaining_digits == 1);
  2070. }
  2071. if (check_rounding_condition_with_next_bit(
  2072. current_digits, segment_boundary_rounding_bit,
  2073. has_further_digits<0, 0, ExtendedCache>(significand, exp2_base, k, uconst0, uconst0)))
  2074. {
  2075. goto round_up;
  2076. }
  2077. goto print_last_digits;
  2078. } // digits_in_the_second_segment <= 2
  2079. // When there are at most 9 digits in the segment.
  2080. if (digits_in_the_second_segment <= 9)
  2081. {
  2082. // Throw away all overlapping digits.
  2083. BOOST_CHARCONV_ASSERT(22 - digits_in_the_second_segment <= 19);
  2084. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(
  2085. compute_power(UINT64_C(10), 22 - digits_in_the_second_segment),
  2086. blocks, cache_block_count);
  2087. // Get one more bit for potential rounding on the segment boundary.
  2088. auto segment = fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  2089. generate_and_discard_lower(power_of_10[9] << 1, blocks,
  2090. cache_block_count);
  2091. std::uint64_t prod;
  2092. digits_in_the_second_segment -= remaining_digits;
  2093. if ((remaining_digits & 1) != 0)
  2094. {
  2095. prod = ((segment * UINT64_C(1441151881)) >> 26) + 1;
  2096. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2097. if (remaining_digits == 1)
  2098. {
  2099. goto second_segment22_at_most_9_digits_rounding;
  2100. }
  2101. print_1_digit(current_digits, buffer);
  2102. ++buffer;
  2103. }
  2104. else
  2105. {
  2106. prod = ((segment * UINT64_C(1801439851)) >> 23) + 1;
  2107. const auto next_digits = static_cast<std::uint32_t>(prod >> 32);
  2108. if (remaining_digits == 0)
  2109. {
  2110. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2111. current_digits,
  2112. uint_with_known_number_of_digits<2>{next_digits}, [&] {
  2113. return static_cast<std::uint32_t>(prod) >=
  2114. (additional_static_data_holder::
  2115. fractional_part_rounding_thresholds32[digits_in_the_second_segment - 3] & UINT32_C(0x7fffffff))
  2116. || has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0);
  2117. }))
  2118. {
  2119. goto round_up_two_digits;
  2120. }
  2121. goto print_last_two_digits;
  2122. }
  2123. else if (remaining_digits == 2)
  2124. {
  2125. current_digits = next_digits;
  2126. goto second_segment22_at_most_9_digits_rounding;
  2127. }
  2128. print_2_digits(next_digits, buffer);
  2129. buffer += 2;
  2130. }
  2131. BOOST_CHARCONV_ASSERT(remaining_digits >= 3);
  2132. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2133. {
  2134. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2135. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2136. buffer += 2;
  2137. }
  2138. if (digits_in_the_second_segment != 0)
  2139. {
  2140. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2141. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2142. remaining_digits = 0;
  2143. second_segment22_at_most_9_digits_rounding:
  2144. if (check_rounding_condition_inside_subsegment(
  2145. current_digits, static_cast<std::uint32_t>(prod),
  2146. digits_in_the_second_segment, has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1,
  2147. uconst0)))
  2148. {
  2149. goto round_up;
  2150. }
  2151. goto print_last_digits;
  2152. }
  2153. else
  2154. {
  2155. prod = static_cast<std::uint32_t>(prod) * UINT64_C(200);
  2156. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2157. const auto segment_boundary_rounding_bit = (current_digits & 1) != 0;
  2158. current_digits >>= 1;
  2159. if (check_rounding_condition_with_next_bit(
  2160. current_digits, segment_boundary_rounding_bit,
  2161. has_further_digits<0, 1, ExtendedCache>(significand, exp2_base, k, uconst0, uconst1)))
  2162. {
  2163. goto round_up_two_digits;
  2164. }
  2165. goto print_last_two_digits;
  2166. }
  2167. } // digits_in_the_second_segment <= 9
  2168. // first_second_subsegments is of 1 ~ 13 digits, and third_subsegment is
  2169. // of 9 digits.
  2170. // Get one more bit for potential rounding condition check.
  2171. auto first_second_subsegments =
  2172. fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(
  2173. power_of_10[13] << 1, blocks, cache_block_count);
  2174. bool first_bit_of_third_subsegment = ((first_second_subsegments & 1) != 0);
  2175. first_second_subsegments >>= 1;
  2176. // Compilers are not aware of how to leverage the maximum value of
  2177. // first_second_subsegments to find out a better magic number which
  2178. // allows us to eliminate an additional shift.
  2179. // 1844674407371 = ceil(2^64/10^7) = floor(2^64*(10^6/(10^13 - 1))).
  2180. const auto first_subsegment =
  2181. static_cast<std::uint32_t>(boost::charconv::detail::umul128_upper64(
  2182. first_second_subsegments, 1844674407371));
  2183. const auto second_subsegment =
  2184. static_cast<std::uint32_t>(first_second_subsegments) - 10000000 * first_subsegment;
  2185. int digits_in_the_second_subsegment;
  2186. // Print the first subsegment (0 ~ 6 digits) if exists.
  2187. if (digits_in_the_second_segment > 16)
  2188. {
  2189. std::uint64_t prod;
  2190. int remaining_digits_in_the_current_subsegment = digits_in_the_second_segment - 16;
  2191. // No rounding, continue.
  2192. if (remaining_digits > remaining_digits_in_the_current_subsegment)
  2193. {
  2194. remaining_digits -= remaining_digits_in_the_current_subsegment;
  2195. // There is no overlap in the second subsegment.
  2196. digits_in_the_second_subsegment = 7;
  2197. // When there is no overlapping digit.
  2198. if (remaining_digits_in_the_current_subsegment == 6)
  2199. {
  2200. prod = (first_subsegment * UINT64_C(429497)) + 1;
  2201. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2202. buffer += 2;
  2203. remaining_digits_in_the_current_subsegment -= 2;
  2204. }
  2205. // If there are overlapping digits, move all overlapping digits
  2206. // into the integer part.
  2207. else
  2208. {
  2209. prod = ((first_subsegment * UINT64_C(687195)) >> 4) + 1;
  2210. prod *= compute_power(UINT64_C(10), 5 - remaining_digits_in_the_current_subsegment);
  2211. if ((remaining_digits_in_the_current_subsegment & 1) != 0)
  2212. {
  2213. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2214. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  2215. ++buffer;
  2216. }
  2217. }
  2218. for (; remaining_digits_in_the_current_subsegment > 1; remaining_digits_in_the_current_subsegment -= 2)
  2219. {
  2220. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2221. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2222. buffer += 2;
  2223. }
  2224. }
  2225. // The first subsegment is the last subsegment to print.
  2226. else
  2227. {
  2228. if ((remaining_digits & 1) != 0)
  2229. {
  2230. prod = ((first_subsegment * UINT64_C(687195)) >> 4) + 1;
  2231. // If there are overlapping digits, move all overlapping digits
  2232. // into the integer part and then get the next digit.
  2233. if (remaining_digits_in_the_current_subsegment < 6)
  2234. {
  2235. prod *= compute_power(UINT64_C(10), 5 - remaining_digits_in_the_current_subsegment);
  2236. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2237. }
  2238. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2239. remaining_digits_in_the_current_subsegment -= remaining_digits;
  2240. if (remaining_digits == 1)
  2241. {
  2242. goto second_segment22_more_than_9_digits_first_subsegment_rounding;
  2243. }
  2244. print_1_digit(current_digits, buffer);
  2245. ++buffer;
  2246. }
  2247. else
  2248. {
  2249. // When there is no overlapping digit.
  2250. if (remaining_digits_in_the_current_subsegment == 6)
  2251. {
  2252. if (remaining_digits == 0)
  2253. {
  2254. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2255. current_digits,
  2256. uint_with_known_number_of_digits<6>{
  2257. first_subsegment},
  2258. has_further_digits<1, 16, ExtendedCache>(significand, exp2_base, k, uconst1, uconst16)))
  2259. {
  2260. goto round_up_two_digits;
  2261. }
  2262. goto print_last_two_digits;
  2263. }
  2264. prod = (first_subsegment * UINT64_C(429497)) + 1;
  2265. }
  2266. // Otherwise, convert the subsegment into a fixed-point
  2267. // fraction form, move all overlapping digits into the
  2268. // integer part, and then extract the next two digits.
  2269. else
  2270. {
  2271. prod = ((first_subsegment * UINT64_C(687195)) >> 4) + 1;
  2272. prod *= compute_power(UINT64_C(10), 5 - remaining_digits_in_the_current_subsegment);
  2273. if (remaining_digits == 0)
  2274. {
  2275. goto second_segment22_more_than_9_digits_first_subsegment_rounding_inside_subsegment;
  2276. }
  2277. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2278. }
  2279. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2280. remaining_digits_in_the_current_subsegment -= remaining_digits;
  2281. if (remaining_digits == 2)
  2282. {
  2283. goto second_segment22_more_than_9_digits_first_subsegment_rounding;
  2284. }
  2285. print_2_digits(current_digits, buffer);
  2286. buffer += 2;
  2287. }
  2288. BOOST_CHARCONV_ASSERT(remaining_digits >= 3);
  2289. if (remaining_digits > 4)
  2290. {
  2291. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2292. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2293. buffer += 2;
  2294. }
  2295. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2296. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2297. remaining_digits = 0;
  2298. second_segment22_more_than_9_digits_first_subsegment_rounding:
  2299. if (remaining_digits_in_the_current_subsegment == 0)
  2300. {
  2301. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2302. current_digits,
  2303. uint_with_known_number_of_digits<7>{second_subsegment},
  2304. has_further_digits<1, 9, ExtendedCache>(significand, exp2_base, k, uconst1, uconst9)))
  2305. {
  2306. goto round_up;
  2307. }
  2308. }
  2309. else
  2310. {
  2311. second_segment22_more_than_9_digits_first_subsegment_rounding_inside_subsegment
  2312. :
  2313. if (check_rounding_condition_inside_subsegment(
  2314. current_digits, static_cast<std::uint32_t>(prod),
  2315. remaining_digits_in_the_current_subsegment,
  2316. has_further_digits<1, 16, ExtendedCache>(significand, exp2_base, k, uconst1, uconst16)))
  2317. {
  2318. goto round_up;
  2319. }
  2320. }
  2321. goto print_last_digits;
  2322. }
  2323. }
  2324. else
  2325. {
  2326. digits_in_the_second_subsegment = digits_in_the_second_segment - 9;
  2327. }
  2328. // Print the second subsegment (1 ~ 7 digits).
  2329. {
  2330. // No rounding, continue.
  2331. if (remaining_digits > digits_in_the_second_subsegment)
  2332. {
  2333. auto prod = ((second_subsegment * UINT64_C(17592187)) >> 12) + 1;
  2334. remaining_digits -= digits_in_the_second_subsegment;
  2335. // When there is no overlapping digit.
  2336. if (digits_in_the_second_subsegment == 7)
  2337. {
  2338. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  2339. ++buffer;
  2340. }
  2341. // If there are overlapping digits, move all overlapping digits
  2342. // into the integer part.
  2343. else
  2344. {
  2345. prod *= compute_power(UINT64_C(10),
  2346. 6 - digits_in_the_second_subsegment);
  2347. if ((digits_in_the_second_subsegment & 1) != 0)
  2348. {
  2349. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2350. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  2351. ++buffer;
  2352. }
  2353. }
  2354. for (; digits_in_the_second_subsegment > 1; digits_in_the_second_subsegment -= 2)
  2355. {
  2356. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2357. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2358. buffer += 2;
  2359. }
  2360. }
  2361. // The second subsegment is the last subsegment to print.
  2362. else
  2363. {
  2364. std::uint64_t prod;
  2365. if ((remaining_digits & 1) != 0)
  2366. {
  2367. prod = ((second_subsegment * UINT64_C(17592187)) >> 12) + 1;
  2368. // If there are overlapping digits, move all overlapping digits
  2369. // into the integer part and then get the next digit.
  2370. if (digits_in_the_second_subsegment < 7)
  2371. {
  2372. prod *= compute_power(UINT64_C(10), 6 - digits_in_the_second_subsegment);
  2373. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2374. }
  2375. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2376. digits_in_the_second_subsegment -= remaining_digits;
  2377. if (remaining_digits == 1)
  2378. {
  2379. goto second_segment22_more_than_9_digits_second_subsegment_rounding;
  2380. }
  2381. print_1_digit(current_digits, buffer);
  2382. ++buffer;
  2383. }
  2384. else
  2385. {
  2386. // When there is no overlapping digit.
  2387. if (digits_in_the_second_subsegment == 7)
  2388. {
  2389. if (remaining_digits == 0)
  2390. {
  2391. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2392. current_digits,
  2393. uint_with_known_number_of_digits<7>{
  2394. second_subsegment},
  2395. has_further_digits<1, 9, ExtendedCache>(significand, exp2_base, k, uconst1, uconst9)))
  2396. {
  2397. goto round_up_two_digits;
  2398. }
  2399. goto print_last_two_digits;
  2400. }
  2401. prod = ((second_subsegment * UINT64_C(10995117)) >> 8) + 1;
  2402. }
  2403. // Otherwise, convert the subsegment into a fixed-point
  2404. // fraction form, move all overlapping digits into the
  2405. // integer part, and then extract the next two digits.
  2406. else
  2407. {
  2408. prod = ((second_subsegment * UINT64_C(17592187)) >> 12) + 1;
  2409. prod *= compute_power(UINT64_C(10), 6 - digits_in_the_second_subsegment);
  2410. if (remaining_digits == 0)
  2411. {
  2412. goto second_segment22_more_than_9_digits_second_subsegment_rounding_inside_subsegment;
  2413. }
  2414. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2415. }
  2416. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2417. digits_in_the_second_subsegment -= remaining_digits;
  2418. if (remaining_digits == 2)
  2419. {
  2420. goto second_segment22_more_than_9_digits_second_subsegment_rounding;
  2421. }
  2422. print_2_digits(current_digits, buffer);
  2423. buffer += 2;
  2424. }
  2425. BOOST_CHARCONV_ASSERT(remaining_digits >= 3);
  2426. if (remaining_digits > 4)
  2427. {
  2428. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2429. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2430. buffer += 2;
  2431. }
  2432. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2433. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2434. remaining_digits = 0;
  2435. second_segment22_more_than_9_digits_second_subsegment_rounding:
  2436. if (digits_in_the_second_subsegment == 0)
  2437. {
  2438. if (check_rounding_condition_with_next_bit(
  2439. current_digits, first_bit_of_third_subsegment,
  2440. has_further_digits<0, 9, ExtendedCache>(significand, exp2_base, k, uconst0, uconst9)))
  2441. {
  2442. goto round_up;
  2443. }
  2444. }
  2445. else
  2446. {
  2447. second_segment22_more_than_9_digits_second_subsegment_rounding_inside_subsegment
  2448. :
  2449. if (check_rounding_condition_inside_subsegment(
  2450. current_digits, static_cast<std::uint32_t>(prod),
  2451. digits_in_the_second_subsegment, has_further_digits<1, 9, ExtendedCache>(significand, exp2_base, k,
  2452. uconst1, uconst9)))
  2453. {
  2454. goto round_up;
  2455. }
  2456. }
  2457. goto print_last_digits;
  2458. }
  2459. }
  2460. // Print the third subsegment (9 digits).
  2461. {
  2462. // Get one more bit if we need to check rounding conditions on
  2463. // the segment boundary. We already have shifted by 1-bit in the
  2464. // computation of first & second subsegments, so here we don't
  2465. // shift the multiplier.
  2466. auto third_subsegment =
  2467. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  2468. generate_and_discard_lower(power_of_10[9], blocks,
  2469. cache_block_count);
  2470. bool segment_boundary_rounding_bit = ((third_subsegment & 1) != 0);
  2471. third_subsegment >>= 1;
  2472. third_subsegment += (first_bit_of_third_subsegment ? 500000000 : 0);
  2473. std::uint64_t prod;
  2474. if ((remaining_digits & 1) != 0)
  2475. {
  2476. prod = ((third_subsegment * UINT64_C(720575941)) >> 24) + 1;
  2477. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2478. if (remaining_digits == 1)
  2479. {
  2480. if (check_rounding_condition_inside_subsegment(
  2481. current_digits, static_cast<std::uint32_t>(prod), 8,
  2482. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  2483. {
  2484. goto round_up_one_digit;
  2485. }
  2486. goto print_last_one_digit;
  2487. }
  2488. print_1_digit(current_digits, buffer);
  2489. ++buffer;
  2490. }
  2491. else
  2492. {
  2493. prod = ((third_subsegment * UINT64_C(450359963)) >> 20) + 1;
  2494. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2495. if (remaining_digits == 2)
  2496. {
  2497. goto second_segment22_more_than_9_digits_third_subsegment_rounding;
  2498. }
  2499. print_2_digits(current_digits, buffer);
  2500. buffer += 2;
  2501. }
  2502. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2503. {
  2504. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2505. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2506. buffer += 2;
  2507. }
  2508. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2509. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2510. if (remaining_digits < 9)
  2511. {
  2512. second_segment22_more_than_9_digits_third_subsegment_rounding:
  2513. if (check_rounding_condition_inside_subsegment(
  2514. current_digits, static_cast<std::uint32_t>(prod), 9 - remaining_digits,
  2515. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  2516. {
  2517. goto round_up_two_digits;
  2518. }
  2519. }
  2520. else
  2521. {
  2522. if (check_rounding_condition_with_next_bit(
  2523. current_digits, segment_boundary_rounding_bit,
  2524. has_further_digits<0, 0, ExtendedCache>(significand, exp2_base, k, uconst0, uconst0)))
  2525. {
  2526. goto round_up_two_digits;
  2527. }
  2528. }
  2529. goto print_last_two_digits;
  2530. }
  2531. }
  2532. } // ExtendedCache::segment_length == 22
  2533. else BOOST_CHARCONV_IF_CONSTEXPR (ExtendedCache::segment_length == 252)
  2534. {
  2535. int overlapping_digits = 252 - digits_in_the_second_segment;
  2536. int remaining_subsegment_pairs = 14;
  2537. while (overlapping_digits >= 18)
  2538. {
  2539. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(
  2540. power_of_10[18], blocks, cache_block_count);
  2541. --remaining_subsegment_pairs;
  2542. overlapping_digits -= 18;
  2543. }
  2544. auto subsegment_pair = fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(power_of_10[18] << 1, blocks, cache_block_count);
  2545. auto subsegment_boundary_rounding_bit = (subsegment_pair & 1) != 0;
  2546. subsegment_pair >>= 1;
  2547. // Deal with the first subsegment pair.
  2548. {
  2549. // Divide it into two 9-digits subsegments.
  2550. const auto first_part = static_cast<std::uint32_t>(subsegment_pair / power_of_10[9]);
  2551. const auto second_part = static_cast<std::uint32_t>(subsegment_pair - power_of_10[9] * first_part);
  2552. auto print_subsegment = [&](std::uint32_t subsegment, int digits_in_the_subsegment)
  2553. {
  2554. remaining_digits -= digits_in_the_subsegment;
  2555. // Move all overlapping digits into the integer part.
  2556. auto prod = ((subsegment * UINT64_C(720575941)) >> 24) + 1;
  2557. if (digits_in_the_subsegment < 9)
  2558. {
  2559. prod *= compute_power(UINT32_C(10), 8 - digits_in_the_subsegment);
  2560. if ((digits_in_the_subsegment & 1) != 0)
  2561. {
  2562. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2563. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  2564. ++buffer;
  2565. }
  2566. }
  2567. else
  2568. {
  2569. print_1_digit(static_cast<std::uint32_t>(prod >> 32), buffer);
  2570. ++buffer;
  2571. }
  2572. for (; digits_in_the_subsegment > 1; digits_in_the_subsegment -= 2)
  2573. {
  2574. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2575. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2576. buffer += 2;
  2577. }
  2578. };
  2579. // When the first part is not completely overlapping with the first segment.
  2580. int digits_in_the_second_part;
  2581. if (overlapping_digits < 9)
  2582. {
  2583. int digits_in_the_first_part = 9 - overlapping_digits;
  2584. // No rounding, continue.
  2585. if (remaining_digits > digits_in_the_first_part)
  2586. {
  2587. digits_in_the_second_part = 9;
  2588. print_subsegment(first_part, digits_in_the_first_part);
  2589. }
  2590. // Perform rounding and return.
  2591. else
  2592. {
  2593. // When there is no overlapping digit.
  2594. std::uint64_t prod;
  2595. if (digits_in_the_first_part == 9)
  2596. {
  2597. if ((remaining_digits & 1) != 0)
  2598. {
  2599. prod = ((first_part * UINT64_C(720575941)) >> 24) + 1;
  2600. }
  2601. else
  2602. {
  2603. if (remaining_digits == 0)
  2604. {
  2605. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2606. current_digits,
  2607. uint_with_known_number_of_digits<9>{first_part},
  2608. compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2609. {
  2610. goto round_up_two_digits;
  2611. }
  2612. goto print_last_two_digits;
  2613. }
  2614. prod = ((first_part * UINT64_C(450359963)) >> 20) + 1;
  2615. }
  2616. }
  2617. else
  2618. {
  2619. prod = ((first_part * UINT64_C(720575941)) >> 24) + 1;
  2620. prod *= compute_power(UINT32_C(10), 8 - digits_in_the_first_part);
  2621. if ((remaining_digits & 1) != 0)
  2622. {
  2623. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2624. }
  2625. else
  2626. {
  2627. if (remaining_digits == 0)
  2628. {
  2629. goto second_segment252_first_subsegment_rounding_inside_subsegment;
  2630. }
  2631. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2632. }
  2633. }
  2634. digits_in_the_first_part -= remaining_digits;
  2635. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2636. if (remaining_digits > 2)
  2637. {
  2638. if ((remaining_digits & 1) != 0)
  2639. {
  2640. print_1_digit(current_digits, buffer);
  2641. ++buffer;
  2642. }
  2643. else
  2644. {
  2645. print_2_digits(current_digits, buffer);
  2646. buffer += 2;
  2647. }
  2648. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2649. {
  2650. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2651. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2652. buffer += 2;
  2653. }
  2654. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2655. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2656. remaining_digits = 0;
  2657. }
  2658. if (digits_in_the_first_part != 0)
  2659. {
  2660. second_segment252_first_subsegment_rounding_inside_subsegment:
  2661. if (check_rounding_condition_inside_subsegment(
  2662. current_digits, static_cast<std::uint32_t>(prod),
  2663. digits_in_the_first_part, compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2664. {
  2665. goto round_up;
  2666. }
  2667. }
  2668. else
  2669. {
  2670. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2671. current_digits,
  2672. uint_with_known_number_of_digits<9>{static_cast<std::uint32_t>(second_part)},
  2673. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2674. {
  2675. goto round_up;
  2676. }
  2677. }
  2678. goto print_last_digits;
  2679. }
  2680. }
  2681. else
  2682. {
  2683. digits_in_the_second_part = 18 - overlapping_digits;
  2684. }
  2685. // Print the second part.
  2686. // No rounding, continue.
  2687. if (remaining_digits > digits_in_the_second_part)
  2688. {
  2689. print_subsegment(second_part, digits_in_the_second_part);
  2690. }
  2691. // Perform rounding and return.
  2692. else
  2693. {
  2694. // When there is no overlapping digit.
  2695. std::uint64_t prod;
  2696. if (digits_in_the_second_part == 9)
  2697. {
  2698. if ((remaining_digits & 1) != 0)
  2699. {
  2700. prod = ((second_part * UINT64_C(720575941)) >> 24) + 1;
  2701. }
  2702. else
  2703. {
  2704. if (remaining_digits == 0)
  2705. {
  2706. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2707. current_digits,
  2708. uint_with_known_number_of_digits<9>{static_cast<std::uint32_t>(second_part)},
  2709. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2710. {
  2711. goto round_up_two_digits;
  2712. }
  2713. goto print_last_two_digits;
  2714. }
  2715. prod = ((second_part * UINT64_C(450359963)) >> 20) + 1;
  2716. }
  2717. }
  2718. else
  2719. {
  2720. prod = ((second_part * UINT64_C(720575941)) >> 24) + 1;
  2721. prod *= compute_power(UINT32_C(10), 8 - digits_in_the_second_part);
  2722. if ((remaining_digits & 1) != 0)
  2723. {
  2724. prod = static_cast<std::uint32_t>(prod) * UINT64_C(10);
  2725. }
  2726. else
  2727. {
  2728. if (remaining_digits == 0)
  2729. {
  2730. goto second_segment252_second_subsegment_rounding_inside_subsegment;
  2731. }
  2732. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2733. }
  2734. }
  2735. digits_in_the_second_part -= remaining_digits;
  2736. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2737. if (remaining_digits > 2)
  2738. {
  2739. if ((remaining_digits & 1) != 0)
  2740. {
  2741. print_1_digit(current_digits, buffer);
  2742. ++buffer;
  2743. }
  2744. else
  2745. {
  2746. print_2_digits(current_digits, buffer);
  2747. buffer += 2;
  2748. }
  2749. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2750. {
  2751. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2752. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2753. buffer += 2;
  2754. }
  2755. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2756. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2757. remaining_digits = 0;
  2758. }
  2759. if (digits_in_the_second_part != 0)
  2760. {
  2761. second_segment252_second_subsegment_rounding_inside_subsegment:
  2762. if (check_rounding_condition_inside_subsegment(
  2763. current_digits, static_cast<std::uint32_t>(prod),
  2764. digits_in_the_second_part, compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2765. {
  2766. goto round_up;
  2767. }
  2768. }
  2769. else
  2770. {
  2771. if (check_rounding_condition_with_next_bit(
  2772. current_digits, subsegment_boundary_rounding_bit,
  2773. compute_has_further_digits<0, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2774. {
  2775. goto round_up;
  2776. }
  2777. }
  2778. goto print_last_digits;
  2779. }
  2780. }
  2781. // Remaining subsegment pairs do not have overlapping digits.
  2782. --remaining_subsegment_pairs;
  2783. for (; remaining_subsegment_pairs > 0; --remaining_subsegment_pairs)
  2784. {
  2785. subsegment_pair = fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(power_of_10[18], blocks, cache_block_count);
  2786. subsegment_pair += (subsegment_boundary_rounding_bit ? power_of_10[18] : 0);
  2787. subsegment_boundary_rounding_bit = (subsegment_pair & 1) != 0;
  2788. subsegment_pair >>= 1;
  2789. const auto first_part = static_cast<std::uint32_t>(subsegment_pair / power_of_10[9]);
  2790. const auto second_part = static_cast<std::uint32_t>(subsegment_pair - power_of_10[9] * first_part);
  2791. // The first part can be printed without rounding.
  2792. if (remaining_digits > 9)
  2793. {
  2794. print_9_digits(first_part, buffer);
  2795. // The second part also can be printed without rounding.
  2796. if (remaining_digits > 18)
  2797. {
  2798. print_9_digits(second_part, buffer + 9);
  2799. }
  2800. // Otherwise, perform rounding and return.
  2801. else
  2802. {
  2803. buffer += 9;
  2804. remaining_digits -= 9;
  2805. std::uint64_t prod;
  2806. int remaining_digits_in_the_current_subsegment = 9 - remaining_digits;
  2807. if ((remaining_digits & 1) != 0)
  2808. {
  2809. prod = ((second_part * UINT64_C(720575941)) >> 24) + 1;
  2810. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2811. if (remaining_digits == 1)
  2812. {
  2813. goto second_segment252_loop_second_subsegment_rounding;
  2814. }
  2815. print_1_digit(current_digits, buffer);
  2816. ++buffer;
  2817. }
  2818. else
  2819. {
  2820. prod = ((second_part * UINT64_C(450359963)) >> 20) + 1;
  2821. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2822. if (remaining_digits == 2)
  2823. {
  2824. goto second_segment252_loop_second_subsegment_rounding;
  2825. }
  2826. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2827. buffer += 2;
  2828. }
  2829. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2830. {
  2831. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2832. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2833. buffer += 2;
  2834. }
  2835. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2836. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2837. remaining_digits = 0;
  2838. if (remaining_digits_in_the_current_subsegment != 0)
  2839. {
  2840. second_segment252_loop_second_subsegment_rounding:
  2841. if (check_rounding_condition_inside_subsegment(
  2842. current_digits, static_cast<std::uint32_t>(prod),
  2843. remaining_digits_in_the_current_subsegment,
  2844. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2845. {
  2846. goto round_up;
  2847. }
  2848. goto print_last_digits;
  2849. }
  2850. else
  2851. {
  2852. if (check_rounding_condition_with_next_bit(
  2853. current_digits, subsegment_boundary_rounding_bit,
  2854. compute_has_further_digits<0, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2855. {
  2856. goto round_up_two_digits;
  2857. }
  2858. goto print_last_two_digits;
  2859. }
  2860. }
  2861. }
  2862. // Otherwise, perform rounding and return.
  2863. else
  2864. {
  2865. std::uint64_t prod;
  2866. int remaining_digits_in_the_current_subsegment = 9 - remaining_digits;
  2867. if ((remaining_digits & 1) != 0)
  2868. {
  2869. prod = ((first_part * UINT64_C(720575941)) >> 24) + 1;
  2870. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2871. if (remaining_digits == 1)
  2872. {
  2873. goto second_segment252_loop_first_subsegment_rounding;
  2874. }
  2875. print_1_digit(current_digits, buffer);
  2876. ++buffer;
  2877. }
  2878. else
  2879. {
  2880. prod = ((first_part * UINT64_C(450359963)) >> 20) + 1;
  2881. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2882. if (remaining_digits == 2)
  2883. {
  2884. goto second_segment252_loop_first_subsegment_rounding;
  2885. }
  2886. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2887. buffer += 2;
  2888. }
  2889. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  2890. {
  2891. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2892. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2893. buffer += 2;
  2894. }
  2895. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2896. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2897. remaining_digits = 0;
  2898. if (remaining_digits_in_the_current_subsegment != 0)
  2899. {
  2900. second_segment252_loop_first_subsegment_rounding:
  2901. if (check_rounding_condition_inside_subsegment(
  2902. current_digits, static_cast<std::uint32_t>(prod),
  2903. remaining_digits_in_the_current_subsegment,
  2904. compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2905. {
  2906. goto round_up;
  2907. }
  2908. goto print_last_digits;
  2909. }
  2910. else
  2911. {
  2912. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  2913. current_digits,
  2914. uint_with_known_number_of_digits<9>{static_cast<std::uint32_t>(second_part)},
  2915. compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  2916. {
  2917. goto round_up_two_digits;
  2918. }
  2919. goto print_last_two_digits;
  2920. }
  2921. }
  2922. buffer += 18;
  2923. remaining_digits -= 18;
  2924. }
  2925. } // ExtendedCache::segment_length == 252
  2926. }
  2927. // Print all remaining segments.
  2928. while (has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0))
  2929. {
  2930. // Get new segment.
  2931. ++multiplier_index;
  2932. k += ExtendedCache::segment_length;
  2933. cache_block_count = load_extended_cache<ExtendedCache, ExtendedCache::constant_block_count>(blocks, e, k, multiplier_index);
  2934. // Compute nm mod 2^Q.
  2935. fixed_point_calculator<ExtendedCache::max_cache_blocks>::discard_upper(significand, blocks, cache_block_count);
  2936. BOOST_CHARCONV_IF_CONSTEXPR (ExtendedCache::segment_length == 22)
  2937. {
  2938. // When at least two subsegments left.
  2939. if (remaining_digits > 16)
  2940. {
  2941. std::uint64_t first_second_subsegments = fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(power_of_10[16], blocks, cache_block_count);
  2942. const auto first_subsegment =
  2943. static_cast<std::uint32_t>(boost::charconv::detail::umul128_upper64(first_second_subsegments, UINT64_C(3022314549036573)) >> 14);
  2944. const std::uint32_t second_subsegment = static_cast<std::uint32_t>(first_second_subsegments) - UINT32_C(100000000) * first_subsegment;
  2945. print_8_digits(first_subsegment, buffer);
  2946. print_8_digits(second_subsegment, buffer + 8);
  2947. // When more segments left.
  2948. if (remaining_digits > 22)
  2949. {
  2950. const auto third_subsegment = static_cast<std::uint32_t>(
  2951. fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate_and_discard_lower(power_of_10[6], blocks,cache_block_count));
  2952. print_6_digits(third_subsegment, buffer + 16);
  2953. buffer += 22;
  2954. remaining_digits -= 22;
  2955. }
  2956. // When this is the last segment.
  2957. else
  2958. {
  2959. buffer += 16;
  2960. remaining_digits -= 16;
  2961. auto third_subsegment = fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  2962. generate_and_discard_lower(power_of_10[6] << 1, blocks, cache_block_count);
  2963. bool segment_boundary_rounding_bit = ((third_subsegment & 1) != 0);
  2964. third_subsegment >>= 1;
  2965. std::uint64_t prod;
  2966. if ((remaining_digits & 1) != 0)
  2967. {
  2968. prod = ((third_subsegment * UINT64_C(687195)) >> 4) + 1;
  2969. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2970. if (remaining_digits == 1)
  2971. {
  2972. if (check_rounding_condition_inside_subsegment(
  2973. current_digits, static_cast<std::uint32_t>(prod), 5,
  2974. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  2975. {
  2976. goto round_up_one_digit;
  2977. }
  2978. goto print_last_one_digit;
  2979. }
  2980. print_1_digit(current_digits, buffer);
  2981. ++buffer;
  2982. }
  2983. else
  2984. {
  2985. prod = (third_subsegment * UINT64_C(429497)) + 1;
  2986. current_digits = static_cast<std::uint32_t>(prod >> 32);
  2987. if (remaining_digits == 2)
  2988. {
  2989. goto segment_loop22_more_than_16_digits_rounding;
  2990. }
  2991. print_2_digits(current_digits, buffer);
  2992. buffer += 2;
  2993. }
  2994. if (remaining_digits > 4)
  2995. {
  2996. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  2997. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  2998. buffer += 2;
  2999. if (remaining_digits == 6)
  3000. {
  3001. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3002. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3003. if (check_rounding_condition_with_next_bit(
  3004. current_digits, segment_boundary_rounding_bit,
  3005. has_further_digits<0, 0, ExtendedCache>(significand, exp2_base, k, uconst0, uconst0)))
  3006. {
  3007. goto round_up_two_digits;
  3008. }
  3009. goto print_last_two_digits;
  3010. }
  3011. }
  3012. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3013. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3014. segment_loop22_more_than_16_digits_rounding:
  3015. if (check_rounding_condition_inside_subsegment(
  3016. current_digits, static_cast<std::uint32_t>(prod), 6 - remaining_digits,
  3017. has_further_digits<1, 0, ExtendedCache>(significand, exp2_base, k, uconst1, uconst0)))
  3018. {
  3019. goto round_up_two_digits;
  3020. }
  3021. goto print_last_two_digits;
  3022. }
  3023. }
  3024. // When two subsegments left.
  3025. else if (remaining_digits > 8)
  3026. {
  3027. // Get one more bit for potential rounding conditions check.
  3028. auto first_second_subsegments =
  3029. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  3030. generate_and_discard_lower(power_of_10[16] << 1, blocks, cache_block_count);
  3031. bool first_bit_of_third_subsegment = ((first_second_subsegments & 1) != 0);
  3032. first_second_subsegments >>= 1;
  3033. // 3022314549036573 = ceil(2^78/10^8) = floor(2^78*(10^8/(10^16 -
  3034. // 1))).
  3035. const auto first_subsegment =
  3036. static_cast<std::uint32_t>(boost::charconv::detail::umul128_upper64(first_second_subsegments, UINT64_C(3022314549036573)) >> 14);
  3037. const auto second_subsegment = static_cast<std::uint32_t>(first_second_subsegments) - UINT32_C(100000000) * first_subsegment;
  3038. print_8_digits(first_subsegment, buffer);
  3039. buffer += 8;
  3040. remaining_digits -= 8;
  3041. // Second subsegment (8 digits).
  3042. std::uint64_t prod;
  3043. if ((remaining_digits & 1) != 0)
  3044. {
  3045. prod = ((second_subsegment * UINT64_C(112589991)) >> 18) + 1;
  3046. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3047. if (remaining_digits == 1)
  3048. {
  3049. if (check_rounding_condition_inside_subsegment(
  3050. current_digits, static_cast<std::uint32_t>(prod), 7, has_further_digits<1, 6, ExtendedCache>(significand, exp2_base, k,
  3051. uconst1, uconst6)))
  3052. {
  3053. goto round_up_one_digit;
  3054. }
  3055. goto print_last_one_digit;
  3056. }
  3057. print_1_digit(current_digits, buffer);
  3058. ++buffer;
  3059. }
  3060. else
  3061. {
  3062. prod = ((second_subsegment * UINT64_C(140737489)) >> 15) + 1;
  3063. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3064. if (remaining_digits == 2)
  3065. {
  3066. goto segment_loop22_more_than_8_digits_rounding;
  3067. }
  3068. print_2_digits(current_digits, buffer);
  3069. buffer += 2;
  3070. }
  3071. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  3072. {
  3073. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3074. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  3075. buffer += 2;
  3076. }
  3077. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3078. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3079. if (remaining_digits < 8)
  3080. {
  3081. segment_loop22_more_than_8_digits_rounding:
  3082. if (check_rounding_condition_inside_subsegment(
  3083. current_digits, static_cast<std::uint32_t>(prod), 8 - remaining_digits,
  3084. has_further_digits<1, 6, ExtendedCache>(significand, exp2_base, k, uconst1, uconst6)))
  3085. {
  3086. goto round_up_two_digits;
  3087. }
  3088. }
  3089. else {
  3090. if (check_rounding_condition_with_next_bit(
  3091. current_digits, first_bit_of_third_subsegment,
  3092. has_further_digits<0, 6, ExtendedCache>(significand, exp2_base, k, uconst0, uconst6)))
  3093. {
  3094. goto round_up_two_digits;
  3095. }
  3096. }
  3097. goto print_last_two_digits;
  3098. }
  3099. // remaining_digits is at most 8.
  3100. else
  3101. {
  3102. // Get one more bit for potential rounding conditions check.
  3103. auto first_subsegment =
  3104. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  3105. generate_and_discard_lower(power_of_10[8] << 1, blocks, cache_block_count);
  3106. bool first_bit_of_second_subsegment = ((first_subsegment & 1) != 0);
  3107. first_subsegment >>= 1;
  3108. std::uint64_t prod;
  3109. if ((remaining_digits & 1) != 0)
  3110. {
  3111. prod = ((first_subsegment * UINT64_C(112589991)) >> 18) + 1;
  3112. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3113. if (remaining_digits == 1)
  3114. {
  3115. if (check_rounding_condition_inside_subsegment(
  3116. current_digits, static_cast<std::uint32_t>(prod), 7, has_further_digits<1, 14, ExtendedCache>(significand, exp2_base, k,
  3117. uconst1, uconst14)))
  3118. {
  3119. goto round_up_one_digit;
  3120. }
  3121. goto print_last_one_digit;
  3122. }
  3123. print_1_digit(current_digits, buffer);
  3124. ++buffer;
  3125. }
  3126. else
  3127. {
  3128. prod = ((first_subsegment * UINT64_C(140737489)) >> 15) + 1;
  3129. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3130. if (remaining_digits == 2)
  3131. {
  3132. goto segment_loop22_at_most_8_digits_rounding;
  3133. }
  3134. print_2_digits(current_digits, buffer);
  3135. buffer += 2;
  3136. }
  3137. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  3138. {
  3139. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3140. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  3141. buffer += 2;
  3142. }
  3143. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3144. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3145. if (remaining_digits < 8)
  3146. {
  3147. segment_loop22_at_most_8_digits_rounding:
  3148. if (check_rounding_condition_inside_subsegment(
  3149. current_digits, static_cast<std::uint32_t>(prod), 8 - remaining_digits,
  3150. has_further_digits<1, 14, ExtendedCache>(significand, exp2_base, k, uconst1, uconst14)))
  3151. {
  3152. goto round_up_two_digits;
  3153. }
  3154. }
  3155. else
  3156. {
  3157. if (check_rounding_condition_with_next_bit(
  3158. current_digits, first_bit_of_second_subsegment,
  3159. has_further_digits<0, 14, ExtendedCache>(significand, exp2_base, k, uconst0, uconst14)))
  3160. {
  3161. goto round_up_two_digits;
  3162. }
  3163. }
  3164. goto print_last_two_digits;
  3165. }
  3166. } // ExtendedCache::segment_length == 22
  3167. else if (ExtendedCache::segment_length == 252)
  3168. {
  3169. // Print as many 18-digits subsegment pairs as possible.
  3170. for (int remaining_subsegment_pairs = 14; remaining_subsegment_pairs > 0;
  3171. --remaining_subsegment_pairs)
  3172. {
  3173. // No rounding, continue.
  3174. if (remaining_digits > 18)
  3175. {
  3176. const auto subsegment_pair =
  3177. fixed_point_calculator<ExtendedCache::max_cache_blocks>::generate(power_of_10[18], blocks, cache_block_count);
  3178. const auto first_part = static_cast<std::uint32_t>(subsegment_pair / power_of_10[9]);
  3179. const auto second_part = static_cast<std::uint32_t>(subsegment_pair - power_of_10[9] * first_part);
  3180. print_9_digits(first_part, buffer);
  3181. print_9_digits(second_part, buffer + 9);
  3182. buffer += 18;
  3183. remaining_digits -= 18;
  3184. }
  3185. // Final subsegment pair.
  3186. else
  3187. {
  3188. auto last_subsegment_pair =
  3189. fixed_point_calculator<ExtendedCache::max_cache_blocks>::
  3190. generate_and_discard_lower(power_of_10[18] << 1, blocks, cache_block_count);
  3191. const bool subsegment_boundary_rounding_bit = ((last_subsegment_pair & 1) != 0);
  3192. last_subsegment_pair >>= 1;
  3193. const auto first_part = static_cast<std::uint32_t>(last_subsegment_pair / power_of_10[9]);
  3194. const auto second_part = static_cast<std::uint32_t>(last_subsegment_pair) - power_of_10[9] * first_part;
  3195. if (remaining_digits <= 9)
  3196. {
  3197. std::uint64_t prod;
  3198. if ((remaining_digits & 1) != 0)
  3199. {
  3200. prod = ((first_part * UINT64_C(1441151881)) >> 25) + 1;
  3201. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3202. if (remaining_digits == 1)
  3203. {
  3204. if (check_rounding_condition_inside_subsegment(
  3205. current_digits, static_cast<std::uint32_t>(prod), 8,
  3206. compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3207. {
  3208. goto round_up_one_digit;
  3209. }
  3210. goto print_last_one_digit;
  3211. }
  3212. print_1_digit(current_digits, buffer);
  3213. ++buffer;
  3214. }
  3215. else
  3216. {
  3217. prod = ((first_part * UINT64_C(450359963)) >> 20) + 1;
  3218. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3219. if (remaining_digits == 2)
  3220. {
  3221. goto segment_loop252_final18_first_part_rounding;
  3222. }
  3223. print_2_digits(current_digits, buffer);
  3224. buffer += 2;
  3225. }
  3226. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  3227. {
  3228. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3229. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  3230. buffer += 2;
  3231. }
  3232. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3233. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3234. if (remaining_digits < 9)
  3235. {
  3236. segment_loop252_final18_first_part_rounding:
  3237. if (check_rounding_condition_inside_subsegment(
  3238. current_digits, static_cast<std::uint32_t>(prod),
  3239. 9 - remaining_digits, compute_has_further_digits<1, 9, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3240. {
  3241. goto round_up_two_digits;
  3242. }
  3243. }
  3244. else
  3245. {
  3246. if (check_rounding_condition_subsegment_boundary_with_next_subsegment(
  3247. current_digits,
  3248. uint_with_known_number_of_digits<9>{static_cast<std::uint32_t>(second_part)},
  3249. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3250. {
  3251. goto round_up_two_digits;
  3252. }
  3253. }
  3254. goto print_last_two_digits;
  3255. } // remaining_digits <= 9
  3256. print_9_digits(first_part, buffer);
  3257. buffer += 9;
  3258. remaining_digits -= 9;
  3259. std::uint64_t prod;
  3260. if ((remaining_digits & 1) != 0)
  3261. {
  3262. prod = ((second_part * UINT64_C(1441151881)) >> 25) + 1;
  3263. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3264. if (remaining_digits == 1)
  3265. {
  3266. if (check_rounding_condition_inside_subsegment(
  3267. current_digits, static_cast<std::uint32_t>(prod), 8,
  3268. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3269. {
  3270. goto round_up_one_digit;
  3271. }
  3272. goto print_last_one_digit;
  3273. }
  3274. print_1_digit(current_digits, buffer);
  3275. ++buffer;
  3276. }
  3277. else
  3278. {
  3279. prod = ((second_part * UINT64_C(450359963)) >> 20) + 1;
  3280. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3281. if (remaining_digits == 2)
  3282. {
  3283. goto segment_loop252_final18_second_part_rounding;
  3284. }
  3285. print_2_digits(current_digits, buffer);
  3286. buffer += 2;
  3287. }
  3288. for (int i = 0; i < (remaining_digits - 3) / 2; ++i)
  3289. {
  3290. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3291. print_2_digits(static_cast<std::uint32_t>(prod >> 32), buffer);
  3292. buffer += 2;
  3293. }
  3294. prod = static_cast<std::uint32_t>(prod) * UINT64_C(100);
  3295. current_digits = static_cast<std::uint32_t>(prod >> 32);
  3296. if (remaining_digits < 9)
  3297. {
  3298. segment_loop252_final18_second_part_rounding:
  3299. if (check_rounding_condition_inside_subsegment(
  3300. current_digits, static_cast<std::uint32_t>(prod), 9 - remaining_digits,
  3301. compute_has_further_digits<1, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3302. {
  3303. goto round_up_two_digits;
  3304. }
  3305. }
  3306. else
  3307. {
  3308. if (check_rounding_condition_with_next_bit(
  3309. current_digits, subsegment_boundary_rounding_bit,
  3310. compute_has_further_digits<0, 0, ExtendedCache>, remaining_subsegment_pairs, significand, exp2_base, k))
  3311. {
  3312. goto round_up_two_digits;
  3313. }
  3314. }
  3315. goto print_last_two_digits;
  3316. }
  3317. }
  3318. } // if (ExtendedCache::segment_length == 252)
  3319. }
  3320. }
  3321. /////////////////////////////////////////////////////////////////////////////////////////////////
  3322. /// Phase 3 - Fill remaining digits with 0's, insert decimal dot, print exponent, and
  3323. /// return.
  3324. /////////////////////////////////////////////////////////////////////////////////////////////////
  3325. fill_remaining_digits_with_0s:
  3326. // This is probably not needed for the general format, but currently I am not 100% sure.
  3327. // (When fixed format is eventually chosen, we do not remove trailing zeros in the integer part.
  3328. // I am not sure if those trailing zeros are guaranteed to be already printed or not.)
  3329. std::memset(buffer, '0', static_cast<std::size_t>(remaining_digits));
  3330. buffer += remaining_digits;
  3331. insert_decimal_dot:
  3332. if (fmt == chars_format::general)
  3333. {
  3334. // Decide between fixed vs scientific.
  3335. if (-4 <= decimal_exponent_normalized && decimal_exponent_normalized < precision)
  3336. {
  3337. // Fixed.
  3338. if (decimal_exponent_normalized >= 0)
  3339. {
  3340. // Insert decimal dot.
  3341. decimal_dot_pos = buffer_starting_pos + decimal_exponent_normalized + 1;
  3342. std::memmove(buffer_starting_pos, buffer_starting_pos + 1,
  3343. static_cast<std::size_t>(decimal_dot_pos - buffer_starting_pos));
  3344. *decimal_dot_pos = '.';
  3345. }
  3346. else
  3347. {
  3348. // Print leading zeros and insert decimal dot.
  3349. int number_of_leading_zeros = -decimal_exponent_normalized - 1;
  3350. std::memmove(buffer_starting_pos + number_of_leading_zeros + 2, buffer_starting_pos + 1,
  3351. static_cast<std::size_t>(buffer - buffer_starting_pos - 1));
  3352. std::memcpy(buffer_starting_pos, "0.", 2);
  3353. std::memset(buffer_starting_pos + 2, '0', static_cast<std::size_t>(number_of_leading_zeros));
  3354. buffer += number_of_leading_zeros + 1;
  3355. }
  3356. // Don't print exponent.
  3357. fmt = chars_format::fixed;
  3358. }
  3359. else
  3360. {
  3361. // Scientific.
  3362. // Insert decimal dot.
  3363. *buffer_starting_pos = *(buffer_starting_pos + 1);
  3364. *(buffer_starting_pos + 1) = '.';
  3365. }
  3366. // Remove trailing zeros.
  3367. while (true)
  3368. {
  3369. auto prev = buffer - 1;
  3370. // Remove decimal dot as well if there is no fractional digits.
  3371. if (*prev == '.')
  3372. {
  3373. buffer = prev;
  3374. break;
  3375. }
  3376. else if (*prev != '0')
  3377. {
  3378. break;
  3379. }
  3380. buffer = prev;
  3381. }
  3382. }
  3383. else if (decimal_dot_pos != buffer_starting_pos)
  3384. {
  3385. std::memmove(buffer_starting_pos, buffer_starting_pos + 1,
  3386. static_cast<std::size_t>(decimal_dot_pos - buffer_starting_pos));
  3387. *decimal_dot_pos = '.';
  3388. }
  3389. if (fmt != chars_format::fixed)
  3390. {
  3391. if (decimal_exponent_normalized >= 0)
  3392. {
  3393. std::memcpy(buffer, "e+", 2); // NOLINT : Specifically not null-terminating
  3394. }
  3395. else
  3396. {
  3397. std::memcpy(buffer, "e-", 2); // NOLINT : Specifically not null-terminating
  3398. decimal_exponent_normalized = -decimal_exponent_normalized;
  3399. }
  3400. buffer += 2;
  3401. if (decimal_exponent_normalized >= 100)
  3402. {
  3403. // d1 = decimal_exponent / 10; d2 = decimal_exponent % 10;
  3404. // 6554 = ceil(2^16 / 10)
  3405. auto prod = static_cast<std::uint32_t>(decimal_exponent_normalized) * UINT32_C(6554);
  3406. auto d1 = prod >> 16;
  3407. prod = static_cast<std::uint16_t>(prod) * UINT16_C(5); // * 10
  3408. auto d2 = prod >> 15; // >> 16
  3409. print_2_digits(d1, buffer);
  3410. print_1_digit(d2, buffer + 2);
  3411. buffer += 3;
  3412. }
  3413. else
  3414. {
  3415. print_2_digits(static_cast<std::uint32_t>(decimal_exponent_normalized), buffer);
  3416. buffer += 2;
  3417. }
  3418. }
  3419. return {buffer, std::errc()};
  3420. round_up:
  3421. if ((remaining_digits & 1) != 0)
  3422. {
  3423. round_up_one_digit:
  3424. if (++current_digits == 10)
  3425. {
  3426. goto round_up_all_9s;
  3427. }
  3428. goto print_last_one_digit;
  3429. }
  3430. else
  3431. {
  3432. round_up_two_digits:
  3433. if (++current_digits == 100)
  3434. {
  3435. goto round_up_all_9s;
  3436. }
  3437. goto print_last_two_digits;
  3438. }
  3439. print_last_digits:
  3440. if ((remaining_digits & 1) != 0)
  3441. {
  3442. print_last_one_digit:
  3443. print_1_digit(current_digits, buffer);
  3444. ++buffer;
  3445. }
  3446. else
  3447. {
  3448. print_last_two_digits:
  3449. print_2_digits(current_digits, buffer);
  3450. buffer += 2;
  3451. }
  3452. goto insert_decimal_dot;
  3453. round_up_all_9s:
  3454. char* first_9_pos = buffer;
  3455. buffer += (2 - (remaining_digits & 1));
  3456. // Find the starting position of printed digits.
  3457. char* digit_starting_pos = [&] {
  3458. // For negative exponent & fixed format, we already printed leading zeros.
  3459. if (fmt == chars_format::fixed && decimal_exponent_normalized < 0)
  3460. {
  3461. return buffer_starting_pos - decimal_exponent_normalized + 1;
  3462. }
  3463. // We reserved one slot for decimal dot, so the starting position of printed digits
  3464. // is buffer_starting_pos + 1 if we need to print decimal dot.
  3465. return buffer_starting_pos == decimal_dot_pos ? buffer_starting_pos
  3466. : buffer_starting_pos + 1;
  3467. }();
  3468. // Find all preceding 9's.
  3469. if ((first_9_pos - digit_starting_pos) % 2 != 0)
  3470. {
  3471. if (*(first_9_pos - 1) != '9')
  3472. {
  3473. ++*(first_9_pos - 1);
  3474. if ((remaining_digits & 1) != 0)
  3475. {
  3476. *first_9_pos = '0';
  3477. }
  3478. else
  3479. {
  3480. std::memcpy(first_9_pos, "00", 2);
  3481. }
  3482. goto insert_decimal_dot;
  3483. }
  3484. --first_9_pos;
  3485. }
  3486. while (first_9_pos != digit_starting_pos)
  3487. {
  3488. if (std::memcmp(first_9_pos - 2, "99", 2) != 0)
  3489. {
  3490. if (*(first_9_pos - 1) != '9')
  3491. {
  3492. ++*(first_9_pos - 1);
  3493. }
  3494. else
  3495. {
  3496. ++*(first_9_pos - 2);
  3497. *(first_9_pos - 1) = '0';
  3498. }
  3499. std::memset(first_9_pos, '0', static_cast<std::size_t>(buffer - first_9_pos));
  3500. goto insert_decimal_dot;
  3501. }
  3502. first_9_pos -= 2;
  3503. }
  3504. // Every digit we wrote so far are all 9's. In this case, we have to shift the whole thing by 1.
  3505. ++decimal_exponent_normalized;
  3506. if (fmt == chars_format::fixed)
  3507. {
  3508. if (decimal_exponent_normalized > 0)
  3509. {
  3510. // We need to print one more character.
  3511. if (buffer == last)
  3512. {
  3513. return {last, std::errc::value_too_large};
  3514. }
  3515. ++buffer;
  3516. // If we were to print the decimal dot, we have to shift it to right
  3517. // since we now have one more digit in the integer part.
  3518. if (buffer_starting_pos != decimal_dot_pos)
  3519. {
  3520. ++decimal_dot_pos;
  3521. }
  3522. }
  3523. else if (decimal_exponent_normalized == 0)
  3524. {
  3525. // For the case 0.99...9 -> 1.00...0, the rounded digit is one before the first digit written.
  3526. // Note: decimal_exponent_normalized was negative before the increment (++decimal_exponent_normalized),
  3527. // so we already have printed "00" onto the buffer.
  3528. // Hence, --digit_starting_pos doesn't go more than the starting position of the buffer.
  3529. --digit_starting_pos;
  3530. }
  3531. }
  3532. // Nolint is applied to the following two calls since we know they are not supposed to be null terminated
  3533. *digit_starting_pos = '1';
  3534. std::memset(digit_starting_pos + 1, '0', static_cast<std::size_t>(buffer - digit_starting_pos - 1)); // NOLINT
  3535. goto insert_decimal_dot;
  3536. }
  3537. }}} // Namespaces
  3538. #ifdef BOOST_MSVC
  3539. # pragma warning(pop)
  3540. #endif
  3541. #endif // BOOST_CHARCONV_DETAIL_FLOFF