o
    8ήcP                     @   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	 d dl
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 d dlZeG d	d
 d
ZedG dd deZedkree  dS dS )    )List)	dataclassfield)cudafloat32)compile_ptx_for_current_device)cossintanexploglog10log2pow)truedivN)CUDATestCaseskip_on_cudasimc                   @   s|   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< dededef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       | ]}| v V  qd S N .0ir   r   L/tmp/pip-target-vg8gfxp4/lib/python/numba/cuda/tests/cudapy/test_fastmath.py	<genexpr>       z*FastMathCriterion.check.<locals>.<genexpr>c                 3       | ]}| vV  qd S r   r   r   r"   r   r#   r$      r%   c                 3   r   r   r   r   r   r   r#   r$      r%   c                 3   r&   r   r   r   r'   r   r#   r$      r%   )
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defddZdef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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r3   )r   jitr+   inspect_asmr   )
r*   pyfuncsigr3   	criterionfastverprecverfastptx_precptxr   r   r#   _test_fast_math_common   s   

z)TestFastMathOption._test_fast_math_commonr:   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#   kernel/      z8TestFastMathOption._test_fast_math_unary.<locals>.kernelc                    s    | S r   r   )rC   rD   r   r#   device_function2   s   zATestFastMathOption._test_fast_math_unary.<locals>.device_function   Fr3   r:   Tr@   r   )r*   rE   r:   rF   rH   r   rD   r#   _test_fast_math_unary.   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 rA   r   rB   rC   yrD   r   r#   rF   =   s   z9TestFastMathOption._test_fast_math_binary.<locals>.kernelc                    s
    | |S r   r   )rC   rN   rD   r   r#   r3   @   s   
z9TestFastMathOption._test_fast_math_binary.<locals>.devicerI   FrJ   TrK   )r*   rE   r:   rF   r3   r   rD   r#   _test_fast_math_binary<   s   
z)TestFastMathOption._test_fast_math_binaryc                 C      |  ttdgdgd d S )Ncos.approx.ftz.f32 r   r   )rL   r   r   r*   r   r   r#   	test_cosfK      zTestFastMathOption.test_cosfc                 C   rP   )Nsin.approx.ftz.f32 rR   )rL   r	   r   rS   r   r   r#   	test_sinfT   rU   zTestFastMathOption.test_sinfc                 C   s   |  ttg ddgd d S )N)rV   rQ   div.approx.ftz.f32 rV   rR   )rL   r
   r   rS   r   r   r#   	test_tanf]   s   zTestFastMathOption.test_tanfc                 C   rP   )Nzfma.rn.f32 )r   r   )rL   r   r   rS   r   r   r#   	test_expfg   rU   zTestFastMathOption.test_expfc                 C      |  ttddgdgd d S )Nlg2.approx.ftz.f32 
0f3F317218rR   )rL   r   r   rS   r   r   r#   	test_logfp      zTestFastMathOption.test_logfc                 C   r[   )Nr\   
0f3E9A209BrR   )rL   r   r   rS   r   r   r#   test_log10fy   r_   zTestFastMathOption.test_log10fc                 C   rP   Nr\   rR   )rL   r   r   rS   r   r   r#   
test_log2f      zTestFastMathOption.test_log2fc                 C   rP   rb   )rO   r   r   rS   r   r   r#   	test_powf   rd   zTestFastMathOption.test_powfc              	   C   s$   |  ttdgdgdgdgd d S )NrX   
div.rn.f32)r   r   r   r   )rO   r   r   rS   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 d    n1 sEw   Y  z|d|f |d	d
 W d S  tyf   | d Y d S w )Nc                 S   s   || | d< d S rA   r   rM   r   r   r#   f10   rG   z3TestFastMathOption.test_divf_exception.<locals>.f10rI   T)r4   debug)ri   
   )dtypeg      $@g        z5Divide in fastmath should not throw ZeroDivisionError)r   r   r6   npemptyassertRaisesZeroDivisionErrorfail)r*   rh   r9   r;   r<   nelem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)Tr5   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 d S )NrI   )r   gridsize)arrvalr!   rv   r   r#   bar   s   

z@TestFastMathOption.test_device_fastmath_propagation.<locals>.barrI   )r4   zdiv.approx.f32rf   zdiv.full.f32)r   r6   r   assertInr7   assertNotIn)r*   r|   r9   r;   r<   r   r{   r#    test_device_fastmath_propagation   s   
z3TestFastMathOption.test_device_fastmath_propagationN)r,   r-   r.   r@   r   rL   rO   rT   rW   rY   rZ   r^   ra   rc   re   rg   rs   unittestexpectedFailurer   r   r   r   r#   r2      s     		
			
r2   __main__)typingr   dataclassesr   r   numbar   r   numba.cuda.compilerr   mathr   r	   r
   r   r   r   r   r   operatorr   numpyrl   numba.cuda.testingr   r   r   r   r2   r,   mainr   r   r   r#   <module>   s"    ( 1