o
    iCj                     @   s&  d dl mZ d dlZd dlmZmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZmZ edg 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ZG dd deZG dd deZdd Zdd  ZG d!d" d"eZG d#d$ d$eZ G d%d& d&eZ!G d'd( d(e!Z"dS ))    )
namedtupleN)Constant	IRBuilder)ir)typescgutils)global_compiler_lock)make_library_cache	NullCache_wrapper_infolibraryenvnamec              	   C   s  |  }|j |||	j|	j|\}}|j|jddC\}}| || W d    n1 s-w   Y  | |
 }|j ||
| |
| W d    n1 sPw   Y  W d    n1 s_w   Y  t	||D ]\}}|
||||j| qi|
||||j| |jS NTZlikely)	call_convcall_functionreturn_typeargsif_elseis_ok
gil_ensureraise_errorgil_releasezipstoreaddloadstepcode)r   r   contextfuncbuilderarraysoutoffsetsstore_offset	signaturepyapir   elemsstatusretvalZif_okZif_errorgiloffary r0   f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/np/ufunc/wrappers.py_build_ufunc_loop_body   s,   

	r2   c                 C   s   |  }t jgt|	j }|jdd |j||t j||\}}|D ]}|| q"W d    n1 s4w   Y  || t||D ]\}}|	|
|||j| qB|	|
|||j| |jS )NT)Zkeep_new)r   pyobjectlenr   Zerr_pushr   r   decrefr   r   r   r   r   r    )r   r   r!   r"   r#   r$   r%   r&   r'   r(   r   r)   r*   Z_objargsr+   r,   elemr.   r/   r0   r0   r1   _build_ufunc_loop_body_objmode,   s$   

r7   c
                    s@    fdd}
fdd}t |
|| | |||	dS )Nc                     s   fddt  D } | S )Nc                        g | ]\}}|  |qS r0   load_directr   .0r.   r/   r#   r0   r1   
<listcomp>O       z6build_slow_loop_body.<locals>.load.<locals>.<listcomp>)r   r*   )r$   r#   r&   r0   r1   r   N   s   
z"build_slow_loop_body.<locals>.loadc                    s    |   d S N)store_directr   r,   )r#   r%   r'   r0   r1   r   S   s   z#build_slow_loop_body.<locals>.storer   r2   )r!   r"   r#   r$   r%   r&   r'   r(   r)   r   r   r   r0   )r$   r#   r&   r%   r'   r1   build_slow_loop_bodyL   s   
rF   c                    sb   |  |	}|
||	 fdd}fdd}t||| | |	S )Nc                     s8   fddt  D } fddt | jD } | S )Nc                    r8   r0   r9   r;   r=   r0   r1   r>   b   r?   z5build_obj_loop_body.<locals>.load.<locals>.<listcomp>c                    s   g | ]\}} || qS r0   )from_native_value)r<   vt)env_managerr)   r0   r1   r>   e   s    )r   r   r@   )r$   r#   rJ   r&   r)   r(   r0   r1   r   `   s   

z!build_obj_loop_body.<locals>.loadc                    sz   t  | } j|dd& j| }|jd u sJ |j  	|  W d    d S 1 s6w   Y  d S r   )
r   Zis_not_nullif_thenZto_native_valuer   cleanuprB   valuer   r5   )r,   r   native)r#   r%   r)   r(   r'   r0   r1   r   i   s   "z"build_obj_loop_body.<locals>.store)Zget_env_bodyZget_env_managerr7   )r!   r"   r#   r$   r%   r&   r'   r(   r)   envptrr   Zenv_bodyr   r   r0   )r$   r#   rJ   r&   r%   r)   r(   r'   r1   build_obj_loop_body[   s   	rP   c                    s<    fdd}fdd}t ||| || ||||	|
dS )Nc                     s   fdd D } | S )Nc                    s   g | ]}|  qS r0   )load_aligned)r<   r/   )indr0   r1   r>   ~   s    z6build_fast_loop_body.<locals>.load.<locals>.<listcomp>r0   r@   )r$   rR   r0   r1   r   }   s   
z"build_fast_loop_body.<locals>.loadc                    s    |   d S rA   )store_alignedrC   )rR   r%   r0   r1   r      s   z#build_fast_loop_body.<locals>.storerD   rE   )r!   r"   r#   r$   r%   r&   r'   r(   rR   r)   r   r   r   r0   )r$   rR   r%   r1   build_fast_loop_body{   s   
rT   c           *      C   s  t |tsJ td}t|}t|}|tj}	t|	}
tt	 ||
|
|g}|
 d}|d}|rK|jtjtjgt|j }n	|j|j|j}tj|||d}|jd t||d|j }|j\}}}}d|_d|_d	|_d
