floff.hpp 190 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057
  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. bool trailing_zeros_removed = false;
  1172. BOOST_CHARCONV_ASSERT(precision >= 0);
  1173. using namespace detail;
  1174. std::uint64_t br = default_float_traits<double>::float_to_carrier(x);
  1175. bool is_negative = ((br >> 63) != 0);
  1176. br <<= 1;
  1177. int e = static_cast<int>(br >> (ieee754_binary64::significand_bits + 1));
  1178. auto significand = (br & ((UINT64_C(1) << (ieee754_binary64::significand_bits + 1)) - 1)); // shifted by 1-bit.
  1179. if (is_negative)
  1180. {
  1181. *buffer = '-';
  1182. ++buffer;
  1183. --buffer_size;
  1184. if (buffer_size == 0)
  1185. {
  1186. return {buffer, std::errc::value_too_large};
  1187. }
  1188. }
  1189. // Infinities or NaN
  1190. if (e == ((UINT32_C(1) << ieee754_binary64::exponent_bits) - 1))
  1191. {
  1192. if (significand == 0)
  1193. {
  1194. constexpr std::size_t inf_chars = 3;
  1195. if (buffer_size < inf_chars)
  1196. {
  1197. return {last, std::errc::value_too_large};
  1198. }
  1199. std::memcpy(buffer, "inf", inf_chars); // NOLINT : Specifically not null-terminating
  1200. return {buffer + inf_chars, std::errc()};
  1201. }
  1202. else
  1203. {
  1204. // Significand values for NaN by type
  1205. // qNaN = 4503599627370496
  1206. // sNaN = 2251799813685248
  1207. //
  1208. if (significand == UINT64_C(4503599627370496))
  1209. {
  1210. if (!is_negative)
  1211. {
  1212. constexpr std::size_t nan_chars = 3;
  1213. if (buffer_size < nan_chars)
  1214. {
  1215. return {last, std::errc::value_too_large};
  1216. }
  1217. std::memcpy(buffer, "nan", nan_chars); // NOLINT : Specifically not null-terminating
  1218. return {buffer + nan_chars, std::errc()};
  1219. }
  1220. else
  1221. {
  1222. constexpr std::size_t neg_nan_chars = 8;
  1223. if (buffer_size < neg_nan_chars)
  1224. {
  1225. return {last, std::errc::value_too_large};
  1226. }
  1227. std::memcpy(buffer, "nan(ind)", neg_nan_chars); // NOLINT : Specifically not null-terminating
  1228. return {buffer + neg_nan_chars, std::errc()};
  1229. }
  1230. }
  1231. else
  1232. {
  1233. constexpr std::size_t snan_chars = 9;
  1234. if (buffer_size < snan_chars)
  1235. {
  1236. return {last, std::errc::value_too_large};
  1237. }
  1238. std::memcpy(buffer, "nan(snan)", snan_chars); // NOLINT : Specifically not null-terminating
  1239. return {buffer + snan_chars, std::errc()};
  1240. }
  1241. }
  1242. }
  1243. else
  1244. {
  1245. // Normal numbers.
  1246. if (e != 0)
  1247. {
  1248. significand |= (decltype(significand)(1) << (ieee754_binary64::significand_bits + 1));
  1249. e += (ieee754_binary64::exponent_bias - ieee754_binary64::significand_bits);
  1250. }
  1251. // Subnormal numbers.
  1252. else
  1253. {
  1254. // Zero
  1255. if (significand == 0)
  1256. {
  1257. if (fmt == boost::charconv::chars_format::general)
  1258. {
  1259. // For the case of chars_format::general, 0 is always printed as 0.
  1260. *buffer = '0';
  1261. return {buffer + 1, std::errc()};
  1262. }
  1263. else if (fmt == boost::charconv::chars_format::fixed)
  1264. {
  1265. return print_zero_fixed(buffer, buffer_size, precision);
  1266. }
  1267. // For the case of chars_format::scientific, print as many 0's as requested after the decimal dot, and then print e+00.
  1268. if (precision == 0)
  1269. {
  1270. constexpr std::size_t zero_chars = 5;
  1271. if (buffer_size < zero_chars)
  1272. {
  1273. return {last, std::errc::value_too_large};
  1274. }
  1275. std::memcpy(buffer, "0e+00", zero_chars);
  1276. return {buffer + zero_chars, std::errc()};
  1277. }
  1278. else
  1279. {
  1280. if (buffer_size < static_cast<std::size_t>(precision) + 6U)
  1281. {
  1282. return {last, std::errc::value_too_large};
  1283. }
  1284. std::memcpy(buffer, "0.", 2); // NOLINT : Specifically not null-terminating
  1285. std::memset(buffer + 2, '0', static_cast<std::size_t>(precision)); // NOLINT : Specifically not null-terminating
  1286. std::memcpy(buffer + 2 + precision, "e+00", 4); // NOLINT : Specifically not null-terminating
  1287. return {buffer + precision + 6, std::errc()};
  1288. }
  1289. }
  1290. // Nonzero
  1291. e = ieee754_binary64::min_exponent - ieee754_binary64::significand_bits;
  1292. }
  1293. }
  1294. constexpr int kappa = 2;
  1295. int k = kappa - log::floor_log10_pow2(e);
  1296. std::uint32_t current_digits {};
  1297. char* const buffer_starting_pos = buffer;
  1298. char* decimal_dot_pos = buffer; // decimal_dot_pos == buffer_starting_pos indicates that there should be no decimal dot.
  1299. int decimal_exponent_normalized {};
  1300. // Number of digits to be printed.
  1301. int remaining_digits {};
  1302. /////////////////////////////////////////////////////////////////////////////////////////////////
  1303. /// Phase 1 - Print the first digit segment computed with the Dragonbox table.
  1304. /////////////////////////////////////////////////////////////////////////////////////////////////
  1305. {
  1306. // Compute the first digit segment.
  1307. const auto main_cache = MainCache::template get_cache<ieee754_binary64>(k);
  1308. const int beta = e + log::floor_log2_pow10(k);
  1309. // Integer check is okay for binary64.
  1310. //auto [first_segment, has_more_segments]
  1311. compute_mul_result segments = [&] {
  1312. const auto r = umul192_upper128(significand << beta, main_cache);
  1313. return compute_mul_result{r.high, r.low == 0};
  1314. }();
  1315. auto first_segment = segments.result;
  1316. auto has_more_segments = !segments.is_integer;
  1317. // The first segment can be up to 19 digits. It is in fact always of either 18 or 19
  1318. // digits except when the input is a subnormal number. For subnormal numbers, the
  1319. // smallest possible value of the first segment is 10^kappa, so it is of at least
  1320. // kappa+1 digits (i.e., 3 in this case).
  1321. int first_segment_length = 19;
  1322. auto first_segment_aligned = first_segment; // Aligned to have 19 digits.
  1323. while (first_segment_aligned < UINT64_C(10000000000000000))
  1324. {
  1325. first_segment_aligned *= 100;
  1326. first_segment_length -= 2;
  1327. }
  1328. if (first_segment_aligned < UINT64_C(1000000000000000000))
  1329. {
  1330. first_segment_aligned *= 10;
  1331. first_segment_length -= 1;
  1332. }
  1333. // The decimal exponent when written as X.XXXX.... x 10^XX.
  1334. decimal_exponent_normalized = first_segment_length - k - 1;
  1335. // Figure out the correct value of remaining_digits.
  1336. if (fmt == boost::charconv::chars_format::scientific)
  1337. {
  1338. remaining_digits = precision + 1;
  1339. // e+XX or e+XXX since we always print at least two characters e.g. e+02
  1340. const int exponent_print_length =
  1341. decimal_exponent_normalized > -100 && decimal_exponent_normalized < 100 ? 4 : 5;
  1342. // No trailing decimal dot.
  1343. const 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. trailing_zeros_removed = true;
  3368. while (true)
  3369. {
  3370. auto prev = buffer - 1;
  3371. // Remove decimal dot as well if there is no fractional digits.
  3372. if (*prev == '.')
  3373. {
  3374. buffer = prev;
  3375. break;
  3376. }
  3377. else if (*prev != '0')
  3378. {
  3379. break;
  3380. }
  3381. buffer = prev;
  3382. }
  3383. }
  3384. else if (decimal_dot_pos != buffer_starting_pos)
  3385. {
  3386. std::memmove(buffer_starting_pos, buffer_starting_pos + 1,
  3387. static_cast<std::size_t>(decimal_dot_pos - buffer_starting_pos));
  3388. *decimal_dot_pos = '.';
  3389. }
  3390. if (fmt != chars_format::fixed)
  3391. {
  3392. if (decimal_exponent_normalized >= 0)
  3393. {
  3394. std::memcpy(buffer, "e+", 2); // NOLINT : Specifically not null-terminating
  3395. }
  3396. else
  3397. {
  3398. std::memcpy(buffer, "e-", 2); // NOLINT : Specifically not null-terminating
  3399. decimal_exponent_normalized = -decimal_exponent_normalized;
  3400. }
  3401. buffer += 2;
  3402. if (decimal_exponent_normalized >= 100)
  3403. {
  3404. // d1 = decimal_exponent / 10; d2 = decimal_exponent % 10;
  3405. // 6554 = ceil(2^16 / 10)
  3406. auto prod = static_cast<std::uint32_t>(decimal_exponent_normalized) * UINT32_C(6554);
  3407. auto d1 = prod >> 16;
  3408. prod = static_cast<std::uint16_t>(prod) * UINT16_C(5); // * 10
  3409. auto d2 = prod >> 15; // >> 16
  3410. print_2_digits(d1, buffer);
  3411. print_1_digit(d2, buffer + 2);
  3412. buffer += 3;
  3413. }
  3414. else
  3415. {
  3416. print_2_digits(static_cast<std::uint32_t>(decimal_exponent_normalized), buffer);
  3417. buffer += 2;
  3418. }
  3419. }
  3420. else if (!trailing_zeros_removed && buffer - (decimal_dot_pos + 1) < precision)
  3421. {
  3422. // If we have fixed precision, and we don't have enough digits after the decimal yet
  3423. // insert a sufficient amount of zeros
  3424. const auto remaining_zeros = precision - (buffer - (decimal_dot_pos + 1));
  3425. BOOST_CHARCONV_ASSERT(remaining_zeros > 0);
  3426. std::memset(buffer, '0', static_cast<std::size_t>(remaining_zeros));
  3427. buffer += remaining_zeros;
  3428. }
  3429. return {buffer, std::errc()};
  3430. round_up:
  3431. if ((remaining_digits & 1) != 0)
  3432. {
  3433. round_up_one_digit:
  3434. if (++current_digits == 10)
  3435. {
  3436. goto round_up_all_9s;
  3437. }
  3438. goto print_last_one_digit;
  3439. }
  3440. else
  3441. {
  3442. round_up_two_digits:
  3443. if (++current_digits == 100)
  3444. {
  3445. goto round_up_all_9s;
  3446. }
  3447. goto print_last_two_digits;
  3448. }
  3449. print_last_digits:
  3450. if ((remaining_digits & 1) != 0)
  3451. {
  3452. print_last_one_digit:
  3453. print_1_digit(current_digits, buffer);
  3454. ++buffer;
  3455. }
  3456. else
  3457. {
  3458. print_last_two_digits:
  3459. print_2_digits(current_digits, buffer);
  3460. buffer += 2;
  3461. }
  3462. goto insert_decimal_dot;
  3463. round_up_all_9s:
  3464. char* first_9_pos = buffer;
  3465. buffer += (2 - (remaining_digits & 1));
  3466. // Find the starting position of printed digits.
  3467. char* digit_starting_pos = [&] {
  3468. // For negative exponent & fixed format, we already printed leading zeros.
  3469. if (fmt == chars_format::fixed && decimal_exponent_normalized < 0)
  3470. {
  3471. return buffer_starting_pos - decimal_exponent_normalized + 1;
  3472. }
  3473. // We reserved one slot for decimal dot, so the starting position of printed digits
  3474. // is buffer_starting_pos + 1 if we need to print decimal dot.
  3475. return buffer_starting_pos == decimal_dot_pos ? buffer_starting_pos
  3476. : buffer_starting_pos + 1;
  3477. }();
  3478. // Find all preceding 9's.
  3479. if ((first_9_pos - digit_starting_pos) % 2 != 0)
  3480. {
  3481. if (*(first_9_pos - 1) != '9')
  3482. {
  3483. ++*(first_9_pos - 1);
  3484. if ((remaining_digits & 1) != 0)
  3485. {
  3486. *first_9_pos = '0';
  3487. }
  3488. else
  3489. {
  3490. std::memcpy(first_9_pos, "00", 2);
  3491. }
  3492. goto insert_decimal_dot;
  3493. }
  3494. --first_9_pos;
  3495. }
  3496. while (first_9_pos != digit_starting_pos)
  3497. {
  3498. if (std::memcmp(first_9_pos - 2, "99", 2) != 0)
  3499. {
  3500. if (*(first_9_pos - 1) != '9')
  3501. {
  3502. ++*(first_9_pos - 1);
  3503. }
  3504. else
  3505. {
  3506. ++*(first_9_pos - 2);
  3507. *(first_9_pos - 1) = '0';
  3508. }
  3509. std::memset(first_9_pos, '0', static_cast<std::size_t>(buffer - first_9_pos));
  3510. goto insert_decimal_dot;
  3511. }
  3512. first_9_pos -= 2;
  3513. }
  3514. // Every digit we wrote so far are all 9's. In this case, we have to shift the whole thing by 1.
  3515. ++decimal_exponent_normalized;
  3516. if (fmt == chars_format::fixed)
  3517. {
  3518. if (decimal_exponent_normalized > 0)
  3519. {
  3520. // We need to print one more character.
  3521. if (buffer == last)
  3522. {
  3523. return {last, std::errc::value_too_large};
  3524. }
  3525. ++buffer;
  3526. // If we were to print the decimal dot, we have to shift it to right
  3527. // since we now have one more digit in the integer part.
  3528. if (buffer_starting_pos != decimal_dot_pos)
  3529. {
  3530. ++decimal_dot_pos;
  3531. }
  3532. }
  3533. else if (decimal_exponent_normalized == 0 || remaining_digits == 1)
  3534. {
  3535. // For the case 0.99...9 -> 1.00...0, the rounded digit is one before the first digit written.
  3536. // This same case applies for 0.099 -> 0.10 in the precision = 2 instance
  3537. // Note: decimal_exponent_normalized was negative before the increment (++decimal_exponent_normalized),
  3538. // so we already have printed "00" onto the buffer.
  3539. // Hence, --digit_starting_pos doesn't go more than the starting position of the buffer.
  3540. --digit_starting_pos;
  3541. }
  3542. }
  3543. // Nolint is applied to the following two calls since we know they are not supposed to be null terminated
  3544. *digit_starting_pos = '1';
  3545. std::memset(digit_starting_pos + 1, '0', static_cast<std::size_t>(buffer - digit_starting_pos - 1)); // NOLINT
  3546. goto insert_decimal_dot;
  3547. }
  3548. }}} // Namespaces
  3549. #ifdef BOOST_MSVC
  3550. # pragma warning(pop)
  3551. #endif
  3552. #endif // BOOST_CHARCONV_DETAIL_FLOFF