U
    -eb"                     @   sB  d dl 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mZ d dlmZ d dlmZ d dlmZmZmZ d dlmZmZ d dlZd dl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$dd Z%dd Z&d d! Z'd"d# Z(d$d% Z)d&d' Z*d(d) Z+d*d+ Z,G d,d- d-eZ-e.d.kr>e/  dS )/    N)compile_ptx)	f2i1i2i4i8u1u2u4u8)cuda)types)CUDATestCaseskip_on_cudasimskip_unless_cc_53)float16float32c                 C   s   t | S N)floatx r   e/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/numba/cuda/tests/cudapy/test_casting.pynative_cast   s    r   c                 C   s
   t | S r   )npint8r   r   r   r   to_int8   s    r   c                 C   s
   t | S r   )r   int16r   r   r   r   to_int16   s    r   c                 C   s
   t | S r   )r   int32r   r   r   r   to_int32   s    r    c                 C   s
   t | S r   )r   int64r   r   r   r   to_int64   s    r"   c                 C   s
   t | S r   )r   uint8r   r   r   r   to_uint8"   s    r$   c                 C   s
   t | S r   )r   uint16r   r   r   r   	to_uint16&   s    r&   c                 C   s
   t | S r   )r   uint32r   r   r   r   	to_uint32*   s    r(   c                 C   s
   t | S r   )r   uint64r   r   r   r   	to_uint64.   s    r*   c                 C   s   t | t d S )Ng      ?r   r   r   r   r   r   
to_float162   s    r,   c                 C   s   t | t d S N   )r   r   r   r   r   r   
to_float328   s    r/   c                 C   s   t | t d S r-   )r   float64r   r   r   r   
to_float64<   s    r1   c                 C   s
   t | S r   )r   	complex64r   r   r   r   to_complex64@   s    r3   c                 C   s
   t | S r   )r   
complex128r   r   r   r   to_complex128D   s    r5   c                 C   s   t jt| dS r-   r   Zfp16Zhmulr   r   r   r   r   r   cuda_int_literal_to_float16O   s    r7   c                 C   s   t | t d S r-   r+   r   r   r   r    reference_int_literal_to_float16U   s    r8   c                 C   s   t jt| dS Ng      @r6   r   r   r   r   cuda_float_literal_to_float16Y   s    r:   c                 C   s   t | t d S r9   r+   r   r   r   r   "reference_float_literal_to_float16_   s    r;   c                   @   s   e Zd Zdd Zedd Zeddd Zedd	 Zedd
d Z	edd Z
edd Zeddd Zeddd Zedd Zeddd Zedd Zeddd ZdS )TestCastingc                    s6   t jdd|t jfdd  fdd}|S )NTZdevicec                    s    | d |d< d S )Nr   r   )argres)wrapped_funcr   r   cuda_wrapper_fng   s    z4TestCasting._create_wrapped.<locals>.cuda_wrapper_fnc                    s:   t jdd}| |d< t jdd} d || |d S )N   )Zdtyper   )rB   rB   )r   Zzeros)r>   ZargarrayZresarray)rA   intypeouttyper   r   
wrapper_fnk   s
    z/TestCasting._create_wrapped.<locals>.wrapper_fn)r   Zjit)selfpyfuncrC   rD   rE   r   )rA   rC   rD   r@   r   _create_wrappedd   s
    zTestCasting._create_wrappedc                 C   s   t tttf}tjtjtjtjf}tj	tj
tjf}t||D ]\}}|D ]|}| j||dd | |||}| |d|d | |dtd | |d|d | |dtd W 5 Q R X qFq:d S )Nfromtytoty皙(@皙()r   r   r    r"   r   r   r   r   r!   r   r   r0   zipsubTestrH   assertEqualintrF   pyfuncstotysfromtysrG   rK   rJ   cfuncr   r   r   test_float_to_intt   s    zTestCasting.test_float_to_intz(Compilation unsupported in the simulatorc                 C   sP   t tttf}d}t||D ]0\}}t|tfdd\}}| d| d| qd S )N          @   Tr=   z	cvt.rni.s.f16)r   r   r    r"   rN   r   r   assertInrF   rS   sizesrG   sizeptx_r   r   r   test_float16_to_int_ptx   s
    z#TestCasting.test_float16_to_int_ptxc                 C   s   t tttf}tjtjtjtjf}tj	tj
tjf}t||D ]b\}}|D ]T}| j||d< | |||}| |d|d | |dtd W 5 Q R X qFq:d S )NrI   rL   )r   r   r    r"   r   r#   r%   r'   r)   r   r   r0   rN   rO   rH   rP   rQ   rR   r   r   r   test_float_to_uint   s    zTestCasting.test_float_to_uintc                 C   sP   t tttf}d}t||D ]0\}}t|tfdd\}}| d| d| qd S )NrX   Tr=   z	cvt.rni.ur]   )r$   r&   r(   r*   rN   r   r   r^   r_   r   r   r   test_float16_to_uint_ptx   s
    z$TestCasting.test_float16_to_uint_ptxc              
   C   sn   t ttf}tjtjtjf}t||D ]D\}}| j|d* | 	|tj
