a
    
d                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dl	m
Z
mZmZmZ d dlmZ d dlmZmZ ee
jjZdd ZG dd deZG d	d
 d
eZedkre  dS )    N)typestypingcgutilscpu)global_compiler_lock)TestCaserun_in_subprocessc                 C   s   t t| S N)irConstantmachine_int)n r   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/numba/tests/test_cgutils.pymachine_const   s    r   c                   @   s`   e Zd Zdd Zejdd Zdd Zdd Zejdd
dZ	ejdd Z
dd Zdd ZdS )StructureTestCasec                 C   s   t  }t|| _d S r	   )r   Contextr   Z
CPUContextcontext)selfZtyping_contextr   r   r   setUp   s    zStructureTestCase.setUpc                 #   s   t ttg| }tjtjgtjf| R   jdt	|
 jsVJ d}t |}dg}t fdd}j|j|fV  d S )N entryTc                     sB   j  }|d  }| |j} |}||  S )Nztest_module.%s)r   codegenZcreate_libraryidZadd_ir_moduleZget_pointer_to_functionname)argsr   ZlibraryZcptrZcfuncZctypes_fntyfunctionmoduler   r   r   	call_func)   s    

z5StructureTestCase.compile_function.<locals>.call_func)r
   FunctionTyper   ctypes	CFUNCTYPEc_size_tr   create_moduler   Zget_or_insert_functionr   Zis_declarationZappend_basic_blockZ	IRBuilderr   r   )r   nargsZ	llvm_fntyZentry_blockbuilderfirstr   r   r   r   compile_function   s    




z"StructureTestCase.compile_functionc                 C   s0   t |tsJ tjj}tjg|_tj|_||S r	   )	
isinstance	bytearrayr!   	pythonapiZPyByteArray_AsString	py_objectargtypesc_void_prestype)r   baZba_as_stringr   r   r   get_bytearray_addr5   s
    
z$StructureTestCase.get_bytearray_addrc                 C   sx   |  d6\}}}}||d |d }|| W d    n1 sF0    Y  | |ddd | |ddd d S )	N   r                  )r(   addretassertEqual)r   r   r&   r   callresr   r   r   test_compile_function<   s
    (z'StructureTestCase.test_compile_functionr   c           
      c   s   |  dx\}}}}|||}||d t|}	||	t|j}	||||	d}||||fV  |tt	d W d    n1 s0    Y  || 
| d S )Nr3   r   )ref)r(   r9   r   Zinttoptrr
   ZPointerType_typer:   r   r   r1   )
r   struct_classbufoffsetr   r&   r   r<   instZsptrr   r   r   run_struct_accessD   s    
0z#StructureTestCase.run_struct_accessc                 c   s   t dd }|d d  }d}| |||$\}}}	}
|||
fV  W d    n1 sT0    Y  | || tj|||g|R   | || d S )N   !(      )r*   rE   ZassertNotEqualstruct	pack_intor;   )r   rA   Z
struct_fmtZstruct_argsrB   expectedrC   r   r&   r   rD   r   r   r   run_simple_struct_testQ   s    
*z(StructureTestCase.run_simple_struct_testc                 C   sv   G dd dt j}d}| ||d>\}}}ttdd|_ttdd|_W d    n1 sh0    Y  d S )	Nc                   @   s    e Zd ZdejfdejfgZdS )z,StructureTestCase.test_int_fields.<locals>.SabN)__name__
__module____qualname__r   int32Zuint16_fieldsr   r   r   r   Sb   s   rT   z=iH)xV4ͫ      rU      rV   )r   	StructurerL   r
   r   IntTyperM   rN   r   rT   fmtr   r&   rD   r   r   r   test_int_fieldsa   s    z!StructureTestCase.test_int_fieldsc                 C   sr   G dd dt j}d}| ||d:\}}}tt d|_tt d|_W d    n1 sd0    Y  d S )Nc                   @   s    e Zd ZdejfdejfgZdS )z.StructureTestCase.test_float_fields.<locals>.SrM   rN   N)rO   rP   rQ   r   float64float32rS   r   r   r   r   rT   m   s   rT   z=df)Gz?=
ףp=@r`   ra   )	r   rY   rL   r
   r   Z
DoubleTyperM   Z	FloatTyperN   r[   r   r   r   test_float_fieldsl   s    z#StructureTestCase.test_float_fieldsN)r   )rO   rP   rQ   r   
contextlibcontextmanagerr(   r1   r>   rE   rL   r]   rb   r   r   r   r   r      s   

r   c                   @   s   e Zd ZdZdd ZdS )TestCGContextz/Tests for code generation context functionalityc                 C   s4   d}d| d}t |\}}| t||  d S )Ni@ az  if 1:
        from numba import njit, types
        from numba.extending import intrinsic

        @intrinsic
        def printf(tyctx, int_arg):
            sig = types.void(int_arg)
            def codegen(cgctx, builder, sig, llargs):
                cgctx.printf(builder, "%d\n", *llargs)
            return sig, codegen

        @njit
        def foo():
            printf(z)

        foo()
        )r   ZassertInstrdecode)r   valuecodeout_r   r   r   test_printf{   s    zTestCGContext.test_printfN)rO   rP   rQ   __doc__rl   r   r   r   r   re   x   s   re   __main__)rc   r!   rI   sysZllvmlite.irr
   numpynpZunittestZ
numba.corer   r   r   r   Znumba.core.compiler_lockr   Znumba.tests.supportr   r   rZ   ZintpZbitwidthr   r   r   re   rO   mainr   r   r   r   <module>   s   c