a
    ò.;bV<  ã                   @   sî  d Z ddlZddlZddlZddlZddlm  mZ	 ddlm
Z
 ddlmZmZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZ dd	lmZ ed
ƒZejZe e d¡¡Zej Z!ej"Z#e e d¡¡Z$e$j Z%e$j"Z&dZ'dZ(dZ)dZ*dd„ Z+dd„ Z,dd„ Z-dd„ Z.dd„ Z/dd„ Z0dd„ Z1dd „ Z2d!d"„ Z3d#d$„ Z4d%d&„ Z5d'd(„ Z6dd*d+„Z7e6ej8e	j9ƒ e6ej:e	j;ƒZ<e6ej=e	j>ƒZ?e6ej@e	jAƒZBe6ejCe	jDƒZEe6ejFe	jGƒZHe7ejId,d-ƒZJe7ejKd.d/ƒZLe7ejMd0d1ƒZNe7ejOd2d3ƒZPe7ejQd4d5ƒZRe7ejSd6d7ƒZTe7ejUd8d9ƒZVe7ejWd:d;ƒZXe7ejYd<d=ƒZZe7ej[d>d?ƒZ\e7ej]d@dAƒZ^e7ej_dBdCƒZ`e7ejadDdEƒZbe7ejcdFdGƒZde7ejedHdIƒZfe7ejgdJdKdLƒZhe7ejidMdNdLƒZje7ejkdOdPƒZle7ejmdQdRƒZne7ejodSdTdLƒZpe7ejqdUdVƒZreejsejtƒdWdX„ ƒZueejsejvƒdYdZ„ ƒZweejxejtƒd[d\„ ƒZyeejxejvƒd]d^„ ƒZzeej{ejtƒd_d`„ ƒZ|eej{ejvƒdadb„ ƒZ}eej~ejtejtƒdcdd„ ƒZeej€ejtƒdedf„ ƒZeej‚ejtejƒƒdgdh„ ƒZ„eej…ej†ej†ƒdidj„ ƒZ‡eej…ejˆejˆƒdkdl„ ƒZ‰eej…ejtejtƒdmdn„ ƒZŠeej‹ej†ej†ƒdodp„ ƒZŒeej‹ejˆejˆƒdqdr„ ƒZeej‹ejtejtƒdsdt„ ƒZŽeejejtƒdudv„ ƒZe5ejeƒ eej‘ejtƒdwdx„ ƒZ’e5ej‘e’ƒ eej“ejtejtƒeej“ejtejvƒdydz„ ƒƒZ”d{d|„ Z•ee•ƒd}d~„ ƒZ–dd€„ Z—eej˜ejvejvƒe—ƒ dS )‚zA
Provide math calls that uses intrinsics or libc math functions.
é    N)ÚType)ÚRegistryÚimpl_ret_untracked)Útypeof)ÚtypesÚutilsÚconfigÚcgutils)Úoverload)Ú	signature)Útrailing_zerosZmathimplÚfloat32Úfloat64iÿÿÿl        l   ÿÿÿÿ l            c                 C   s   |   d||¡S )z<
    Return a condition testing whether *val* is a NaN.
    Zuno)Zfcmp_unordered©ÚbuilderÚval© r   ú5lib/python3.9/site-packages/numba/cpython/mathimpl.pyÚis_nan(   s    r   c                 C   sT   t j |jtdƒ¡}t j |jtdƒ¡}|  t j||¡}|  t j||¡}|  ||¡S )zB
    Return a condition testing whether *val* is an infinite.
    z+infz-inf)ÚlcÚConstantÚrealÚtypeÚfloatZfcmpZFCMP_OEQÚor_)r   r   Zpos_infZneg_infZisposinfZisneginfr   r   r   Úis_inf.   s
    r   c                 C   s   |   ||¡}|  d||¡S )z?
    Return a condition testing whether *val* is a finite.
    Úord)ÚfsubZfcmp_ordered)r   r   Zval_minus_valr   r   r   Ú	is_finite8   s    r   c                 C   s$   |j t ¡ ksJ ‚|  |t d¡¡S )z1
    Bitcast a double into a 64-bit integer.
    é@   )r   r   ÚdoubleÚbitcastÚintr   r   r   r   Úf64_as_int64@   s    r#   c                 C   s$   |j t d¡ksJ ‚|  |t ¡ ¡S )z1
    Bitcast a 64-bit integer into a double.
    r   )r   r   r"   r!   r    r   r   r   r   Úint64_as_f64G   s    r$   c                 C   s$   |j t ¡ ksJ ‚|  |t d¡¡S )z0
    Bitcast a float into a 32-bit integer.
    é    )r   r   r   r!   r"   r   r   r   r   Úf32_as_int32N   s    r&   c                 C   s$   |j t d¡ksJ ‚|  |t ¡ ¡S )z0
    Bitcast a 32-bit integer into a float.
    r%   )r   r   r"   r!   r   r   r   r   r   Úint32_as_f32U   s    r'   c                 C   s   |   tj |jd¡|¡S )zB
    Negate real number *val*, with proper handling of zeros.
    g       €)r   r   r   r   r   r   r   r   r   Únegate_real\   s    r(   c                 C   s,   | j }tj ||dd„ |D ƒ¡}|  ||¡S )z9
    Call a LLVM intrinsic floating-point operation.
    c                 S   s   g | ]
}|j ‘qS r   )r   )Ú.0Úar   r   r   Ú
<listcomp>h   ó    z%call_fp_intrinsic.<locals>.<listcomp>)Úmoduler   ZFunctionZ	intrinsicÚcall)r   ÚnameÚargsÚmodZintrr   r   r   Úcall_fp_intrinsicc   s    r2   c                    s   ‡ fdd„}|S )zŒ
    Return an implementation factory to convert the single integral input
    argument to a float64, then defer to the *wrapped_impl*.
    c           	         sT   |\}|j d }|  |||tj¡}ttjtjƒ}ˆ | |||fƒ}|  ||tj|j¡S )Nr   )r0   Úcastr   r   r   Úreturn_type)	Úcontextr   Úsigr0   r   Ú