|_t|d}||j}|j}|||j|}|j|dd}g }t|jD ]\}}| t!|||||| qt!||||t||j}g }|"tjd}|D ]} t#$||	}!| |! |%||! qt#$||	}"|%||" t#j&}#|D ]	}$|'|#|$j(}#q|)|}%|r;|%* }&t#j+|||	d t,|||||||"||%|| W d   n	1 s,w   Y  |%-|& |.  n|/|#\}'}(|'1 t#j+|||	d})t0|||||||"||)j1|%|d W d   n	1 sjw   Y  W d   n	1 szw   Y  |(/ t#j+|||	d t2|||||||"||%|d
 W d   n	1 sw   Y  W d   n	1 sw   Y  W d   n	1 sw   Y  |.  ~|3| |4|  t5|||jdS )z
    Wrap the scalar function with a loop that iterates over the arguments

    Returns
    -------
    (library, env, name)
       Zufunc_wrapper r   alwaysinlinez
__ufunc__.r   dimsstepsdataentry	loopcountr   intpNrD   r   )6
isinstancestrr   IntTypePointerTypeget_value_typer   r_   FunctionTypeVoidTypecodegencreate_librarycreate_ir_moduler   get_function_typer3   r4   r   r   Function
attributesr   r   r   append_basic_blockZget_env_namefndescenvironmentr   Zdeclare_env_globalmodule	enumerateappend	UArrayArgget_constantr   alloca_oncer   true_bitand_is_unit_stridedget_python_apir   	for_rangerP   r   ret_voidr   rT   indexrF   add_ir_moduleadd_linking_libraryr   )*r   r!   fnamer(   Zobjmodecresbyte_t
byte_ptr_tbyte_ptr_ptr_tintp_t
intp_ptr_tfnty
wrapperlibwrapper_module	func_typer"   wrapperarg_argsarg_dims	arg_stepsarg_datar#   envnamer   rO   r]   r$   itypr%   r&   zero_pr'   Zunit_stridedr/   r)   r-   rx   Z
is_stridedloopr0   r0   r1   build_ufunc_wrapper   s   












r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )rs   c                 C   s   || _ || _|| _| j tj|}| j|||g}||}	| j	||	
 | _| j |	}
| j tj|
| _| j||g}| j|| _|d| j| j| _|| _d S )Nz==)r!   r#   fe_typert   r   r_   r   gepget_data_typebitcast
as_pointerdataptrget_abi_sizeofZabisizer   Zicmp_unsignedrx   )selfr!   r#   r   rZ   r   r   offsetZoffseted_argsZ	data_typeZsizeofZoffseted_stepr0   r0   r1   __init__   s"   

zUArrayArg.__init__c                 C   s&   t | j| j|}| j| j| j|S )zm
        Generic load from the given *byteoffset*.  load_aligned() is
        preferred if possible.
        )r   pointer_addr#   r   r!   unpack_valuer   )r   
