Qf&*dZddlZddlZ ddlZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy#e$rdZY-wxYw)aPython implementations of some algorithms for use by longobject.c. The goal is to provide asymptotically faster algorithms that can be used for operations on integers with many digits. In those cases, the performance overhead of the Python implementation is not significant since the asymptotic behavior is what dominates runtime. Functions provided by this module should be considered private and not part of any public API. Note: for ease of maintainability, please prefer clear code and avoid "micro-optimizations". This module will only be imported and used for integers with a huge number of digits. Saving a few microseconds with tricky or non-obvious code is not worth it. For people looking for maximum performance, they should use something like gmpy2.Nc tjddi fd  fdtj5}tj|_tj |_tj|_d|jtj<|dkrd}| }nd}||j}|r| }d d d |S#1swYSxYw) z6Asymptotically fast conversion of an 'int' to Decimal.cj|x}>|kr|z}n.|dz vr|dz x}|z}n|dz }|||z z}||<|S)zReturn D(2)**w and store the result. Also possibly save some intermediate results. In context, these are likely to be reused across various levels of the conversion to Decimal.get)wresulttw2BITLIMD2memw2pows ./opt/alt/python312/lib64/python3.12/_pylong.pyrzint_to_decimal..w2pow+sxggaj F )F{QQ#"1q5z/!Q.!V rU1r6]2CF c|kr|S|dz }||z }|||zz }|||||z |zzSNr) nr r hilorDinnerrs rrzint_to_decimal..inner>sW ;Q4K !V "W "(^R}uRR059<<.innerds{ 9q6M !V OOB  9"#R%2+ -A B1BRR 5R=#6#6r#:::rgyPD?rr-0)r'_decimalr.r*intlstrip)rr signsrr2s @@rint_to_decimal_stringr;Ws A7{x+>!$%% ;" A" "Q &'AK1u B a Ats{q HHSM !8OrcVdifdfddtS)z6Asymptotically fast conversion of a 'str' to an 'int'.icj|x}<|krd|z}n,|dz vr |dz dz}n|dz }|||z z}||<|S)zReturn 5**w and store the result. Also possibly save some intermediate results. In context, these are likely to be reused across various levels of the conversion to 'int'. r-rr)r r r DIGLIMrw5pows rr?z _str_to_int_inner..w5powss ggaj F )F{AQ#QUa!V rU1r6]2CF rc||z krt||S||zdzdz }||||||z z||z zzSr)r7)abmidr>rr:r?s rrz _str_to_int_inner..inners] q5F?q1v; 1uqyQS!}q#q3w!?QW MNNrrlen)r:r>rrr?s`@@@@r_str_to_int_innerrFs-F C*O CF rcX|jjdd}t|S)zoAsymptotically fast version of PyLong_FromString(), conversion of a string of decimal digits into an 'int'._r4)rstripreplacerF)r:s rint_from_stringrKs'  3#A Q rctjd|}|s tdt|j d}|j ddk(r| }|S)zBAsymptotically fast version of decimal string to 'int' conversion.z\s*([+-]?)([0-9_]+)\s*z&invalid literal for int() with base 10rrr3)rematch ValueErrorrKgroup)r:mvs r str_to_intrSsR *A.A ABB #AwwqzS B Hric.|j|z tkr t||S|dz}|r|dz}|dz}|dz }|dz }d|zdz }||z ||z}}t||z ||z |z||||\}} t| ||z||||\} } |r| dz} ||z| z| fS)aRDivide a 2n-bit nonnegative integer a by an n-bit positive integer b, using a recursive divide-and-conquer algorithm. Inputs: n is a positive integer b is a positive integer with exactly n bits a is a nonnegative integer such that a < 2**n * b Output: (q, r) such that a = b*q+r and 0 <= r < b. r)r' _DIV_LIMITr/_div3n2n) rArBrpadhalf_nmaskb1b2q1rq2s r_div2n1nr_s ||~Z'a| a%C  a a Q !VF K1 D &[!d(B Q!Va6kT11b"f EEB QD!RV 4EB  a <" a rc||z |k(rd|zdz |||zz |z}}nt|||\}}||z|z||zz }|dkr|dz}||z }|dkr||fS)zAHelper function for _div2n1n; not intended to be called directly.rr)r_)a12a3rBrZr[rqr]s rrVrVs ax2~Q!|SB!G_r11R#1 a"BA a% Q Q a% a4Krc~dg|jzdz zzfd|r|dtS)aRDecompose non-negative int a into base 2**n Input: a is a non-negative integer Output: List of the digits of a in base 2**n in little-endian order, meaning the most significant digit is last. The most significant digit is guaranteed to be non-zero. If a is 0 then the output is an empty list. rrc|dz|k(r||<y||zdz }||z z}||z }|||zz }||||||yrr) xLRrCshiftupperlowera_digitsrrs rrz_int2digits..inner sc q5A:HQK 1ulqA U Ue^$ eQ eS!r)r'rE)rArrlrs `@@r _int2digitsrmsEsq||~)A-!34H   aCM" OrcBfdrdtSdS)zCombine base-2**n digits into an int. This function is the inverse of `_int2digits`. For more details, see _int2digits. cj|dz|k(r|S||zdz }||z z}|||z||zSrr)rgrhrCridigitsrrs rrz_digits2int..inner!sN q5A:!9 1ulqA c1 &%3-77rrrD)rprrs``@r _digits2intrqs" 8%+5CK 11rc|j}t||}d}g}t|D])}t||z|z||\}}|j |+|j t ||}||fS)z[Divide a non-negative integer a by a positive integer b, giving quotient and remainder.r)r'rmreversedr_appendreverserq) rArBrrlr]q_digitsa_digitq_digitrcs r _divmod_posry+s~ A1a H AHH%qAv0!Q7  & Ha A a4Krc|dk(rt|dkrt| | \}}|| fS|dkrt||\}}|||zfSt||S)zAsymptotically fast replacement for divmod, for 'int'. Its time complexity is O(n**1.58), where n = #bits(a) + #bits(b). r)ZeroDivisionError int_divmodry)rArBrcr]s rr|r|<so Av Q1"qb!11"u Q1"a 1r1r6z1a  r)__doc__rMrr6 ImportErrorr*r;rFrKrSrUr_rVrmrqryr|rrrrsr >  =@*Z,^   "  < > 2" !S Hs 4>>