input_typeZfpvalZ	inner_sigÚres©Úwrapped_implr   r   Úimplementerq   s    
z2_unary_int_input_wrapper_impl.<locals>.implementerr   )r:   r;   r   r9   r   Ú_unary_int_input_wrapper_impll   s    r<   c                 C   s   t |ƒ}t| tjƒ|ƒ d S ©N)r<   Úlowerr   ÚInteger)ÚfnÚ
float_implÚimplr   r   r   Úunary_math_int_impl{   s    rC   c                    s&   t | tjƒ‡ fdd„ƒ}t| |ƒ |S )zO
    Implement the math function *fn* using the LLVM intrinsic *intrcode*.
    c                    s   t |ˆ |ƒ}t| ||j|ƒS r=   )r2   r   r4   ©r5   r   r6   r0   r8   ©Úintrcoder   r   rA   ƒ   s    z#unary_math_intr.<locals>.float_impl)r>   r   ÚFloatrC   )r@   rF   rA   r   rE   r   Úunary_math_intr   s    

rH   Fc                    s:   |r
t jnd}‡ ‡fdd„}t| t jƒ|ƒ t| |ƒ |S )a!  
    Register implementations of Python function *fn* using the
    external function named *f32extern* and *f64extern* (for float32
    and float64 inputs, respectively).
    If *int_restype* is true, then the function's return value should be
    integral, otherwise floating-point.
    Nc                    s„   |\}|j }|jd }|  |¡}tjˆ tjˆi| }t ||g¡}	tj	|j |	|d}
| 
|
|f¡}|  ||||j¡}t| ||j|ƒS )z9
        Implement *fn* for a types.Float input.
        r   ©r/   )r-   r0   Úget_value_typer   r   r   r   Úfunctionr	   Úinsert_pure_functionr.   r3   r4   r   )r5   r   r6   r0   r   r1   r7   ÚltyÚ	func_nameÚfntyr@   r8   ©Ú	f32externÚ	f64externr   r   rA   •   s    

þýz%unary_math_extern.<locals>.float_impl)r   Úint64r>   rG   rC   )r@   rQ   rR   Zint_restypeZ	f_restyperA   r   rP   r   Úunary_math_extern‹   s
    