byteoffsetptrr0   r0   r1   r:     s   zUArrayArg.load_directc                 C   s&   | j | j|g}| j| j | j|S rA   )r#   r   r   r!   r   r   )r   rR   r   r0   r0   r1   rQ     s   zUArrayArg.load_alignedc                 C   s,   t | j| j|}| j| j| j|| d S rA   )r   r   r#   r   r!   
pack_valuer   )r   rM   r   r   r0   r0   r1   rB        zUArrayArg.store_directc                 C   s,   | j | j|g}| j| j | j|| d S rA   )r#   r   r   r!   r   r   )r   rM   rR   r   r0   r0   r1   rS     r   zUArrayArg.store_alignedN)__name__
__module____qualname__r   r:   rQ   rB   rS   r0   r0   r0   r1   rs      s    rs   Zgufc                   @   s   e Zd Zdd Zedd Zedd Zedd Zed	d
 Zedd Z	e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S )_GufuncWrapperc                 C   sN   || _ || _|| _|| _| jjtjk| _|rt	| j dnt
 | _t|| _dS )a,  
        The *is_parfors* argument is a boolean that indicates if the GUfunc
        being built is to be used as a ParFors kernel. If True, it disables
        the caching on the wrapper as a separate unit because it will be linked
        into the caller function and cached along with it.
        )py_funcN)r   r   sinsoutr(   r   r   r3   Zis_objectmodeGufWrapperCacher
   cachebool
is_parfors)r   r   r   r   r   r   r   r0   r0   r1   r   &  s   z_GufuncWrapper.__init__c                 C      | j jS rA   )r   r   r   r0   r0   r1   r   6     z_GufuncWrapper.libraryc                 C   r   rA   )r   target_contextr   r0   r0   r1   r!   :  r   z_GufuncWrapper.contextc                 C   r   rA   )r!   r   r   r0   r0   r1   r   >  r   z_GufuncWrapper.call_convc                 C   r   rA   )r   r(   r   r0   r0   r1   r(   B  r   z_GufuncWrapper.signaturec                 C   r   rA   )r   rn   r   r0   r0   r1   rn   F  r   z_GufuncWrapper.fndescc                 C   r   rA   )r   ro   r   r0   r0   r1   r   J  r   z_GufuncWrapper.envc                 C   sR   t d}t |}t |}| jtj}t |}t t  ||||g}|S )NrU   )	r   rb   rc   r!   rd   r   r_   re   rf   )r   r   r   r   r   r   r   r0   r0   r1   _wrapper_function_typeN  s   



z%_GufuncWrapper._wrapper_function_typec           !         s  | j tj}|  }|d}| j| jj	| jj
}| jj}tj|||d}|jd t|||}	d|	_|	j\}
}}}d|
_d|_d|_d|_t|	d	}|j|d
d}| j |}t }| j| jfD ]}|D ]}|t|O }qkqgi }| jD ]}|D ]}||vrt|||< q~qzi }| D ]\}}|||| j tj|d g||< qg }t| jt| j }tt| j j| j| j D ]\}\}}t!| j ||
||||||	}|t|7 }|"| q|d}| #|| t$j%|||d"  fdd|D }| &||||\}} t$'|| | W d   n	1 sw   Y  |(| |)| | *|| |+  |,| |-| j. dS )z
        The LLVM IRBuilder code to create the gufunc wrapper.
        The *library* arg is the CodeLibrary to which the wrapper should
        be added.  The *name* arg is the name of the wrapper function being
        created.
        Z_gufunc_wrapperrW   rX   Zweak_odrr   rY   rZ   r[   r\   r]      z.returnr^   c                    s   g | ]}|  jqS r0   )get_array_at_offsetr|   )r<   ar   r0   r1   r>     s    z1_GufuncWrapper._build_wrapper.<locals>.<listcomp>N)/r!   rd   r   r_   r   ri   r   rj   rn   restypeargtypesZllvm_func_namer   rk   rl   r   linkager   r   r   rm   r   ry   setr   r   r4   itemsr   rt   rq   r   r(   
GUArrayArgrr   gen_prologuer   rz   gen_loop_bodycbranch_or_continuebranchposition_at_endgen_epiloguer{   r}   r~   r   )!r   r   r   r   r   r   r   r   r"   r   r   r   r   r   r#   r]   r)   Zunique_symsgrpsymsZsym_mapssym_dimr   r$   step_offsetr   symr/   Zbbreturnr   	innercallerrorr0   r   r1   _build_wrapperY  s|   









