fW#^dZddlZddlZdZdZdZdZdZdZd Z d Z d Z d Z d Z dZy)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 /usr/lib/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<<.w5powiss ggaj F )F{AQ#QUa!V rU1r6]2CF rc||z krt||S||zdzdz }||||||z z||z zzSr)int)abmidr1rsr2s rrz _str_to_int_inner..inner~s] q5F?q1v; 1uqyQS!}q#q3w!?QW MNNrrlen)r8r1rrr2s`@@@@r_str_to_int_innerr;Xs-F C*O CF rcX|jjdd}t|S)zoAsymptotically fast version of PyLong_FromString(), conversion of a string of decimal digits into an 'int'._)rstripreplacer;)r8s rint_from_stringrAs'  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 10rr-)rematch ValueErrorrAgroup)r8mvs r str_to_intrJsR *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_LIMITdivmod_div3n2n) r5r6rpadhalf_nmaskb1b2q1rq2s r_div2n1nrWs ||~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)rW)a12a3r6rRrSrqrUs rrNrNs 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) xLRr7shiftupperlowera_digitsrrs rrz_int2digits..innersc q5A:HQK 1ulqA U Ue^$ eQ eS!r)r'r:)r5rrdrs `@@r _int2digitsresEsq||~)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)r_r`r7radigitsrrs rrz_digits2int..innersN q5A:!9 1ulqA c1 &%3-77rrr9)rhrrs``@r _digits2intris" 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'rereversedrWappendreverseri) r5r6rrdrUq_digitsa_digitq_digitr[s r _divmod_posrqs 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_divmodrq)r5r6r[rUs rrtrtso Av Q1"qb!11"u Q1"a 1r1r6z1a  r)__doc__rDrr*r-r;rArJrLrWrNrerirqrtrrrrvsR > =@" ,^   "  < > 2" !r