rT   Zlog1pfÚlog1pZexpm1fÚexpm1ZerffÚerfZerfcfÚerfcZtanfÚtanZasinfÚasinZacosfÚacosZatanfÚatanZasinhfÚasinhZacoshfÚacoshZatanhfÚatanhZsinhfÚsinhZcoshfÚcoshZtanhfÚtanhZlog2fÚlog2ZceilfÚceilTZfloorfÚfloorZnumba_gammafZnumba_gammaZsqrtfÚsqrtZtruncfÚtruncZlgammafÚlgammac                 C   s    |\}t ||ƒ}t| ||j|ƒS r=   )r   r   r4   ©r5   r   r6   r0   r   r8   r   r   r   Úisnan_float_implÕ   s    
rj   c                 C   s   t j}t| ||j|ƒS r=   ©r	   Z	false_bitr   r4   rD   r   r   r   Úisnan_int_implÛ   s    rl   c                 C   s    |\}t ||ƒ}t| ||j|ƒS r=   )r   r   r4   ri   r   r   r   Úisinf_float_implá   s    
rm   c                 C   s   t j}t| ||j|ƒS r=   rk   rD   r   r   r   Úisinf_int_implç   s    rn   c                 C   s    |\}t ||ƒ}t| ||j|ƒS r=   )r   r   r4   ri   r   r   r   Úisfinite_float_implí   s    
ro   c                 C   s   t j}t| ||j|ƒS r=   )r	   Ztrue_bitr   r4   rD   r   r   r   Úisfinite_int_implô   s    rp   c                 C   sN   |d j }|j}t |tj |||f¡d|j ¡}| ||¡}t	| ||j
|ƒS )Nr   zllvm.copysign.%s)r   r-   r	   Úget_or_insert_functionr   r   rK   Zintrinsic_namer.   r   r4   )r5   r   r6   r0   rM   r1   r@   r8   r   r   r   Úcopysign_float_implú   s    
ÿrr   c                 C   s¤   |\}|   |jd ¡}|   |jd ¡}tj||dd}t ||t |¡f¡}dddœt|ƒ }	t 	|j
||	¡}
| |
||f¡}t ||| |¡f¡}t| ||j|ƒS )Nr   é   ÚexprI   Znumba_frexpfZnumba_frexp©r   r    )Úget_data_typer0   r4   r	   Zalloca_oncer   rK   ZpointerÚstrrq   r-   r.   Zmake_anonymous_structÚloadr   )r5   r   r6   r0   r   ÚflttyÚinttyZexpptrrO   Úfnamer@   r8   r   r   r   Ú
frexp_impl  s    þýr|   c                 C   sn   |\}}t | j|jƒ\}}t |||f¡}dddœt|ƒ }	tj|j||	d}
| 	|
||f¡}t
| ||j|ƒS )NZnumba_ldexpfZnumba_ldexpru   rI   )Úmaprv   r0   r   rK   rw   r	   rL   r-   r.   r   r4   )r5   r   r6   r0   r   rt   ry   rz   rO   r{   r@   r8   r   r   r   Ú
ldexp_impl  s    þýr~   c                 C   sL   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒS r=   )Úsitofpr   r    r   r   r   Úatan2_float_impl©r5   r   r6   r0   ÚyÚxÚfsigr   r   r   Úatan2_s64_impl)  s
    r…   c                 C   sL   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒS r=   )Zuitofpr   r    r   r   r   r€   r   r   r   r   Úatan2_u64_impl1  s
    r†   c                 C   s|   t |ƒdksJ ‚|j}|jd }|  |¡}tjdtjdi| }t |||f¡}t	j
|j||d}	| |	|¡}
t| ||j|
ƒS )Né   r   Zatan2fÚatan2rI   )Úlenr-   r0   rJ   r   r   r   r   rK   r	   rL   r.   r   r4   )r5   r   r6   r0   r1   ZtyrM   rN   rO   r@   r8   r   r   r   r€   9  s    