z_GufuncWrapper._build_wrapperc                 C   s   | j r| j t| }| || |S | j| jj	| jj
}|d u rA| j t| }|  | || | j| jj	| |S rA   )r   r!   rg   rh   ra   r   r   Zload_overloadr   r(   r   Zenable_object_cachingZsave_overloadr   Zwrapper_namer   r0   r0   r1   _compile_wrapper  s   z_GufuncWrapper._compile_wrapperc                 C   s&   d| j j }| |}t|| j|dS )Nz__gufunc__.r   )rn   Zmangled_namer   r   r   r   r0   r0   r1   build  s
   
z_GufuncWrapper.buildc                 C   s   | j ||| jj| jj|\}}|j|jdd | }| jj 	||| |
| W d    n1 s5w   Y  |j|jfS )NFr   )r   r   r(   r   r   rK   is_errorr   r!   r   r   r    )r   r#   r)   r"   r   r+   r,   r-   r0   r0   r1   r     s   z_GufuncWrapper.gen_loop_bodyc                 C      d S rA   r0   r   r#   r)   r0   r0   r1   r        z_GufuncWrapper.gen_prologuec                 C   r   rA   r0   r   r0   r0   r1   r     r   z_GufuncWrapper.gen_epilogueN)r   r   r   r   propertyr   r!   r   r(   rn   r   r   r   r   r   r   r   r   r   r0   r0   r0   r1   r   %  s,    





T
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_GufuncObjectWrapperc                 C   s"   t | j|||| j|\}}||fS rA   )_prepare_call_to_object_moder!   r(   )r   r#   r)   r"   r   r   r   r0   r0   r1   r     s   z"_GufuncObjectWrapper.gen_loop_bodyc                 C   s   |  | _d S rA   )r   r-   r   r0   r0   r1   r     s   z!_GufuncObjectWrapper.gen_prologuec                 C   s   | | j d S rA   )r   r-   r   r0   r0   r1   r     s   z!_GufuncObjectWrapper.gen_epilogueN)r   r   r   r   r   r   r0   r0   r0   r1   r     s    r   c                 C   s2   |j }|jtjkrtnt}|| |||||d S )N)r   )r(   r   r   r3   r   r   r   )r   r   r   r   r   r   r(   Zwrapclsr0   r0   r1   build_gufunc_wrapper  s   r   c           &   
   C   sX  |j }|d}| tj}| tj}	t|	}
| tj}| tj	}t
|||
|
|||g}t||d}tj|tddd}|tj| g }g }tt||jD ]\}\}}tj||dd}|| t|tjr| |}|| ||d}t|d	}t||j}||d
||g}||d||g}||j|}tt |j}t||j!}t||j"}|#|||||||g} n|$||} || | ||  t%|| }!||!| t&||!| qVtj	gt'| }"| j()||tj	|"|\}#}$||#j*| |+|$ |,| |-| |D ]}|+|.| q|#j/}%|%|.|fS )Nzufunc.core.returnZnumba_ndarray_newr   r   rW   T)zfill)rM   r   shapestrides)0rp   rm   rd   r   Zint32r_   r   rc   Zvoidptrr3   re   r   Zget_or_insert_functionru   rb   r   rv   rq   r   r   rr   r`   Array
make_arrayr   ndimr   Z_get_ptr_by_namer   r[   npdtypera   numitemsizecallrG   Zis_nullr   r4   r   r   r   r5   r   r   r   r    )&r!   r#   r)   r"   r(   r   modZbb_core_returnZll_intZll_intpZll_intp_ptrZ
ll_voidptrZll_pyobjr   Zfn_array_newZerror_pointerZobject_argsZobject_pointersr   argZargtyZobjptraryclsarrayr   ndrY   r   r[   r   Ztype_numr   objZobj_is_nullZ
object_sigr+   r,   r   r0   r0   r1   r     sp   











r   c                   @   s   e Zd Zdd Zdd ZdS )r   c
                    sR  || _ || _|tj|}
