B
      ›\$8  ã               @   sÜ  d Z ddlmZmZm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mZmZmZ ddlmZ eƒ 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„ Z.dd„ Z/dd„ Z0dd„ Z1d d!„ Z2d"d#„ Z3d$d%„ Z4dzd'd(„Z5e4ej6ej7ƒ e4ej8ej9ƒZ:e4ej;ej<ƒZ=e4ej>ej?ƒZ@e4ejAejBƒZCe4ejDejEƒZFe5ejGd)d*ƒZHe5ejId+d,ƒZJe5ejKd-d.ƒZLe5ejMd/d0ƒZNe5ejOd1d2ƒZPe5ejQd3d4ƒZRe5ejSd5d6ƒZTe5ejUd7d8ƒZVe5ejWd9d:ƒZXe5ejYd;d<ƒZZe5ej[d=d>ƒZ\e5ej]d?d@ƒZ^e5ej_dAdBƒZ`e5ejadCdDƒZbejcdEkrœe5ejddFdGƒZee5ejfdHdIdJƒZge5ejhdKdLdJƒZine5ejfdHdIƒZge5ejhdKdLƒZie5ejjdMdNƒZke5ejldOdPƒZme5ejndQdRdJƒZoe5ejpdSdTƒZqeejrejsƒdUdV„ ƒZteejrejuƒdWdX„ ƒZveejwejsƒdYdZ„ ƒZxeejwejuƒd[d\„ ƒZyejcd]kr‚eejzejsƒd^d_„ ƒZ{eejzejuƒd`da„ ƒZ|eej}ejsejsƒdbdc„ ƒZ~eejejsƒddde„ ƒZ€eejejsej‚ƒdfdg„ ƒZƒeej„ej…ej…ƒdhdi„ ƒZ†eej„ej‡ej‡ƒdjdk„ ƒZˆeej„ejsejsƒdldm„ ƒZ‰eejŠej…ej…ƒdndo„ ƒZ‹eejŠej‡ej‡ƒdpdq„ ƒZŒeejŠejsejsƒdrds„ ƒZeejŽejsƒdtdu„ ƒZe3ejŽeƒ eejejsƒdvdw„ ƒZ‘e3eje‘ƒ eej’ejsejsƒeej’ejsejuƒdxdy„ ƒƒZ“dS ){zA
Provide math calls that uses intrinsics or libc math functions.
é    )Úprint_functionÚabsolute_importÚdivisionN)ÚType)ÚRegistryÚimpl_ret_untracked)ÚtypesÚcgutilsÚutilsÚconfig)Ú	signatureÚ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.7/site-packages/numba/targets/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_finite5   s    r   c             C   s$   |j t ¡ kst‚|  |t d¡¡S )z1
    Bitcast a double into a 64-bit integer.
    é@   )r   r   ÚdoubleÚAssertionErrorÚbitcastÚint)r   r   r   r   r   Úf64_as_int64=   s    r#   c             C   s$   |j t d¡kst‚|  |t ¡ ¡S )z1
    Bitcast a 64-bit integer into a double.
    r   )r   r   r"   r    r!   r   )r   r   r   r   r   Úint64_as_f64D   s    r$   c             C   s$   |j t ¡ kst‚|  |t d¡¡S )z0
    Bitcast a float into a 32-bit integer.
    é    )r   r   r   r    r!   r"   )r   r   r   r   r   Úf32_as_int32K   s    r&   c             C   s$   |j t d¡kst‚|  |t ¡ ¡S )z0
    Bitcast a 32-bit integer into a float.
    r%   )r   r   r"   r    r!   r   )r   r   r   r   r   Úint32_as_f32R   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   r   Únegate_realY   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>e   s    z%call_fp_intrinsic.<locals>.<listcomp>)Úmoduler   ZFunctionZ	intrinsicÚcall)r   ÚnameÚargsÚmodZintrr   r   r   Úcall_fp_intrinsic`   s    r1   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   )r/   Úcastr   r   r   Úreturn_type)	Úcontextr   Úsigr/   r   Ú