þýr€   c                 C   s\   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒ}t| ||j|ƒS r=   ©	r   r   r    r   r   r   Úhypot_float_implr   r4   ©r5   r   r6   r0   rƒ   r‚   r„   r8   r   r   r   Úhypot_s64_implL  s    r   c                 C   s\   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒ}t| ||j|ƒS r=   rŠ   rŒ   r   r   r   Úhypot_u64_implV  s    rŽ   c                    sÆ   |j \}}||  kr |jks&n J ‚|\}}tjtjdkr@dndtjtjdkrTdndi| }t ||¡‰tjdkrštj	dkrš|t
dƒƒ‰ ‡ ‡fdd	„}	n‡fd
d	„}	|  ||	||¡}
t| ||j|
ƒS )NZwin32Z_hypotfZhypotfZ_hypotÚhypotr%   Úinfc                    s"   t  | ¡st  |¡rˆ S ˆ| |ƒS r=   )ÚmathÚisinf©rƒ   r‚   ©r   Ú
plat_hypotr   r   Ú
hypot_implq  s    z$hypot_float_impl.<locals>.hypot_implc                    s
   ˆ | |ƒS r=   r   r“   )r•   r   r   r–   v  s    )r0   r4   r   r   ÚsysÚplatformr   ZExternalFunctionr   ZMACHINE_BITSr   Úcompile_internalr   )r5   r   r6   r0   ÚxtyÚytyrƒ   r‚   r{   r–   r8   r   r”   r   r‹   `  s    
þýr‹   c                 C   s6   |\}|   |jtjd ¡}| ||¡}t| ||j|ƒS ©Né´   ©Zget_constantr4   r‘   ZpiZfmulr   ©r5   r   r6   r0   rƒ   Zcoefr8   r   r   r   Úradians_float_impl  s    r    c                 C   s6   |\}|   |jdtj ¡}| ||¡}t| ||j|ƒS rœ   rž   rŸ   r   r   r   Údegrees_float_implŠ  s    r¡   c                 C   s   |   tj|¡}|||ƒS r=   )Zget_functionÚoperatorÚpow)r5   r   r6   r0   rB   r   r   r   Úpow_impl•  s    r¤   c                 C   s   dS )z8Convert integer to unsigned integer of equivalent width.Nr   ©ÚTr   r   r   Ú	_unsignedž  s    r§   c                    s>   | t jv rdd„ S | t jv r:tt d | j¡ƒ‰ ‡ fdd„S d S )Nc                 S   s   | S r=   r   r¥   r   r   r   Ú<lambda>¥  r,   z _unsigned_impl.<locals>.<lambda>zuint{}c                    s   ˆ | ƒS r=   r   r¥   ©ZnewTr   r   r¨   ¨  r,   )r   Zunsigned_domainZsigned_domainÚgetattrÚformatZbitwidthr¥   r   r©   r   Ú_unsigned_impl¢  s
    

r¬   c           
      C   sV   |j \}}||  kr |jks&n J ‚|\}}dd„ }|  ||||¡}	t| ||j|	ƒS )Nc           	      S   s²   t | ƒ}| dkrt|ƒS |dkr(t| ƒS t| ƒ}t|ƒ}t||ƒ}ttt | |¡ƒƒ}ttt ||¡ƒƒ}||krž||kr„|| }}||8 }t |t|ƒ¡}qjt ||ƒ|¡}|S )zO
        Stein's algorithm, heavily cribbed from Julia implementation.
        r   )r   Úabsr   Úminr§   ÚnpZright_shiftZ
left_shift)	r*   Úbr¦   ZzaZzbÚkÚuÚvÚrr   r   r   Úgcd°  s    

zgcd_impl.<locals>.gcd)r0   r4   r™   r   )
r5   r   r6   r0   rš   r›   rƒ   r‚   rµ   r8   r   r   r   Úgcd_impl«  s    
r¶   )F)™Ú__doc__r‘   r¢   r—   Znumpyr¯   Zllvmlite.llvmpy.coreZllvmpyZcorer   r   Znumba.core.imputilsr   r   Znumbar   Z
numba.corer   r   r   r	   Znumba.core.extendingr
   Znumba.core.typingr   Znumba.cpython.unsafe.numbersr   Úregistryr>   ZfinfoZdtypeZ_NP_FLT_FINFOÚmaxZFLT_MAXZtinyZFLT_MINZ_NP_DBL_FINFOZDBL_MAXZDBL_MINZFLOAT_ABS_MASKZFLOAT_SIGN_MASKZDOUBLE_ABS_MASKZDOUBLE_SIGN_MASKr   r   r   r#   r$   r&   r'   r(   r2   r<   rC   rH   rT   ZfabsZ	INTR_FABSrt   ZINTR_EXPZexp_implÚlogZINTR_LOGZlog_implZlog10Z
INTR_LOG10Z
log10_implZsinZINTR_SINZsin_implZcosZINTR_COSZcos_implrU   Z
log1p_implrV   Z
expm1_implrW   Zerf_implrX   Z	erfc_implrY   Ztan_implrZ   Z	asin_implr[   Z	acos_implr\   Z	atan_implr]   Z
asinh_implr^   Z
acosh_implr_   Z
atanh_implr`   Z	sinh_implra   Z	cosh_implrb   Z	tanh_implrc   Z	log2_implrd   Z	ceil_implre   Z
floor_implZgammaZ
gamma_implrf   Z	sqrt_implrg   Z
trunc_implrh   Zlgamma_implZisnanrG   rj   r?   rl   r’   rm   rn   Zisfinitero   rp   Zcopysignrr   Zfrexpr|   ZldexpZintcr~   rˆ   rS   r…   Zuint64r†   r€   r   r   rŽ   r‹   Zradiansr    Zdegreesr¡   r£   r¤   r§   r¬   r¶   rµ   r   r   r   r   Ú<module>   sØ   
	
$












	
	



