U
    b»{j   ã                   @   sÖ   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlmZmZmZ d dlZeG d	d
„ d
ƒƒZedƒG dd„ deƒƒZe dkrÒe !¡  dS )é    )ÚList)Ú	dataclassÚfield)ÚcudaÚfloat32)Úcompile_ptx_for_current_deviceÚcompile_ptx)	ÚcosÚsinÚtanÚexpÚlogÚlog10Úlog2ÚpowÚtanh)ÚtruedivN)ÚCUDATestCaseÚskip_on_cudasimÚskip_unless_cc_75c                   @   sx   e Zd ZU eed�Zee ed< eed�Z	ee ed< eed�Z
ee ed< eed�Zee ed< eeedœdd„Zd	S )
ÚFastMathCriterion)Údefault_factoryÚfast_expectedÚfast_unexpectedÚprec_expectedÚprec_unexpected)ÚtestÚfastÚprecc                    s|   |  t‡ fdd„| jD ƒƒ¡ |  t‡ fdd„| jD ƒƒ¡ |  t‡fdd„| jD ƒƒ¡ |  t‡fdd„| jD ƒƒ¡ d S )Nc                 3   s   | ]}|ˆ kV  qd S ©N© ©Ú.0Úi©r   r    úd/var/www/html/bwave/python/venv/lib/python3.8/site-packages/numba/cuda/tests/cudapy/test_fastmath.pyÚ	<genexpr>   s     z*FastMathCriterion.check.<locals>.<genexpr>c                 3   s   | ]}|ˆ kV  qd S r   r    r!   r$   r    r%   r&      s     c                 3   s   | ]}|ˆ kV  qd S r   r    r!   ©r   r    r%   r&      s     c                 3   s   | ]}|ˆ kV  qd S r   r    r!   r'   r    r%   r&      s     )Z
assertTrueÚallr   r   r   r   )Úselfr   r   r   r    )r   r   r%   Úcheck   s    zFastMathCriterion.checkN)Ú__name__Ú
__module__Ú__qualname__r   Úlistr   r   ÚstrÚ__annotations__r   r   r   r   r*   r    r    r    r%   r      s
   
r   z4Fastmath and PTX inspection not available on cudasimc                   @   s¢   e Zd Zdd„ Zedœdd„Zedœdd„Zdd	„ Zd
d„ Zdd„ Z	e
d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„ Zejd d!„ ƒZd"S )#ÚTestFastMathOptionc           
      C   sx   t j||dd�|ƒ}t j||d�|ƒ}| | | |¡| |¡¡ t|||dd�\}}t|||d�\}	}| | ||	¡ d S )NT)ÚdeviceÚfastmath©r2   )r   Újitr*   Úinspect_asmr   )
r)   ZpyfuncÚsigr2   Ú	criterionÚfastverÚprecverÚfastptxÚ_Úprecptxr    r    r%   Ú_test_fast_math_common   s&      ÿ   ÿ
  ÿ