input_typeZfpvalZ	inner_sigÚres)Úwrapped_implr   r   Úimplementern   s    
z2_unary_int_input_wrapper_impl.<locals>.implementerr   )r8   r9   r   )r8   r   Ú_unary_int_input_wrapper_impli   s    r:   c             C   s   t |ƒ}t| tjƒ|ƒ d S )N)r:   Úlowerr   ÚInteger)ÚfnÚ
float_implÚimplr   r   r   Úunary_math_int_implx   s    r@   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 )N)r1   r   r3   )r4   r   r5   r/   r7   )Úintrcoder   r   r>   €   s    z#unary_math_intr.<locals>.float_impl)r;   r   ÚFloatr@   )r=   rA   r>   r   )rA   r   Úunary_math_intr|   s    
rC   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,   r/   Úget_value_typer   r   r   r   Úfunctionr	   Úinsert_pure_functionr-   r2   r3   r   )r4   r   r5   r/   r   r0   r6   ÚltyÚ	func_nameÚfntyr=   r7   )Ú	f32externÚ	f64externr   r   r>   ’   s    

z%unary_math_extern.<locals>.float_impl)r   Úint64r;   rB   r@   )r=   rJ   rK   Zint_restypeZ	f_restyper>   r   )rJ   rK   r   Úunary_math_externˆ   s
    
rM   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Útanh)é   r   Zlog2fÚlog2ZceilfÚceilTZfloorfÚfloorZnumba_gammafZnumba_gammaZsqrtfÚsqrtZtruncfÚtruncZlgammafÚlgammac             C   s    |\}t ||ƒ}t| ||j|ƒS )N)r   r   r3   )r4   r   r5   r/   r   r7   r   r   r   Úisnan_float_implÖ   s    
rc   c             C   s   t j}t| ||j|ƒS )N)r	   Ú	false_bitr   r3   )r4   r   r5   r/   r7   r   r   r   Úisnan_int_implÜ   s    re   c             C   s    |\}t ||ƒ}t| ||j|ƒS )N)r   r   r3   )r4   r   r5   r/   r   r7   r   r   r   Úisinf_float_implâ   s    
rf   c             C   s   t j}t| ||j|ƒS )N)r	   rd   r   r3   )r4   r   r5   r/   r7   r   r   r   Úisinf_int_implè   s    rg   )r\   é   c             C   s    |\}t ||ƒ}t| ||j|ƒS )N)r   r   r3   )r4   r   r5   r/   r   r7   r   r   r   Úisfinite_float_implï   s    
ri   c             C   s   t j}t| ||j|ƒS )N)r	   Ztrue_bitr   r3   )r4   r   r5   r/   r7   r   r   r   Úisfinite_int_implõ   s    rj   c             C   sL   |d j }|j}| tj |||f¡d|j ¡}| ||¡}t| ||j	|ƒS )Nr   zllvm.copysign.%s)
r   r,   Úget_or_insert_functionr   r   rE   Zintrinsic_namer-   r   r3   )r4   r   r5   r/   rG   r0   r=   r7   r   r   r   Úcopysign_float_implû   s    
rl   c             C   s¤   |\}|   |jd ¡}|   |jd ¡}tj||dd}t ||t |¡f¡}dddœt|ƒ }	|j	j
||	d}
| |
||f¡}t ||| |¡f¡}t| ||j|ƒS )Nr   é   Úexp)r.   Znumba_frexpfZnumba_frexp)r   r   )Úget_data_typer/   r3   r	   Zalloca_oncer   rE   ZpointerÚstrr,   rk   r-   Zmake_anonymous_structÚloadr   )r4   r   r5   r/   r   ÚflttyÚinttyZexpptrrI   Úfnamer=   r7   r   r   r   Ú
frexp_impl  s    
ru   c             C   sn   |\}}t | j|jƒ\}}t |||f¡}dddœt|ƒ }	tj|j||	d}
| 	|
||f¡}t
| ||j|ƒS )NZnumba_ldexpfZnumba_ldexp)r   r   )r.   )Úmapro   r/   r   rE   rp   r	   rF   r,   r-   r   r3   )r4   r   r5   r/   r   rn   rr   rs   rI   rt   r=   r7   r   r   r   Ú
ldexp_impl  s    
rw   c             C   sL   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒS )N)Úsitofpr   r   r   r   r   Úatan2_float_impl)r4   r   r5   r/   Ú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 )N)Zuitofpr   r   r   r   r   ry   )r4   r   r5   r/   rz   r{   r|   r   r   r   Úatan2_u64_impl2  s
    r~   c             C   sŠ   t |ƒdkst‚|j}|jd }|  |¡}tjdtjtj	dkrBdndi| }t
 |||f¡}tj|j||d}	| |	|¡}
