-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Fix Off By One Errors In Printf Long Double #66957
[libc] Fix Off By One Errors In Printf Long Double #66957
Conversation
Two major off-by-one errors are fixed in this patch. The first is in float_to_string.h with length_for_num, which wasn't accounting for the implicit leading bit when calculating the length of a number, causing a missing digit on 80 bit float max. The other off-by-one is the ryu_long_double_constants.h (a.k.a the Mega Table) not having any entries for the last POW10_OFFSET in POW10_SPLIT. This was also found on 80 bit float max. Finally, the integer calculation mode was using a slightly too short integer, again on 80 bit float max, not accounting for the mantissa width. All of these are fixed in this patch.
@llvm/pr-subscribers-libc ChangesTwo major off-by-one errors are fixed in this patch. The first is in Patch is 66.76 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/66957.diff 4 Files Affected:
diff --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index 4714068eadf0e52..4c58651dc6884b7 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -157,7 +157,7 @@ LIBC_INLINE constexpr uint32_t ceil_log10_pow2(const uint32_t e) {
LIBC_INLINE constexpr uint32_t length_for_num(const uint32_t idx,
const uint32_t mantissa_width) {
//+8 to round up when dividing by 9
- return (ceil_log10_pow2(idx) + ceil_log10_pow2(mantissa_width) +
+ return (ceil_log10_pow2(idx) + ceil_log10_pow2(mantissa_width + 1) +
(BLOCK_SIZE - 1)) /
BLOCK_SIZE;
// return (ceil_log10_pow2(16 * idx + mantissa_width) + 8) / 9;
@@ -666,7 +666,7 @@ FloatToString<long double>::get_positive_block(int block_index) {
// ----------------------------- INT CALC MODE -----------------------------
const int32_t SHIFT_CONST = CALC_SHIFT_CONST;
const uint64_t MAX_POW_2_SIZE =
- exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * block_index);
+ pos_exp + CALC_SHIFT_CONST - (BLOCK_SIZE * block_index);
const uint64_t MAX_POW_5_SIZE =
internal::log2_pow5(BLOCK_SIZE * block_index);
const uint64_t MAX_INT_SIZE =
@@ -680,8 +680,10 @@ FloatToString<long double>::get_positive_block(int block_index) {
val = internal::get_table_positive<4096>(pos_exp, block_index);
} else if (MAX_INT_SIZE < 8192) {
val = internal::get_table_positive<8192>(pos_exp, block_index);
- } else {
+ } else if (MAX_INT_SIZE < 16384) {
val = internal::get_table_positive<16384>(pos_exp, block_index);
+ } else {
+ val = internal::get_table_positive<16384 + 128>(pos_exp, block_index);
}
#endif
const uint32_t shift_amount = SHIFT_CONST + pos_exp - exponent;
diff --git a/libc/src/__support/ryu_long_double_constants.h b/libc/src/__support/ryu_long_double_constants.h
index 8415423e9eddd6a..86640bcc8257007 100644
--- a/libc/src/__support/ryu_long_double_constants.h
+++ b/libc/src/__support/ryu_long_double_constants.h
@@ -35403,6 +35403,558 @@ constexpr uint64_t POW10_SPLIT[][5] = {
{8185529170695989288u, 251935u, 0u, 0u, 0u},
{4647388653739187u, 0u, 0u, 0u, 0u},
{4647389u, 0u, 0u, 0u, 0u},
+ {1u, 0u, 0u, 0u, 1666406u},
+ {9167511163728910775u, 3312060066959522888u, 1295184565790726114u, 14514316166628409175u, 467615u},
+ {18446650963712978694u, 11459123952047835623u, 11203169122561999106u, 12474589757305876786u, 2263011u},
+ {1727375103581005166u, 6279996614298463960u, 7709863957368393064u, 16687049419988932499u, 1149488u},
+ {5260295235157421525u, 18426378431810315116u, 1049713953025921878u, 12343052908124526721u, 3504815u},
+ {3705581585852188490u, 3335423218796647794u, 5009356519446298754u, 17358475006776455890u, 3071434u},
+ {14296650234967904607u, 13533728612605645886u, 13550853390885990417u, 17206365355322613797u, 2749915u},
+ {18409334644427041092u, 1794178469698007910u, 17611594257294438978u, 1708051658308162884u, 3019512u},
+ {4754135742177139480u, 6231107125027989758u, 17105560207999383557u, 17277465141864323237u, 2595660u},
+ {18358242171314739190u, 5662514190210093154u, 11287743438560124974u, 5452201045844425276u, 2058811u},
+ {17761256625928321051u, 17212837789613283598u, 9527998849657635872u, 11639251005171736777u, 1601676u},
+ {5821492835406745926u, 2889087753330502654u, 17816699099621606956u, 1830985569848452224u, 878190u},
+ {12963632653576508465u, 16198926776608324708u, 18116681369755801167u, 3835252232019257168u, 913381u},
+ {9574571567457348478u, 3277569375660351599u, 18148476178141229221u, 8447587411821609647u, 210151u},
+ {13880179963005905487u, 5477919832920232767u, 14817599233219952607u, 8578730300674845931u, 1381841u},
+ {4139500131013550121u, 3663479087884115807u, 3494753886518274538u, 15734045976124579229u, 3587513u},
+ {3951002731038455889u, 6648345471811276523u, 14225211067814220084u, 8568974046381648398u, 2873430u},
+ {1688486474885901699u, 3743680607006362896u, 5194811187761484099u, 15977733718774767139u, 2499029u},
+ {11994796996225841889u, 17880557183921429048u, 15497995200573975779u, 12079717168707477337u, 3113160u},
+ {14145368163067234658u, 9261433793631684194u, 7680189330107885166u, 13243967386801039084u, 526971u},
+ {15445030309197543459u, 14728335854035343397u, 17075675439650901028u, 7287537910245955718u, 2849008u},
+ {1142171144987344907u, 7490797843865302792u, 8787155366672039387u, 2718685908130822644u, 484791u},
+ {5408153414594581696u, 2427550704946339717u, 6793841772739607308u, 14348404031772582899u, 624735u},
+ {12520228542564573657u, 14812481887778795293u, 9249283428732450365u, 8081974873255221800u, 806845u},
+ {790425527012045027u, 17212573622941371702u, 7910578513568475163u, 12408789845827732048u, 1161789u},
+ {18054897266975082573u, 8506153697810646314u, 1662661719671814836u, 4056656502883405208u, 3185768u},
+ {9270379265643408620u, 1195149283137702515u, 13207984899139388752u, 9930657434226997265u, 2202773u},
+ {16614137443348276141u, 14781126627478736439u, 6803683723866315318u, 14668325589413505852u, 859112u},
+ {7447673545517676182u, 1083916344985410534u, 13308050152951963757u, 4195188288062798637u, 2406710u},
+ {640276865045725606u, 9429623404773127942u, 600293903614474160u, 1125259878193744843u, 2131145u},
+ {16798302994488502999u, 1110264672744614054u, 10559380483812768002u, 11568527733592695380u, 931863u},
+ {2248359540383920274u, 5894188707032120406u, 9043257816917527345u, 6934718877482368793u, 164401u},
+ {2862341250731230715u, 4246895923326563304u, 8809995434622158204u, 1876530115093307137u, 2901191u},
+ {8405103544676021516u, 11567689276855949105u, 15784543052813908722u, 4809318736265723378u, 999334u},
+ {15579631011428052726u, 16785760031102532158u, 5909361390067953776u, 9241806500206251005u, 2150196u},
+ {370319694131435420u, 3083801843464632004u, 4662741834226062242u, 6164559617785995043u, 3620301u},
+ {1033559633838940172u, 15810150688083630215u, 3891367580811375289u, 13037149699008382861u, 120482u},
+ {11308568505275257325u, 613731621304509155u, 3803382403086045620u, 18304851399290334117u, 421341u},
+ {8677144826867200054u, 17827139855619338965u, 8813322557930393339u, 8078222920147541087u, 3545930u},
+ {7375575360705793469u, 12651035560908367098u, 9394262074024389219u, 18440097350963135510u, 1843942u},
+ {16669835522062886782u, 17348663479059537611u, 18244674427130191192u, 1619281813435276080u, 747200u},
+ {17464778948620520076u, 12917510971872457916u, 7429449805763610688u, 15722338909059447638u, 491710u},
+ {7702674345127274593u, 11601590047702440914u, 12627737072937908926u, 15717625964519112680u, 1868229u},
+ {9788000944531531893u, 15462636986002358161u, 7842093577620732495u, 11851908180171889790u, 2452442u},
+ {12109297881698999285u, 17131651483582465886u, 6569550798974851021u, 1774621838691795044u, 753597u},
+ {13133653791234500424u, 4324827303956416464u, 12192519312904677579u, 6931430440409418991u, 1146093u},
+ {5045375477987867200u, 6678799442071584796u, 10139350272668939471u, 17603194636441516825u, 1872575u},
+ {12798246487239652604u, 16714555538748891233u, 1085554466557611808u, 14950464895238104052u, 720150u},
+ {2664578200625687471u, 16793883355150161601u, 15391344404818619526u, 17235049412759923532u, 1840075u},
+ {18374901552925357261u, 12553865988854528153u, 17519513554508473654u, 12139619208208373763u, 3447533u},
+ {17671381561613929399u, 2262727631311519342u, 15121480342923670975u, 3017957126631332695u, 837291u},
+ {8460112156141233331u, 7983046005719909202u, 15005410844269686945u, 7365319887678826942u, 2794384u},
+ {2379370595853479811u, 13860668065444366236u, 6673137095531534673u, 9779322488977260039u, 1224093u},
+ {1415933634462666168u, 13318864766636345134u, 2647925470075013075u, 13065005060937698251u, 2142779u},
+ {7782049967851326679u, 14165493027220141411u, 4424988645680867478u, 12505491077446057268u, 3817586u},
+ {14298756137794571746u, 5871188803067413627u, 14048602070109968112u, 7708527012447228845u, 3011900u},
+ {4050782139123098694u, 16341196407815195137u, 17698899155718192812u, 8414240664583773386u, 2285155u},
+ {5890934981019196614u, 7047030131107131070u, 126062594834924354u, 13661038951036378319u, 3048693u},
+ {6564357374484156534u, 13967226511753616903u, 1498174585292480796u, 17854464200559548937u, 2283376u},
+ {13259932982941242686u, 6889276820041754061u, 12733502488724171474u, 8256686590874299525u, 3495034u},
+ {9313261355332184729u, 13168455044449058327u, 16546258483028024409u, 11593687052052069739u, 2491259u},
+ {13207118334093637735u, 3391170805209232789u, 16627789664552178912u, 4729699241253328475u, 71313u},
+ {7085701162826158826u, 15325670087512086235u, 9365786138160176070u, 5693865426386134634u, 267597u},
+ {14198705572034952613u, 14562521029100056325u, 4979170736928754471u, 2887240060584875321u, 826036u},
+ {12613313478282210826u, 7767769415626507749u, 14085002163747829797u, 18173751375132750675u, 554120u},
+ {3568965762911820816u, 943907302893284828u, 16126302233087205720u, 14709970911737174255u, 3795586u},
+ {16009388400392220742u, 11870203406362803303u, 16918269583830235138u, 8933100284926861981u, 1843037u},
+ {12360163387826619807u, 7286063737319541239u, 14915295217745959906u, 610458793093901203u, 243975u},
+ {4678693883975533460u, 8650260372010974321u, 17513611905647666644u, 2670631524399175712u, 3709456u},
+ {16140389596301619804u, 14018514106585446252u, 16573599126622797627u, 2374270397369011814u, 3456412u},
+ {17824937920199406379u, 15949883623231815687u, 1644962704585409640u, 3234293687620398160u, 1623083u},
+ {7450515525907448647u, 9803984207968383106u, 14498223819039278025u, 14729689783682981351u, 1011383u},
+ {16773139797707794461u, 6386168526813270247u, 2306327785699430784u, 18393343217762814051u, 1794415u},
+ {2118816782039041761u, 8371232817323324030u, 15234034981350631406u, 10049106703932351846u, 3740848u},
+ {12707956644941545515u, 11976903329115696638u, 12469301980390122684u, 13327603778702495156u, 1254629u},
+ {9039950576108104416u, 17209421422031733581u, 13593878325969039004u, 12921453166187158463u, 1521546u},
+ {11248333658881489667u, 16727437224260833846u, 11257619938869563541u, 1036873899110820743u, 2318833u},
+ {17879843874279495826u, 4181601617475580607u, 15000982146088610352u, 3861827403947726647u, 824110u},
+ {13578648902386511104u, 13590197159220965458u, 9776560520962997987u, 15795815244426630166u, 3725688u},
+ {7017447564488921474u, 16338797213553232755u, 6783642089315312534u, 16641973457553731316u, 3755983u},
+ {17446940937448854982u, 10737119951556083111u, 6660194076904400201u, 8860312146415385472u, 3473657u},
+ {16519166898418538210u, 17091600978035266948u, 6514747771652737435u, 5252224441269973238u, 1919721u},
+ {4417044329409092931u, 11380051351719377962u, 7522441564903196811u, 4214753061431970503u, 1841622u},
+ {5491857990967617848u, 9024171026985363161u, 5499558347578912177u, 3997139606015302673u, 348159u},
+ {6994480456139902559u, 12313040250767907698u, 14690574092329243413u, 13120904518869982601u, 2135085u},
+ {1749605835261128324u, 9540847690459802115u, 16270580970601591029u, 471730943919088292u, 3546644u},
+ {3749285147232329917u, 18225836298634055362u, 16614336693631217981u, 10369659982083983330u, 906236u},
+ {2743226272316612352u, 494720978589131697u, 5575725903735101804u, 5348979072738709495u, 902588u},
+ {3368311441953910552u, 2002280659113327640u, 9457492639531033449u, 737225755568259789u, 3541191u},
+ {12026781733668095552u, 11397995406927758609u, 6730745515409304376u, 3524087958650890284u, 2961094u},
+ {951703565280633457u, 10944382877554487105u, 12612374439307178584u, 2432523149971906757u, 3258871u},
+ {3678049391738471619u, 6893069984109299175u, 2883083942276601277u, 10148178727048668110u, 1798332u},
+ {8881561970967635567u, 11415923064514023299u, 7846739896879877179u, 9977121357545796140u, 3435576u},
+ {15140395773232853756u, 15949019427562859500u, 894426476091955682u, 17429255364335532700u, 3067248u},
+ {17343315567527744615u, 11986546187690574179u, 2040310762243297928u, 12450486930819457831u, 1902443u},
+ {17947624836606589070u, 18081310986542907202u, 5985151613684732328u, 14302497511096038479u, 3638956u},
+ {1795629144512604728u, 6353876384338179960u, 3807248879631167322u, 3670006606226384126u, 2669315u},
+ {8677459242106997072u, 8003813114481567210u, 18414409997315103887u, 15253392516329915113u, 2568061u},
+ {4549848593366540956u, 14148344854827193065u, 2996704749739267152u, 4875231179827238853u, 215866u},
+ {1258411078263355085u, 8071640287602051658u, 16535261262344243984u, 9515584442737934117u, 3168614u},
+ {4530894777772685112u, 2537700881417552542u, 17010007271659599876u, 15622890933234391735u, 283124u},
+ {14658436266069083432u, 1958346567304787567u, 6015854389490711016u, 16362296578201657348u, 593706u},
+ {2401778765968598525u, 7251070512154274413u, 4674151200882376820u, 155067147075178253u, 275922u},
+ {14207166018946484512u, 4204863376551275335u, 18007125055201591893u, 13551917541803825201u, 10119u},
+ {4936749078018473750u, 1556720238523122447u, 13957313961871519189u, 11817632098375380912u, 1612084u},
+ {1893466179509874520u, 18326974623317736743u, 3370379831240751071u, 7811957868887171175u, 1770409u},
+ {14926971438317492437u, 11182884361940575192u, 8376705575892579410u, 8031051227750750818u, 844059u},
+ {11508141943287493176u, 14962914172118026658u, 3287212790684287614u, 5491947295269685574u, 2068611u},
+ {12203518853080004698u, 3617089502073706471u, 10101639049898170375u, 18124615246716919620u, 483134u},
+ {10458294034992519390u, 16322774878186535045u, 4194128554942565631u, 15357179797454573205u, 606813u},
+ {7272475181693406686u, 15474321324440527285u, 11055742709930368211u, 16007979615888781503u, 948952u},
+ {11027272155779304107u, 8347542244846677935u, 5790210415825921656u, 2971866446245259818u, 1432432u},
+ {14971231974885946753u, 12913247256233115751u, 12648377723538553512u, 17968764624922914430u, 2925732u},
+ {9096847586665120971u, 11332908202750209530u, 14600548915543739907u, 15041949807290037706u, 2634952u},
+ {12751314985995067672u, 15668349264919302017u, 13645428086455660608u, 1561815775028945593u, 566942u},
+ {11563193684702396635u, 1552781408958269503u, 11828492545255311661u, 11467615687570994639u, 1110816u},
+ {1400683406726104317u, 12208172938207697009u, 15843415876437138014u, 17746659083262869692u, 2383573u},
+ {5406308906466419666u, 5127688944296462729u, 6214222207037262306u, 5088000761513618700u, 1080264u},
+ {2389978042491125839u, 14948981118903029532u, 10708802086349539540u, 7513917138572547880u, 2639383u},
+ {11832158916618231760u, 1813043199960936153u, 838234966387375669u, 17126337817216337708u, 312398u},
+ {5700684272435441046u, 7801926537902043096u, 13319893546977646890u, 5193909513812287925u, 1753546u},
+ {1570101120788386704u, 17423072751416593387u, 3879687148014542261u, 11489504671507928427u, 3568293u},
+ {14464154804035843377u, 14124577894714444578u, 10559652613460736609u, 3452530319023126940u, 481637u},
+ {2189077236620091698u, 3171089786171651284u, 6176012944244790385u, 17590937583182375950u, 2553494u},
+ {18288199254312507011u, 11629802359215142600u, 18013656107996006429u, 17124753454891611312u, 1462544u},
+ {4889116951016786383u, 5998709352979101940u, 6461570099486319930u, 4926895586568392565u, 3554236u},
+ {2199975600337289595u, 5847296645763337637u, 8593193283912379856u, 16278522745330246328u, 639489u},
+ {1528656688250066267u, 7353460423402946370u, 14455063314795463713u, 3470561020020016120u, 3101567u},
+ {3309997611820274407u, 7165917856594695028u, 16546699786208060589u, 3804208706119276804u, 1191102u},
+ {7247262146298419974u, 8102554607163688212u, 8636721983754521603u, 886663086019027532u, 36260u},
+ {6985121815884159426u, 3339005953553563912u, 6652332961876980623u, 18159182577384615666u, 836459u},
+ {3775045753865620181u, 11298099168662134230u, 4054288322569509565u, 2825676130739523099u, 2424698u},
+ {15009540523965777788u, 5814927245346129690u, 2282509117946592630u, 17410607949428344109u, 2734920u},
+ {5714078105996605758u, 10456837455245123602u, 6160748199906420014u, 13741393253918985516u, 460852u},
+ {10698833280884522061u, 12003930615174384973u, 3695295091961330649u, 801134767058457744u, 2831945u},
+ {17314883983826999139u, 5091428160819785466u, 3867006901853908082u, 2213967986584794243u, 328921u},
+ {3874904667587726835u, 16276922674913825336u, 219158004127495052u, 14417586331307023806u, 2044083u},
+ {6462729573893065578u, 14810529203087500665u, 12966436553073377484u, 8612560380897431156u, 2220044u},
+ {10992882775436657320u, 5451928658754496942u, 13009054558269493127u, 5517329738995457964u, 3652553u},
+ {11276480754406500102u, 6345866629411296281u, 16217152760168926909u, 499723280151557398u, 1244174u},
+ {2050366996710390896u, 10268184946870732763u, 15197213283395228081u, 7228710363653680387u, 2510223u},
+ {6532193867484341533u, 7457984715706605113u, 8113814323310135212u, 15610745760670083951u, 934917u},
+ {16469990856175821417u, 10102528740535617311u, 9929141208155016529u, 4196586644439726361u, 3420949u},
+ {1415383548198264335u, 7610345376856824064u, 6104580011670919070u, 783681315268078621u, 896010u},
+ {15520506072182345160u, 10389412246267891887u, 11280751816407303678u, 14139816899375041266u, 480051u},
+ {2946249913431227664u, 8152608466925387942u, 6232300417495203228u, 11970416002375182619u, 2747316u},
+ {8125325724997209183u, 13396350733685016202u, 16281931502026287873u, 4590307471501483400u, 1267659u},
+ {16794466152993196567u, 1098757763778858028u, 18326293196870121102u, 1392478472456672831u, 1979936u},
+ {10368277562512346599u, 11193720681604274198u, 4520363714669583110u, 2198251195204640747u, 1673978u},
+ {7184967683087223506u, 8846546202861894722u, 18441010737938086033u, 5219026216680082754u, 2887387u},
+ {4835751726793421944u, 15196833033289030149u, 14608901680839924901u, 10861901999938972617u, 368653u},
+ {11890210545962808297u, 1289662455915373824u, 4996593304567863138u, 15643298364635269359u, 1316082u},
+ {3336554993796378285u, 2048191322387287071u, 13745736545843869859u, 16669581666238667394u, 1034001u},
+ {2727752792486717226u, 6581687957571454482u, 5887518377010538905u, 5567508709408992488u, 1316243u},
+ {12956757253818591693u, 13913997601160102501u, 13958488472393829671u, 9896170786523447622u, 3339119u},
+ {2767695869091116105u, 17414355213661054864u, 14866487952318986598u, 8132046415768760582u, 3062439u},
+ {7501146529116990161u, 5852935974416269097u, 5720556823733168865u, 7550481816550898765u, 3414693u},
+ {12756619068956600691u, 17942153926652316844u, 8987118190203140826u, 8205498101697626074u, 1816737u},
+ {17015936328127409039u, 10136771478937301710u, 6858924948191133665u, 16390586737303378443u, 2140237u},
+ {57068893624212306u, 4857076147447689907u, 17069721106543804309u, 3786475310558923318u, 23879u},
+ {13521054681577729046u, 3175496704063358748u, 17376127936839109697u, 4972414482205239004u, 1866179u},
+ {16667616643150715876u, 16238425037539080919u, 9784785207097591992u, 2412325534632767587u, 2535422u},
+ {4538245396420150238u, 4280696400899147636u, 7565185717904791440u, 46770283165178353u, 1370631u},
+ {14703604926721831111u, 11811609319397813971u, 11181826873128016813u, 9753058874443534239u, 2860867u},
+ {4988305735952570894u, 17806446327762073362u, 4547958265771955199u, 3655653393027376898u, 3539102u},
+ {456147882709526703u, 12714877307156564599u, 10526924677494593346u, 10297463265886173926u, 3147099u},
+ {9885549183067932984u, 4610282344598720356u, 1242128309267264411u, 11803441568307344090u, 2877109u},
+ {10305296386839065299u, 1637601500952734988u, 11947997347810065558u, 6970602332839689838u, 727840u},
+ {11108361518556214403u, 6048296423113065275u, 16378481821564012818u, 10822065390866401492u, 855476u},
+ {3205859866592529305u, 14406112921496106744u, 4513009014306327810u, 7437712943561843551u, 389110u},
+ {14355819235995762550u, 11385337577142860630u, 3526008637419628181u, 7933513164196307027u, 1555999u},
+ {2894580858817241479u, 16914896149213534922u, 14780479720941674...
[truncated]
|
Two major off-by-one errors are fixed in this patch. The first is in
float_to_string.h with length_for_num, which wasn't accounting for the
implicit leading bit when calculating the length of a number, causing
a missing digit on 80 bit float max. The other off-by-one is the
ryu_long_double_constants.h (a.k.a the Mega Table) not having any
entries for the last POW10_OFFSET in POW10_SPLIT. This was also found on
80 bit float max. Finally, the integer calculation mode was using a
slightly too short integer, again on 80 bit float max, not accounting
for the mantissa width. All of these are fixed in this patch.