
    F\h6.                     d   S r SSKrSSKr SSKrSS jr\R                  " 5       R                  5       r\R                  \l
        \R                  \l        \R                  \l        S\R                  \R                   '   S rS rS rS rS	 rS
rS rS rS rS rS rS rg! \ a    Sr Nf = f)a  Python 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                    [        5       n[        5       nU 1nU(       a{  UR                  5       n X;   d  X::  a  M#  UR                  U 5        U S-	  nUR                  U5        UR                  U5        U S-  (       a  UR                  US-   5        U(       a  M{  0 nU(       d  U$ [        [	        U5      5      n	[        U	5      n
U(       a  [        SU
5        X-  X'   U	 H  nUS-
  U;   a!  U(       a  [        SU5        XS-
     U-  X'   M-  US-	  nX-
  nXx;   d   eU(       a  [        SU5        X   X   -  nX:w  a   XS-   :X  d   eU(       a  [        S5        X-  nXU'   M     U$ )N   zpow atz	* base atz	square atz    and * base)setpopadditersortednextprint)wbase	more_thanshowseenneedwsloditfirstthishisqs                 /usr/lib/python3.13/_pylong.pycompute_powersr   3   sQ   5D5D
B
FFH9!V
r
q5FF26N " 	A	fTl	BHEh}AH!8q=k4(qkD(AGBB7N7k4( Bx!V|#|*+
dG' ( H    r   c                    ^^^^ SSK Jm  SmUUUU4S jm[         R                  " [        5         U R	                  5       n[        UT" S5      T5      mU S:  a  SnU * n OSnT" X5      nU(       a  U* nSSS5        U$ ! , (       d  f       W$ = f)	z6Asymptotically fast conversion of an 'int' to Decimal.r   )Decimal   c                 |   > UT::  a  T" U 5      $ US-	  nX-	  nU SU-  S-
  -  nT" XB5      T" X1U-
  5      TU   -  -   $ Nr    )	nr   w2r   r   BITLIMDinnerw2pows	        r   r'   int_to_decimal.<locals>.innery   sV    ;Q4K!VW17a- R}uRR059<<<r      TFN)decimalr   localcontext_unbounded_dec_context
bit_lengthr   )r#   nbitsnegateresultr%   r&   r'   r(   s       @@@@r   int_to_decimalr2   g   s     %F= = 
		4	5uadF3q5FAFqWF 
6 M 
6	5 Ms   AA>>
Bc                 p  ^^^ U R                  5       nUS:  a  [        b  [        [        U 5      5      $ SmUUU4S jm[	        US-  S-   5      n[        UST5      mTR                  5        H  u  p#X2-  TU'   M     U S:  a  U * n SnOS	nT" X5      nUS   S
:X  a  U (       a  UR                  S
5      nXE-   $ )z?Asymptotically fast conversion of an 'int' to a decimal string.i i  c                    > UT::  a  [        U 5      $ US-	  n[        U TU   5      u  p4T" X1U-
  5      T" XB5      R                  U5      -   $ r!   )strdivmodzfill)r#   r   r$   r   r   DIGLIMr'   pow10s        r   r'   $int_to_decimal_string.<locals>.inner   sQ    ;q6M!V59%RR 5=#6#6r#:::r   gyPD?r      r   - 0)r.   _decimalr5   r2   intr   itemslstrip)	r#   r   kvsignsr8   r'   r9   s	         @@@r   int_to_decimal_stringrG      s    	A7{x+ >!$%% F; 	A""Q&'A1a(E6a 1uBaAts{q HHSM8Or   c                 p   ^ ^^^ SmUUU U4S jm[        [        T 5      ST5      mT" S[        T 5      5      $ )z6Asymptotically fast conversion of a 'str' to an 'int'.i   c                 ~   > X-
  T::  a  [        TX 5      $ X-   S-   S-	  nT" X!5      T" X5      TX-
     -  X-
  -  -   $ r!   )r@   )abmidr8   r'   rF   w5pows      r   r'    _str_to_int_inner.<locals>.inner   sW    5F?qv;uqyQc!ME!'N2!" 	#r   r;   r   )r   len)rF   r8   r'   rM   s   `@@@r   _str_to_int_innerrP      s9     F# # 3q61f-ECFr   c                 X    U R                  5       R                  SS5      n [        U 5      $ )zkAsymptotically fast version of PyLong_FromString(), conversion
of a string of decimal digits into an 'int'._r=   )rstripreplacerP   )rF   s    r   int_from_stringrU      s'     	

3#AQr   c                     [         R                  " SU 5      nU(       d  [        S5      e[        UR	                  S5      5      nUR	                  S5      S:X  a  U* nU$ )zBAsymptotically fast version of decimal string to 'int' conversion.z\s*([+-]?)([0-9_]+)\s*z&invalid literal for int() with base 10r*   r   r<   )rematch
ValueErrorrU   group)rF   mrD   s      r   
str_to_intr\      sR     	*A.AABB
#AwwqzSBHr   i  c                 (   U R                  5       U-
  [        ::  a  [        X5      $ US-  nU(       a  U S-  n US-  nUS-  nUS-	  nSU-  S-
  nX-	  X-  pv[        X-	  X-	  U-  XXt5      u  p[        XU-  XXt5      u  pU(       a  U	S-  n	X-  U
-  U	4$ )a2  Divide 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_LIMITr6   _div3n2n)rJ   rK   r#   padhalf_nmaskb1b2q1rq2s              r   _div2n1nrh      s     	||~Z'a|
a%C
	a	a	Q!VFK1D[!(QVakT11"EEBQD!4EB
	a<"ar   c                     X-	  U:X  a  SU-  S-
  XU-  -
  U-   pvO[        XU5      u  pgXu-  U-  Xd-  -
  nUS:  a  US-  nXr-  nUS:  a  M  Xg4$ )zAHelper function for _div2n1n; not intended to be called directly.r   r   )rh   )a12a3rK   rc   rd   r#   qrf   s           r   r_   r_     sn    
x2~Q!|S!G_r11#	
"A
a%	Q	 a% 4Kr   c                    ^^^ S/U R                  5       T-   S-
  T-  -  mUUU4S jmU (       a  T" U S[        T5      5        T$ )a2  Decompose 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.

r   r   c                 z   > US-   U:X  a  U TU'   g X-   S-	  nX1-
  T	-  nX-	  nXU-  -  nT" XaU5        T" XSU5        g r!   r"   )
xLRrL   shiftupperlowera_digitsr'   r#   s
          r   r'   _int2digits.<locals>.inner.  sW    q5A:HQKulA
e^$ee!r   )r.   rO   )rJ   r#   ru   r'   s    `@@r   _int2digitsrw     sE     sq||~)A-!34H	 	aCM"Or   c                 N   ^ ^^ U UU4S jmT (       a  T" S[        T 5      5      $ S$ )zxCombine base-2**n digits into an int. This function is the
inverse of `_int2digits`. For more details, see _int2digits.
c                 d   > U S-   U:X  a  TU    $ X-   S-	  nX -
  T-  nT" X!5      U-  T" X5      -   $ r!   r"   )rp   rq   rL   rr   digitsr'   r#   s       r   r'   _digits2int.<locals>.innerC  sF    q5A:!9ulAc&%-77r   r   )rO   )rz   r#   r'   s   ``@r   _digits2intr|   >  s"    
8 %+5CK 11r   c                     UR                  5       n[        X5      nSn/ n[        U5       H'  n[        XB-  U-   X5      u  ptUR	                  U5        M)     UR                  5         [        XR5      nX4$ )zWDivide a non-negative integer a by a positive integer b, giving
quotient and remainder.r   )r.   rw   reversedrh   appendreverser|   )	rJ   rK   r#   ru   rf   q_digitsa_digitq_digitrl   s	            r   _divmod_posr   M  sr     	
A1 H	AHH%qv0!7
  & H A4Kr   c                     US:X  a  [         eUS:  a  [        U * U* 5      u  p#X#* 4$ U S:  a  [        U ) U5      u  p#U) X) -   4$ [        X5      $ )zyAsymptotically fast replacement for divmod, for 'int'.
Its time complexity is O(n**1.58), where n = #bits(a) + #bits(b).
r   )ZeroDivisionError
int_divmodr   )rJ   rK   rl   rf   s       r   r   r   ^  se     	Av	
Q1"qb!"u	
Q1"a r1r6z1  r   )F)__doc__rW   r+   r?   ImportErrorr   
getcontextcopyr-   MAX_PRECprecMAX_EMAXEmaxMIN_EMINEmintrapsInexactr2   rG   rP   rU   r\   r^   rh   r_   rw   r|   r   r   r"   r   r   <module>r      s   > 
 B,\ !++-224 %..  %..  %..  01   W__ -%N+Z6 	" 
 <
>2"!W
  Hs   B$ $B/.B/