z)TestFastMathOption._test_fast_math_common)r8   c                    sP   ‡ fdd„}‡ fdd„}| j |td d d… tfd|d� | j |tfd|d� d S )	Nc                    s   ˆ |ƒ| d< d S ©Nr   r    ©ÚrÚx©Úopr    r%   Úkernel2   s    z8TestFastMathOption._test_fast_math_unary.<locals>.kernelc                    s   ˆ | ƒS r   r    )rB   rC   r    r%   Údevice_function5   s    zATestFastMathOption._test_fast_math_unary.<locals>.device_functioné   F©r2   r8   T©r>   r   )r)   rD   r8   rE   rF   r    rC   r%   Ú_test_fast_math_unary1   s       ÿ   ÿz(TestFastMathOption._test_fast_math_unaryc                    sT   ‡ fdd„}‡ fdd„}| j |td d d… ttfd|d� | j |ttfd|d� d S )	Nc                    s   ˆ ||ƒ| d< d S r?   r    ©rA   rB   ÚyrC   r    r%   rE   @   s    z9TestFastMathOption._test_fast_math_binary.<locals>.kernelc                    s
   ˆ | |ƒS r   r    )rB   rL   rC   r    r%   r2   C   s    z9TestFastMathOption._test_fast_math_binary.<locals>.devicerG   FrH   TrI   )r)   rD   r8   rE   r2   r    rC   r%   Ú_test_fast_math_binary?   s      þ   ÿz)TestFastMathOption._test_fast_math_binaryc                 C   s   |   ttdgdgd�¡ d S )Núcos.approx.ftz.f32 ©r   r   )rJ   r	   r   ©r)   r    r    r%   Ú	test_cosfN   s    þþzTestFastMathOption.test_cosfc                 C   s   |   ttdgdgd�¡ d S )Núsin.approx.ftz.f32 rO   )rJ   r
   r   rP   r    r    r%   Ú	test_sinfW   s    þþzTestFastMathOption.test_sinfc                 C   s    |   ttdddgdgd�¡ d S )NrR   rN   údiv.approx.ftz.f32 rO   )rJ   r   r   rP   r    r    r%   Ú	test_tanf`   s    ýüþzTestFastMathOption.test_tanfc                 C   s   |   ttdgdgd�¡ d S )Nútanh.approx.f32 rO   )rJ   r   r   rP   r    r    r%   Ú
test_tanhfj   s    þþzTestFastMathOption.test_tanhfc                    sL   dd„ ‰‡ ‡fdd„}|dt dgdgd�d� |d	t d
dgdgd�d� d S )Nc                 S   s   t |ƒ| d< d S r?   )r   r@   r    r    r%   Útanh_kernelv   s    z>TestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_kernelc                    sT   t ˆtd d d… tfd| d�\}}t ˆtd d d… tf| d�\}}| ˆ ||¡ d S )NrG   T)r3   Úcc)rY   )r   r   r*   )rY   r8   r;   r<   r=   ©r)   rX   r    r%   Útanh_common_testy   s     ÿ
ÿ
zCTestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_common_test)é   é   rV   rO   )rY   r8   )r\   r   zex2.approx.ftz.f32 zrcp.approx.ftz.f32 )r   )r)   r[   r    rZ   r%   Útest_tanhf_compile_ptxu   s    þ
ÿýÿz)TestFastMathOption.test_tanhf_compile_ptxc                 C   s   |   ttdgdgd�¡ d S )Nzfma.rn.f32 )r   r   )rJ   r   r   rP   r    r    r%   Ú	test_expf‹   s    þþzTestFastMathOption.test_expfc                 C   s   |   ttddgdgd�¡ d S )Núlg2.approx.ftz.f32 Z
0f3F317218rO   )rJ   r   r   rP   r    r    r%   Ú	test_logf”   s     þÿzTestFastMathOption.test_logfc                 C   s   |   ttddgdgd�¡ d S )Nr`   Z
0f3E9A209BrO   )rJ   r   r   rP   r    r    r%   Útest_log10f�   s     þÿzTestFastMathOption.test_log10fc                 C   s   |   ttdgdgd�¡ d S ©Nr`   rO   )rJ   r   r   rP   r    r    r%   Ú
test_log2f¦   s     þÿzTestFastMathOption.test_log2fc                 C   s   |   ttdgdgd�¡ d S rc   )rM   r   r   rP   r    r    r%   Ú	test_powf®   s     þÿzTestFastMathOption.test_powfc              	   C   s$   |   ttdgdgdgdgd�¡ d S )NrT   ú
div.rn.f32)r   r   r   r   )rM   r   r   rP   r    r    r%   Ú	test_divf¶   s     üÿzTestFastMathOption.test_divfc              	   C   s¼   dd„ }t d d d… t t f}tj|ddd�|ƒ}tj|dd�|ƒ}d}tj|tj d�}|  t¡� |d|f |d	d
ƒ W 5 Q R X z|d|f |d	d
ƒ W n tk
r¶   |  d¡ Y nX d S )Nc                 S   s   || | d< d S r?   r    rK   r    r    r%   Úf10Á   s    z3TestFastMathOption.test_divf_exception.<locals>.f10rG   T)r3   Údebug)ri   é
   )Zdtypeg      $@g        z5Divide in fastmath should not throw ZeroDivisionError)r   r   r5   ÚnpÚemptyZassertRaisesÚZeroDivisionErrorZfail)r)   rh   r7   r9   r:   ZnelemZaryr    r    r%   Útest_divf_exceptionÀ   s    z&TestFastMathOption.test_divf_exceptionc                    s    t jddd�dd„ ƒ‰ ‡ fdd„}td d d… tf}t j|dd	�|ƒ}t  |¡|ƒ}|  d
| |¡¡ |  d| |¡¡ |  d
| |¡¡ |  d| |¡¡ d S )Nzfloat32(float32, float32)Tr4   c                 S   s   | | S r   r    )ÚaÚbr    r    r%   ÚfooÖ   s    z@TestFastMathOption.test_device_fastmath_propagation.<locals>.fooc                    s&   t  d¡}|| jk r"ˆ ||ƒ| |< d S )NrG   )r   ÚgridÚsize)ZarrÚvalr#   ©rq   r    r%   ÚbarÚ   s    

z@TestFastMathOption.test_device_fastmath_propagation.<locals>.barrG   )r3   zdiv.approx.f32rf   zdiv.full.f32)r   r5   r   ZassertInr6   ZassertNotIn)r)   rv   r7   r9   r:   r    ru   r%   Ú test_device_fastmath_propagationÑ   s    
z3TestFastMathOption.test_device_fastmath_propagationN)r+   r,   r-   r>   r   rJ   rM   rQ   rS   rU   r   rW   r^   r_   ra   rb   rd   re   rg   rn   ÚunittestZexpectedFailurerw   r    r    r    r%   r1      s$   		


			
r1   Ú__main__)"Útypingr   Údataclassesr   r   Znumbar   r   Znumba.cuda.compilerr   r   Úmathr	   r
   r   r   r   r   r   r   r   Úoperatorr   Únumpyrk   Znumba.cuda.testingr   r   r   rx   r   r1   r+   Úmainr    r    r    r%   Ú<module>   s   , T