|}| |d|d W 5 Q R X q$d S )N)rK   A  )r,   r/   r1   r   r   r   r0   rN   rO   rH   r!   rP   )rF   rS   rT   rG   rK   rV   r   r   r   test_int_to_float   s    
zTestCasting.test_int_to_floatc              
   C   sf   t tf}ttf}t||D ]F\}}| j|d, | |tjtj}| 	|d|d W 5 Q R X qd S )N)funcrg   )
r7   r:   r8   r;   rN   rO   rH   r   r   rP   )rF   Z	cudafuncsZ	hostfuncsZcudafuncZhostfuncrV   r   r   r   test_literal_to_float16   s    z#TestCasting.test_literal_to_float16c                 C   sN   t tttf}d}t||D ].\}}tt|fdd\}}| d| | qd S )NrX   Tr=   zcvt.rn.f16.s)r   r   r   r   rN   r   r,   r^   rF   rU   r`   tyra   rb   rc   r   r   r   test_int_to_float16_ptx   s
    z#TestCasting.test_int_to_float16_ptxc                 C   sN   t tttf}d}t||D ].\}}tt|fdd\}}| d| | qd S )NrX   Tr=   zcvt.rn.f16.u)r   r	   r
   r   rN   r   r,   r^   rk   r   r   r   test_uint_to_float16_ptx   s
    z$TestCasting.test_uint_to_float16_ptxc              
   C   s   t ttf}tjtjtjf}tt	|||D ]x\\}}}| j
||dX | |||}tjj|d|d|d dd tjj|d|d|d dd W 5 Q R X q,d S )NrI   rL   r.   ga2U0*3?)ZrtolrM   )r,   r/   r1   r   r   r   r0   	itertoolsproductrN   rO   rH   testingassert_allclose)rF   rS   ZtysrG   rJ   rK   rV   r   r   r   test_float_to_float   s    
  zTestCasting.test_float_to_floatc                 C   sL   t tf}d}t||D ]0\}}t|tfdd\}}| d| d| qd S )N)Zf32Zf64Tr=   zcvt.r]   )r/   r1   rN   r   r   r^   )rF   rS   Z	postfixesrG   Zpostfixrb   rc   r   r   r   test_float16_to_float_ptx   s
    z%TestCasting.test_float16_to_float_ptxc                 C   s   t tf}tjtjf}tjtjtjf}t||D ]r\}}|D ]d}| j	||dL | 
|||}tj|d||d tj|d||dd  W 5 Q R X q:q.d S )NrI   gGz	@gGz	y                )r3   r5   r   r2   r4   r   r   r0   rN   rO   rH   rq   rr   rR   r   r   r   test_float_to_complex   s    
z!TestCasting.test_float_to_complexc                 C   sH   t jttfdd\}}| d| t jttfdd\}}| d| d S )NTr=   zst.f32zst.u16)r   r   r   r   r^   r   )rF   Zfloat32_ptxrc   Zfloat16_ptxr   r   r   test_native_cast   s    zTestCasting.test_native_castN)__name__
__module____qualname__rH   r   rW   r   rd   re   rf   rh   rj   rm   rn   rs   rt   ru   rv   r   r   r   r   r<   c   s2   




	





r<   __main__)0numpyr   Z
numba.cudar   Znumba.core.typesr   r   r   r   r   r   r	   r
   r   Znumbar   Z
numba.corer   Znumba.cuda.testingr   r   r   Znumba.typesr   r   ro   Zunittestr   r   r   r    r"   r$   r&   r(   r*   r,   r/   r1   r3   r5   r7   r8   r:   r;   r<   rw   mainr   r   r   r   <module>   s<   , 
