o
    ib"                     @   sD  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re/  dS 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   b/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/numba/cuda/tests/cudapy/test_casting.pynative_cast   s   r   c                 C   
   t | S r   )npint8r   r   r   r   to_int8      
r   c                 C   r   r   )r   int16r   r   r   r   to_int16   r   r    c                 C   r   r   )r   int32r   r   r   r   to_int32   r   r"   c                 C   r   r   )r   int64r   r   r   r   to_int64   r   r$   c                 C   r   r   )r   uint8r   r   r   r   to_uint8"   r   r&   c                 C   r   r   )r   uint16r   r   r   r   	to_uint16&   r   r(   c                 C   r   r   )r   uint32r   r   r   r   	to_uint32*   r   r*   c                 C   r   r   )r   uint64r   r   r   r   	to_uint64.   r   r,   c                 C      t | t d S )Ng      ?r   r   r   r   r   r   
to_float162      r/   c                 C      t | t d S N   )r   r   r   r   r   r   
to_float328      r4   c                 C   r1   r2   )r   float64r   r   r   r   
to_float64<   r5   r7   c                 C   r   r   )r   	complex64r   r   r   r   to_complex64@   r   r9   c                 C   r   r   )r   
complex128r   r   r   r   to_complex128D   r   r;   c                 C      t jt| dS r2   r   fp16hmulr   r   r   r   r   r   cuda_int_literal_to_float16O   r0   r@   c                 C   r-   r2   r.   r   r   r   r    reference_int_literal_to_float16U   r5   rA   c                 C   r<   Ng      @r=   r   r   r   r   cuda_float_literal_to_float16Y   r0   rC   c                 C   r-   rB   r.   r   r   r   r   "reference_float_literal_to_float16_   r5   rD   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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   )dtyper   )rL   rL   )r   zeros)rH   argarrayresarray)rK   intypeouttyper   r   
wrapper_fnk   s
   z/TestCasting._create_wrapped.<locals>.wrapper_fn)r   jit)selfpyfuncrQ   rR   rS   r   )rK   rQ   rR   rJ   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 ]O\}}|D ]H}| j||d7 | |||}| |d|d | |dtd | |d|d | |dtd W d    n1 sfw   Y  q#qd S )Nfromtytoty皙(@皙()r   r    r"   r$   r   r   r   r!   r#   r   r   r6   zipsubTestrW   assertEqualintrU   pyfuncstotysfromtysrV   rZ   rY   cfuncr   r   r   test_float_to_intt   s   zTestCasting.test_float_to_intz(Compilation unsupported in the simulatorc                 C   P   t tttf}d}t||D ]\}}t|tfdd\}}| d| d| qd S )N          @   TrF   z	cvt.rni.s.f16)r   r    r"   r$   r]   r   r   assertInrU   rb   sizesrV   sizeptx_r   r   r   test_float16_to_int_ptx      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 ];\}}|D ]4}| j||d# | |||}| |d|d | |dtd W d    n1 sRw   Y  q#qd S )NrX   r[   )r   r    r"   r$   r   r%   r'   r)   r+   r   r   r6   r]   r^   rW   r_   r`   ra   r   r   r   test_float_to_uint   s   zTestCasting.test_float_to_uintc                 C   rg   )Nrh   TrF   z	cvt.rni.urm   )r&   r(   r*   r,   r]   r   r   rn   ro   r   r   r   test_float16_to_uint_ptx   ru   z$TestCasting.test_float16_to_uint_ptxc              	   C   s   t ttf}tjtjtjf}t||D ],\}}| j|d | 	|tj
|}| |d|d W d    n1 s9w   Y  qd S )N)rZ   A  )r/   r4   r7   r   r   r   r6   r]   r^   rW   r#   r_   )rU   rb   rc   rV   rZ   re   r   r   r   test_int_to_float   s   
zTestCasting.test_int_to_floatc              	   C   sz   t tf}ttf}t||D ]-\}}| j|d | |tjtj}| 	|d|d W d    n1 s5w   Y  qd S )N)funcrx   )
r@   rC   rA   rD   r]   r^   rW   r   r   r_   )rU   	cudafuncs	hostfuncscudafunchostfuncre   r   r   r   test_literal_to_float16   s   z#TestCasting.test_literal_to_float16c                 C   N   t tttf}d}t||D ]\}}tt|fdd\}}| d| | qd S )Nrh   TrF   zcvt.rn.f16.s)r   r   r   r   r]   r   r/   rn   rU   rd   rp   tyrq   rr   rs   r   r   r   test_int_to_float16_ptx      z#TestCasting.test_int_to_float16_ptxc                 C   r   )Nrh   TrF   zcvt.rn.f16.u)r   r	   r
   r   r]   r   r/   rn   r   r   r   r   test_uint_to_float16_ptx   r   z$TestCasting.test_uint_to_float16_ptxc              	   C   s   t ttf}tjtjtjf}tt	|||D ]F\\}}}| j
||d1 | |||}tjj|d|d|d dd tjj|d|d|d dd W d    n1 sWw   Y  qd S )NrX   r[   r3   ga2U0*3?)rtolr\   )r/   r4   r7   r   r   r   r6   	itertoolsproductr]   r^   rW   testingassert_allclose)rU   rb   tysrV   rY   rZ   re   r   r   r   test_float_to_float   s   
zTestCasting.test_float_to_floatc                 C   sL   t tf}d}t||D ]\}}t|tfdd\}}| d| d| qd S )N)f32f64TrF   zcvt.rm   )r4   r7   r]   r   r   rn   )rU   rb   	postfixesrV   postfixrr   rs   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 ]C\}}|D ]<}| j	||d+ | 
|||}tj|d||d tj|d||dd  W d    n1 sTw   Y  qqd S )NrX   gGz	@gGz	y                )r9   r;   r   r8   r:   r   r   r6   r]   r^   rW   r   r   ra   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 )NTrF   zst.f32zst.u16)r   r   r   r   rn   r   )rU   float32_ptxrs   float16_ptxr   r   r   test_native_cast   s   zTestCasting.test_native_castN)__name__
__module____qualname__rW   r   rf   r   rt   rv   rw   ry   r   r   r   r   r   r   r   r   r   r   r   rE   c   s4    




	





rE   __main__)0numpyr   
numba.cudar   numba.core.typesr   r   r   r   r   r   r	   r
   r   numbar   
numba.corer   numba.cuda.testingr   r   r   numba.typesr   r   r   unittestr   r   r    r"   r$   r&   r(   r*   r,   r/   r4   r7   r9   r;   r@   rA   rC   rD   rE   r   mainr   r   r   r   <module>   s@    , 