t| ||j|
ƒS )Nrh   r   Zatan2fÚwin32Zatan2_fixedÚatan2)r.   )Úlenr    r,   r/   rD   r   r   r   ÚsysÚplatformr   rE   r	   rF   r-   r   r3   )r4   r   r5   r/   r0   ZtyrG   rH   rI   r=   r7   r   r   r   ry   :  s    

ry   c             C   s\   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒ}t| ||j|ƒS )N)	rx   r   r   r   r   r   Úhypot_float_implr   r3   )r4   r   r5   r/   r{   rz   r|   r7   r   r   r   Úhypot_s64_implN  s    r…   c             C   s\   |\}}|  |t ¡ ¡}|  |t ¡ ¡}ttjtjtjƒ}t| ||||fƒ}t| ||j|ƒS )N)	rx   r   r   r   r   r   r„   r   r3   )r4   r   r5   r/   r{   rz   r|   r7   r   r   r   Úhypot_u64_implX  s    r†   c                sÆ   |j \}}||  kr |jks&n t‚|\}}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 )Nr   Z_hypotfZhypotfZ_hypotÚhypotr%   Úinfc                s"   t  | ¡st  |¡rˆ S ˆ| |ƒS )N)ÚmathÚisinf)r{   rz   )rˆ   Ú
plat_hypotr   r   Ú
hypot_impls  s    z$hypot_float_impl.<locals>.hypot_implc                s
   ˆ | |ƒS )Nr   )r{   rz   )r‹   r   r   rŒ   x  s    )r/   r3   r    r   r   r‚   rƒ   r   ZExternalFunctionr   ZMACHINE_BITSr   Zcompile_internalr   )r4   r   r5   r/   ZxtyZytyr{   rz   rt   rŒ   r7   r   )rˆ   r‹   r   r„   b  s    
r„   c             C   s6   |\}|   |jtjd ¡}| ||¡}t| ||j|ƒS )Né´   )Úget_constantr3   r‰   ÚpiÚfmulr   )r4   r   r5   r/   r{   Úcoefr7   r   r   r   Úradians_float_impl  s    r’   c             C   s6   |\}|   |jdtj ¡}| ||¡}t| ||j|ƒS )Nr   )rŽ   r3   r‰   r   r   r   )r4   r   r5   r/   r{   r‘   r7   r   r   r   Údegrees_float_implŒ  s    r“   c             C   s   |   tj|¡}|||ƒS )N)Zget_functionÚoperatorÚpow)r4   r   r5   r/   r?   r   r   r   Úpow_impl—  s    r–   )F)”Ú__doc__Z
__future__r   r   r   r‰   r”   r‚   ZnumpyZnpZllvmlite.llvmpy.coreZllvmpyZcorer   r   Znumba.targets.imputilsr   r   Znumbar   r	   r
   r   Znumba.typingr   Ú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(   r1   r:   r@   rC   rM   ZfabsZ	INTR_FABSrn   ZINTR_EXPZexp_implÚlogZINTR_LOGZlog_implZlog10Z
INTR_LOG10Z
log10_implZsinZINTR_SINZsin_implZcosZINTR_COSZcos_implrN   Z
log1p_implrO   Z
expm1_implrP   Zerf_implrQ   Z	erfc_implrR   Ztan_implrS   Z	asin_implrT   Z	acos_implrU   Z	atan_implrV   Z
asinh_implrW   Z
acosh_implrX   Z
atanh_implrY   Z	sinh_implrZ   Z	cosh_implr[   Z	tanh_implZ	PYVERSIONr]   Z	log2_implr^   Z	ceil_implr_   Z
floor_implZgammaZ
gamma_implr`   Z	sqrt_implra   Z
trunc_implrb   Zlgamma_implZisnanrB   rc   r<   re   rŠ   rf   rg   Zisfiniteri   rj   Zcopysignrl   Zfrexpru   ZldexpZintcrw   r€   rL   r}   Zuint64r~   ry   r‡   r…   r†   r„   Zradiansr’   Zdegreesr“   r•   r–   r   r   r   r   Ú<module>   s®   
	
$

