xref: /llvm-project/libc/utils/mathtools/GenerateHPDConstants.py (revision 69c0b2febe01108f50db6e8ed21cd8b2e6088caf)
16f80339bSMichael Jonesfrom math import *
26f80339bSMichael Jones
3*f98ee40fSTobias Hieta"""
46f80339bSMichael JonesThis script is used to generate a table used by
56f80339bSMichael Joneslibc/src/__support/high_precision_decimal.h.
66f80339bSMichael Jones
76f80339bSMichael JonesFor the ith entry in the table there are two values (indexed starting at 0).
86f80339bSMichael JonesThe first value is the number of digits longer the second value would be if
96f80339bSMichael Jonesmultiplied by 2^i.
106f80339bSMichael JonesThe second value is the smallest number that would create that number of
116f80339bSMichael Jonesadditional digits (which in base ten is always 5^i). Anything less creates one
126f80339bSMichael Jonesfewer digit.
136f80339bSMichael Jones
146f80339bSMichael JonesAs an example, the 3rd entry in the table is {1, "125"}. This means that if
156f80339bSMichael Jones125 is multiplied by 2^3 = 8, it will have exactly one more digit.
166f80339bSMichael JonesMultiplying it out we get 125 * 8 = 1000. 125 is the smallest number that gives
176f80339bSMichael Jonesthat extra digit, for example 124 * 8 = 992, and all larger 3 digit numbers
186f80339bSMichael Jonesalso give only one extra digit when multiplied by 8, for example 8 * 999 = 7992.
196f80339bSMichael JonesThis makes sense because 5^3 * 2^3 = 10^3, the smallest 4 digit number.
206f80339bSMichael Jones
216f80339bSMichael JonesFor numbers with more digits we can ignore the digits past what's in the second
226f80339bSMichael Jonesvalue, since the most significant digits determine how many extra digits there
236f80339bSMichael Joneswill be. Looking at the previous example, if we have 1000, and we look at just
246f80339bSMichael Jonesthe first 3 digits (since 125 has 3 digits), we see that 100 < 125, so we get
256f80339bSMichael Jonesone fewer than 1 extra digits, which is 0.
266f80339bSMichael JonesMultiplying it out we get 1000 * 8 = 8000, which fits the expectation.
276f80339bSMichael JonesAnother few quick examples:
286f80339bSMichael JonesFor 1255, 125 !< 125, so 1 digit more: 1255 * 8 = 10040
296f80339bSMichael JonesFor 9999, 999 !< 125, so 1 digit more: 9999 * 8 = 79992
306f80339bSMichael Jones
316f80339bSMichael JonesNow let's try an example with the 10th entry: {4, "9765625"}. This one means
326f80339bSMichael Jonesthat 9765625 * 2^10 will have 4 extra digits.
336f80339bSMichael JonesLet's skip straight to the examples:
346f80339bSMichael JonesFor 1, 1 < 9765625, so 4-1=3 extra digits: 1 * 2^10 = 1024, 1 digit to 4 digits is a difference of 3.
356f80339bSMichael JonesFor 9765624, 9765624 < 9765625 so 3 extra digits: 9765624 * 1024 = 9999998976, 7 digits to 10 digits is a difference of 3.
366f80339bSMichael JonesFor 9765625, 9765625 !< 9765625 so 4 extra digits: 9765625 * 1024 = 10000000000, 7 digits to 11 digits is a difference of 4.
376f80339bSMichael JonesFor 9999999, 9999999 !< 9765625 so 4 extra digits: 9999999 * 1024 = 10239998976, 7 digits to 11 digits is a difference of 4.
386f80339bSMichael JonesFor 12345678, 1234567 < 9765625 so 3 extra digits: 12345678 * 1024 = 12641974272, 8 digits to 11 digits is a difference of 3.
396f80339bSMichael Jones
406f80339bSMichael Jones
416f80339bSMichael JonesThis is important when left shifting in the HPD class because it reads and
426f80339bSMichael Joneswrites the number backwards, and to shift in place it needs to know where the
436f80339bSMichael Joneslast digit should go. Since a binary left shift by i bits is the same as
446f80339bSMichael Jonesmultiplying by 2^i we know that looking up the ith element in the table will
456f80339bSMichael Jonestell us the number of additional digits. If the first digits of the number
466f80339bSMichael Jonesbeing shifted are greater than or equal to the digits of 5^i (the second value
476f80339bSMichael Jonesof each entry) then it is just the first value in the entry, else it is one
486f80339bSMichael Jonesfewer.
49*f98ee40fSTobias Hieta"""
506f80339bSMichael Jones
516f80339bSMichael Jones
526f80339bSMichael Jones# Generate Left Shift Table
536f80339bSMichael JonesoutStr = ""
546f80339bSMichael Jonesfor i in range(61):
556f80339bSMichael Jones    tenToTheI = 10**i
566f80339bSMichael Jones    fiveToTheI = 5**i
576f80339bSMichael Jones    outStr += "{"
586f80339bSMichael Jones    # The number of new digits that would be created by multiplying 5**i by 2**i
596f80339bSMichael Jones    outStr += str(ceil(log10(tenToTheI) - log10(fiveToTheI)))
606f80339bSMichael Jones    outStr += ', "'
616f80339bSMichael Jones    if not i == 0:
626f80339bSMichael Jones        outStr += str(fiveToTheI)
636f80339bSMichael Jones    outStr += '"},\n'
646f80339bSMichael Jones
656f80339bSMichael Jonesprint(outStr)
66