|j|j||
gdddd}|| _|j||
gdd}||}t|tj	r| }t
||jkrQt
|dkrH|jdkrHn	td|d |j} fdd	|D }g }t|D ]}|j||tj|| gd
d}||}|| qc|rtnt}||j|||||d| _d S |rtd||d t||d| _d S )Nzdata.ptrrW   r[   zcore.step.ptrr   r   z.type and shape signature mismatch for arg #{0}c                    s   g | ]} | qS r0   r0   )r<   r   r   r0   r1   r>   {  s    z'GUArrayArg.__init__.<locals>.<listcomp>zstep.ptrr   r   	core_step	as_scalarr   r   z2scalar type {0} given for non scalar argument #{1}r   stride)r!   r#   rt   r   r_   r   r   r[   r`   r   r4   r   	TypeErrorformatrangerr   _ArrayAsScalarArgLoader_ArrayArgLoaderr   _loader_ScalarArgLoader)r   r!   r#   r   rZ   r   r   r   r   r   r   r[   Zcore_step_ptrr   r   r   r   r   jZstepptrr   Zldclsr0   r   r1   r   \  sZ   


zGUArrayArg.__init__c                 C   s   | j j| j| j| j|dS )N)r!   r#   r[   rR   )r   r   r!   r#   r[   )r   rR   r0   r0   r1   r     s   zGUArrayArg.get_array_at_offsetN)r   r   r   r   r   r0   r0   r0   r1   r   [  s    ;r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z
    Handle GFunc argument loading where a scalar type is used in the core
    function.
    Note: It still has a stride because the input to the gufunc can be an array
          for this argument.
    c                 C   s   || _ || _d S rA   r   )r   r   r   r0   r0   r1   r     s   
z_ScalarArgLoader.__init__c                 C   s:   | |||| jg}|||| j }||S rA   )r   mulr   r   r   r   r   r   )r   r!   r#   r[   rR   Zdptrr0   r0   r1   r     s
   
z_ScalarArgLoader.loadN)r   r   r   __doc__r   r   r0   r0   r0   r1   r     s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   zD
    Handle GUFunc argument loading where an array is expected.
    c                 C   s(   || _ || _|| _|| _|| _|| _d S rA   r   )r   r   r   r   r   r   r   r0   r0   r1   r     s   
z_ArrayArgLoader.__init__c              	   C   s   t j| j| jdd}||}|||}t|||| j|}| 	||\}	}
|
|| j}|j||||jj|	|
|t j|d d | S )NA)r   r   Zlayout)r[   r   r   r   Zmeminfo)r   r   r   r   r   r   r   r   r   _shape_and_stridesr   r   Zpopulate_arrayr   r[   typert   r_   Z	_getvalue)r   r!   r#   r[   rR   Zarytypr   r   Zoffseted_datar   r   r   r0   r0   r1   r     s.   

	z_ArrayArgLoader.loadc                 C   s$   t || j}t || j}||fS rA   )r   
pack_arrayr   r   )r   r!   r#   r   r   r0   r0   r1   r     s   z"_ArrayArgLoader._shape_and_stridesN)r   r   r   r   r   r   r   r0   r0   r0   r1   r     s
    r   c                   @   s   e Zd ZdZdd ZdS )r   z
    Handle GUFunc argument loading where the shape signature specifies
    a scalar "()" but a 1D array is used for the type of the core function.
    c                 C   s@   | tjd}| tjd}t||g}t||g}||fS )Nr   r   )rt   r   r_   r   r   )r   r!   r#   oner   r   r   r0   r0   r1   r     s
   z*_ArrayAsScalarArgLoader._shape_and_stridesN)r   r   r   r   r   r0   r0   r0   r1   r     s    r   )#collectionsr   numpyr   Zllvmlite.irr   r   Zllvmliter   Z
numba.corer   r   Znumba.core.compiler_lockr   Znumba.core.cachingr	   r
   r   r2   r7   rF   rP   rT   r   objectrs   r   r   r   r   r   r   r   r   r   r0   r0   r0   r1   <module>   s2      o) <
`A+