
    J/Ph                     .   d Z ddlZddlmZmZ ddlZddlmZ ddlm	Z	m
Z
mZmZ ddlZ ej        d          Z ej        d          Z ej        d          Z ej        e	j                  Ze                                Z ed          Z ed          Z ed          Z ed          Zd	 Zd[d
Zd Zi Zd\dZi fdZ  G d de!          Z" G d de"          Z# G d de"          Z$ G d de!          Z%d]dZ&d Z'd^dZ(d Z)d Z*d Z+d_dZ,d  Z-d! Z.d" Z/d# Z0d$ Z1d% Z2d& Z3d' Z4 ej5        d(d)          Z6ed`d*            Z7edad,            Z8ed-             Z9edbd/            Z:ed0             Z;d[d1Z<d2 Z=d[d3Z>	 	 dcd4Z?d[d5Z@	 dcd6ZAd7 ZBd8 ZCd9 ZDd: ZEeDZFeCZGd; ZHed<efd=            ZId> ZJd? ZKd[d@ZLedddA            ZMeKZNdB ZOdC ZPdD ZQdE ZRdF ZSd[dGZTdH ZUdI ZVdedKZWdL ZXdM ZYdN ZZdO Z[dfdPZ\dfdQZ]dR Z^dS Z_dT Z`dU ZadV ZbdW ZcdX ZddY ZedZ ZfdS )gz+
Generic helpers for LLVM code generation.
    N)contextmanager	ExitStack)ir)utilstypesconfig	debuginfo          c                 V    |                      d||                    d                    S )N!=r   )icmp_unsignedtypebuildervalues     R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numba/core/cgutils.pyas_bool_bitr      s$      uejjmm<<<    c                     |t          j        d |D                       } |t           j                  }t          |          D ]\  }}|                     |||          }|S )zH
    Create an anonymous struct containing the given LLVM *values*.
    Nc                     g | ]	}|j         
S  r   .0vs     r   
<listcomp>z)make_anonymous_struct.<locals>.<listcomp>%   s    +C+C+CqAF+C+C+Cr   r   LiteralStructType	Undefined	enumerateinsert_value)r   valuesstruct_type
struct_valir   s         r   make_anonymous_structr(       st     *+C+CF+C+C+CDDR\**J&!! < <1))*a;;

r   c                     t          |           }t          |          }t          j        t          j        t          j        d          |          |          S )z0
    Make a byte array constant from *buf*.
    r   )	bytearraylenr   Constant	ArrayTypeIntType)bufbns      r   make_bytearrayr2   ,   s@     	#AAA;r|BJqMM155q999r   r   c                     | |f}t                               |          }|]t          t          d|         }|j        dz   t          |           z   }|f}t          |           }t          |||          }|t           |<   |S )zK
    Returns a specialized StructProxy subclass for the given fe_type.
    N)r   data_)_fe_type)_struct_proxy_cachegetValueStructProxyDataStructProxy__name__strdictr   )fe_typekind	cache_keyresbaseclsnamebases
clsmemberss           r   create_struct_proxyrF   8   s     $I

!
!)
,
,C
{)'  -#%G47+++
7E:..),I&Jr   c                    |                                 }|j        j        D ]7}|                    |t	          ||                    }t          | ||           8|                                D ]\  }}t          | ||           | S )zJ
    Copy structure from *src* to *dst* with replacement from *repl*.
    )copy
_datamodel_fieldspopgetattrsetattritems)dstsrcreplkr   s        r   copy_structrS   K   s     99;;D^#  HHQQ((Q

  1QJr   c                   |     e Zd ZdZdZddZd Zd Zd Zd Z	d Z
d	 Zd
 Z fdZd Zd Zd Zd Zd Zd Z xZS )_StructProxyz
    Creates a `Structure` like interface that is constructed with information
    from DataModel instance.  FE type must have a data model that is a
    subclass of StructModel.
    Nc                    || _         | j         j        | j                 | _        t	          | j        t
          j        j        j                  s't          d
                    | j                            || _        |                     | j                  | _        t          | j                  rJ |                     |          \  }}|j        j        | j        k    r1t%          d| j                                        d|j                  |T|j        |j        j        k    r$t%          d|j        j        d|j                  | j                            ||           || _        || _        d S )NzNot a structure model: {0}zbad ref type: expected z, got zbad value type: expected )_contextdata_model_managerr6   rI   
isinstancenumbacore	datamodelStructModel	TypeErrorformat_builder_get_be_type_be_type
is_pointer
_make_refsr   pointeeAssertionError
as_pointerstore_value
_outer_ref)selfcontextr   r   ref	outer_refs         r   __init__z_StructProxy.__init__c   sk   -:4=I$/5:+?+KLL 	F,33DODDF F F))$/::dm,,,,,--	38t},, .$(M$<$<$>$>$>$>"J K K K zY^333$n(1(>(>(>

&L M M MMy111#r   c                 F    |t          | j        | j        d          }||fS )z
        Return an (outer ref, value ref) pair.  By default, these are
        the same pointers, but a derived class may override this.
        NTzfill)alloca_oncer`   rb   )rk   rm   s     r   rd   z_StructProxy._make_refs|   s*    
 ;dmT]$GGGCCxr   c                     t           NNotImplementedErrorrk   r\   s     r   ra   z_StructProxy._get_be_type       !!r   c                     t           ru   rv   rk   indexvals      r   _cast_member_to_valuez"_StructProxy._cast_member_to_value   ry   r   c                     t           ru   rv   r{   s      r   _cast_member_from_valuez$_StructProxy._cast_member_from_value   ry   r   c                 :    t          | j        | j        d|          S Nr   )gep_inboundsr`   ri   rk   r|   s     r   _get_ptr_by_indexz_StructProxy._get_ptr_by_index   s    DM4;5AAAr   c                 `    | j                             |          }|                     |          S ru   )rI   get_field_positionr   )rk   attrnamer|   s      r   _get_ptr_by_namez_StructProxy._get_ptr_by_name   s+    228<<%%e,,,r   c                     |                     d          s | | j                            |                   S t          |          z;
        Load the LLVM value of the named *field*.
        r5   )
startswithrI   r   AttributeErrorrk   fields     r   __getattr__z_StructProxy.__getattr__   sC     $$ 	(::5AABB '''r   c                     |                     d          r)t          t          |                               ||          S || | j                            |          <   dS z@
        Store the LLVM *value* into the named *field*.
        r5   N)r   superrU   __setattr__rI   r   rk   r   r   	__class__s      r   r   z_StructProxy.__setattr__   s\     C   	Gt,,88FFF:?T_//66777r   c                     | j                             |                     |                    }|                     ||          S z>
        Load the LLVM value of the field at *index*.
        )r`   loadr   r~   )rk   r|   
member_vals      r   __getitem__z_StructProxy.__getitem__   s=     ]''(>(>u(E(EFF
))%<<<r   c                    |                      |          }|                     ||          }|j        |j        j        k    rt	          |j                  rit	          |j        j                  rP|j        j        |j        j        j        k    r1| j                            | j        ||j        j        j                  }n&t          d
                    ||| |                    | j                            ||           dS )C
        Store the LLVM *value* into the field at *index*.
        zjInvalid store of {value.type} to {ptr.type.pointee} in {self._datamodel} (trying to write member #{index}))r   ptrrk   r|   N)r   r   r   re   rc   rW   addrspacecastr`   	addrspacer^   r_   rh   )rk   r|   r   r   s       r   __setitem__z_StructProxy.__setitem__   s    $$U++,,UE:::)))5:&& 6:ch6F+G+G 6
*ch.>.FFF 33DM4947H4D4NP P   !D "(e4.3 "( "5 "5	6 6 6 	E3'''''r   c                     | j         j        S z.
        Return the number of fields.
        )rI   field_countrk   s    r   __len__z_StructProxy.__len__   s     **r   c                     | j         S zF
        Return the LLVM pointer to the underlying structure.
        )rj   r   s    r   _getpointerz_StructProxy._getpointer   s     r   c                 @    | j                             | j                  S zM
        Load and return the value of the underlying LLVM structure.
        )r`   r   rj   r   s    r   	_getvaluez_StructProxy._getvalue   s     }!!$/222r   c                     t          |j                  rJ |j        | j        k    sJ |j        | j        f            | j                            || j                   dS )z4
        Store the value in this structure.
        N)rc   r   rb   r`   rh   ri   rk   r   s     r   	_setvaluez_StructProxy._setvalue   s`     ej)))))zT]***UZ,G***E4;/////r   NN)r;   
__module____qualname____doc__r6   ro   rd   ra   r~   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r   rU   rU   Z   s(         H$ $ $ $2  " " "" " "" " "B B B- - -( ( (@ @ @ @ @= = =( ( (.+ + +  3 3 30 0 0 0 0 0 0r   rU   c                   $    e Zd ZdZd Zd Zd ZdS )r9   zl
    Create a StructProxy suitable for accessing regular values
    (e.g. LLVM values or alloca slots).
    c                 *    |                                 S ru   )get_value_typerx   s     r   ra   zValueStructProxy._get_be_type   s    '')))r   c                     |S ru   r   r{   s      r   r~   z&ValueStructProxy._cast_member_to_value       
r   c                     |S ru   r   r{   s      r   r   z(ValueStructProxy._cast_member_from_value   r   r   Nr;   r   r   r   ra   r~   r   r   r   r   r9   r9      sK         * * *      r   r9   c                   $    e Zd ZdZd Zd Zd ZdS )r:   zO
    Create a StructProxy suitable for accessing data persisted in memory.
    c                 *    |                                 S ru   )get_data_typerx   s     r   ra   zDataStructProxy._get_be_type   s    &&(((r   c                 l    | j                             |          }|                    | j        |          S ru   )rI   	get_model	from_datar`   rk   r|   r}   models       r   r~   z%DataStructProxy._cast_member_to_value   s-    ))%00t}c222r   c                 l    | j                             |          }|                    | j        |          S ru   )rI   r   as_datar`   r   s       r   r   z'DataStructProxy._cast_member_from_value   s-    ))%00}}T]C000r   Nr   r   r   r   r:   r:      sK         ) ) )3 3 31 1 1 1 1r   r:   c                   `     e Zd ZdZddZd Zd Zd Z fdZd	 Z	d
 Z
d Zd Zd Zd Z xZS )	Structurezs
    A high-level object wrapping a alloca'ed LLVM structure, including
    named fields and attribute access.
    NFc                 v   |                     |           | _        || _        || _        |vt	          || j        d          | _        |Wt          |j                  rJ |j        | j        k    sJ |j        | j        f            |                    || j                   n|J t          |j                  sJ | j        |j        j	        k    rT|r.|
                    || j                                                  }n$t          d|j        j	        d| j                  || _        i | _        g | _        g | _        t!          d          }t#          | j                  D ]U\  }\  }}	|| j        |<   | j                            |t!          |          f           | j                            |	           Vd S )NTrq   zmismatching pointer type: got z, expected r   )get_struct_type_typerW   r`   rs   ri   rc   r   rh   re   bitcastrg   r^   _namemap_fdmap_typemapint32_tr"   rJ   append)
rk   rl   r   r   rm   cast_refrB   r'   rR   tps
             r   ro   zStructure.__init__  s   ,,T22
;%gtzFFFDK %ej11111zTZ///%*dj1I///eT[111===ch'''''zSX--- :!//#tz/D/D/F/FGGCC#)8+++TZZ9: : : DKqzz#DL11 	% 	%JAw2 DM!Kgajj1222M  $$$$	% 	%r   c                 `    | j                             | j        | j        |         d          }|S )NT)inbounds)r`   gepri   r   )rk   r|   r   s      r   r   zStructure._get_ptr_by_index$  s,    mT[-?$OO
r   c                 B    |                      | j        |                   S ru   )r   r   )rk   r   s     r   r   zStructure._get_ptr_by_name(  s    %%dmH&=>>>r   c                 p    |                     d          s| | j        |                  S t          |          r   )r   r   r   r   s     r   r   zStructure.__getattr__+  s:     $$ 	(e,-- '''r   c                     |                     d          r)t          t          |                               ||          S || | j        |         <   dS r   )r   r   r   r   r   r   s      r   r   zStructure.__setattr__4  sS     C   	DD))55eUCCC%*T]5!"""r   c                 \    | j                             |                     |                    S r   )r`   r   r   r   s     r   r   zStructure.__getitem__<  s(    
 }!!$"8"8"?"?@@@r   c                    |                      |          }|j        j        |j        k    r@d}t          ||t	          |j        j                  t	          |j                  fz            | j                            ||           dS )r   z:Type mismatch: __setitem__(%d, ...) expected %r but got %rN)r   r   re   rf   r<   r`   rh   )rk   r|   r   r   fmts        r   r   zStructure.__setitem__C  s     $$U++8uz))NC (+CH,<(=(=(+EJ(9 "9 : : : 	E3'''''r   c                 *    t          | j                  S r   )r+   r   r   s    r   r   zStructure.__len__O  s     4=!!!r   c                     | j         S r   )ri   r   s    r   r   zStructure._getpointerU  s     {r   c                 @    | j                             | j                  S r   )r`   r   ri   r   s    r   r   zStructure._getvalue[  s     }!!$+...r   c                     t          |j                  rJ |j        | j        k    sJ |j        | j        f            | j                            || j                   dS )z!Store the value in this structureN)rc   r   r   r`   rh   ri   r   s     r   r   zStructure._setvaluea  s^    ej)))))zTZ'''%*dj)A'''E4;/////r   )NNF)r;   r   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   s   @r   r   r      s         % % % %>  ? ? ?( ( (+ + + + +A A A
( 
( 
(" " "  / / /0 0 0 0 0 0 0r   r    Fc                    t          |t                    rt          j        t          |          }t          j        |           5  |                                 5  |                     |||          }| 	                     |d          |           ddd           n# 1 swxY w Y   |r.| 	                    |j
                            d          |           |cddd           S # 1 swxY w Y   dS )a  Allocate stack memory at the entry block of the current function
    pointed by ``builder`` with llvm type ``ty``.  The optional ``size`` arg
    set the number of element to allocate.  The default is 1.  The optional
    ``name`` arg set the symbol name inside the llvm IR for debugging.
    If ``zfill`` is set, fill the memory with zeros at the current
    use-site location.  Note that the memory is always zero-filled after the
    ``alloca`` at init-site (the entry block).
    )sizenameN)rY   intr   r,   intp_tr	   suspend_emissiongoto_entry_blockallocarh   r   re   )r   tyr   r   rr   r   s         r   rs   rs   j  si    $ ){64(( 
	#G	,	,  %%'' 	) 	)..$T.::CMM""T((C(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)
  	7MM#(**400#666                 s6   C&8BC&B!	!C&$B!	%4C&&C*-C*c                      |d          }|                     t          d          g          }|                     |t                    S )zCompute sizeof using GEP
    Nr
   )r   r   ptrtointr   )r   ptr_typenulloffsets       r   sizeofr     s@     8D>>DXXwqzzl##FFF+++r   c                 `    t          | |j        |          }|                     ||           |S )z
    Like alloca_once(), but passing a *value* instead of a type.  The
    type is inferred and the allocated slot is also initialized with the
    given value.
    rq   )rs   r   rh   )r   r   r   rr   storages        r   alloca_once_valuer     s3     '5:U;;;GMM%!!!Nr   c                     t          | ||          }|j                            d           |j                            d           |S )z_
    Insert a pure function (in the functional programming sense) in the
    given module.
    readonlynounwind)get_or_insert_function
attributesaddmodulefntyr   fns       r   insert_pure_functionr     sF    
 
 d	3	3BMj!!!Mj!!!Ir   c                 l    | j                             |d          }|t          j        | ||          }|S )zm
    Get the function named *name* with type *fnty* from *module*, or insert it
    if it doesn't exist.
    N)globalsr8   r   Functionr   s       r   r   r     s8    
 
		D$	'	'B	z[t,,Ir   c                 x    	 |                      |          S # t          $ r |                     |          cY S w xY wru   )get_named_metadataKeyErroradd_named_metadata)r   r   s     r   get_or_insert_named_metadatar    sP    /((... / / /((...../s    99c                 Z    |                      |          }t          j        | |||          S ru   )get_unique_namer   GlobalVariable)r   r   r   r   unique_names        r   add_global_variabler    s,    ((..KVRi@@@r   c                 P    | j         }|j        |                     |           d S d S ru   )basic_block
terminatorbranch)r   bbendbbs      r   	terminater    s2    		B	}u r   c                      | d           S ru   r   )ltypes    r   get_null_valuer    s    5;;r   c                 X    t          |j                  }|                     d||          S )N==r  r   r   r   r}   r   s      r   is_nullr    )    #(##D  tS111r   c                 X    t          |j                  }|                     d||          S )Nr   r  r  s      r   is_not_nullr    r  r   c                 0    |                      |d          S )NFlikelyif_thenr   preds     r   if_unlikelyr%    s    ??4?...r   c                 0    |                      |d          S )NTr  r!  r#  s     r   	if_likelyr'    s    ??4?---r   c                 R    |                      |                     |                    S ru   )r"  not_r#  s     r   ifnotr*    s     ??7<<--...r   c                 ^    |                     d          }|                     ||dg          S )z#
    Increment an index *val*.
    r
   nsw)flags)r   r   )r   r}   ones      r   increment_indexr/    s/     ((1++C ;;sCw;///r   Loop)r|   do_breakc              #   2   K   ||j         }| |d          }|}                     d          }                     d          }                     d           fd} j        }                     |                                |          5                       |d          }	                     d	|	|          }
                     |
|           ddd           n# 1 swxY w Y                        |          5  t          |	|          V   j        }t           |	          }t           |           ddd           n# 1 swxY w Y   |	                    ||           |	                    ||                                           dS )
a  
    Generate LLVM IR for a for-loop in [start, count).
    *start* is equal to 0 by default.

    Yields a Loop namedtuple with the following members:
    - `index` is the loop index's value
    - `do_break` is a no-argument callable to break out of the loop
    Nr   for.condfor.bodyfor.endc                  2                                     d S ru   )r  )r  r   s   r   r1  zfor_range.<locals>.do_break  s    ur   
loop.indexr   <)r   append_basic_blockr  r  
goto_blockphiicmp_signedcbranchr0  r/  r  add_incomingposition_at_end)r   countstartintpstopbbcondbbbodyr1  bbstartr|   r$  incrr  s   `           @r   	for_rangerI    s;      |z}QD''
33F''
33F&&y11E      !GNN6			F	#	# - -D|44""3t44fe,,,- - - - - - - - - - - - - - -
 
		F	#	# # #5(#####$w..'6"""# # # # # # # # # # # # # # # 
ug&&&	tV$$$E"""""s%   AC%%C),C):EEETc              #     K   ||j         }|                     d          }|                     d          }|                     d          }| j        }	|                     |           |                     |          5  |                     |d          }
|                     |d          }|r|                     d|
|          }n|                     d	|
|          }|                     |||           ddd           n# 1 swxY w Y   |                     |          5  |
|fV  | j        }|                     |
|          }t          | |          }t          | |           ddd           n# 1 swxY w Y   |
                    ||	           |
                    ||           |                    t          j        |d
          |	           |                    ||           |                     |           dS )a[  
    Generate LLVM IR for a for-loop based on a slice.  Yields a
    (index, count) tuple where `index` is the slice index's value
    inside the loop, and `count` the iteration count.

    Parameters
    -------------
    builder : object
        IRBuilder object
    start : int
        The beginning value of the slice
    stop : int
        The end value of the slice
    step : int
        The step value of the slice
    intp :
        The data type
    inc : boolean, optional
        Signals whether the step is positive (True) or negative (False).

    Returns
    -----------
        None
    Nr3  r4  r5  r7  r8  z
loop.countr9  >r   )r   r:  r  r  r;  r<  r=  r>  r   r/  r  r?  r   r,   r@  )r   rB  rD  steprC  incrE  rF  r  rG  r|   rA  r$  rH  
next_counts                  r   for_range_slicerO    s}     4 |z''
33F''
33F&&y11E!GNN6			F	#	# - -D|44D|44 	9&&sE488DD&&sE488Dfe,,,- - - - - - - - - - - - - - - 
		F	#	# # #Ul${{5$''$We44
'6"""# # # # # # # # # # # # # # # 
ug&&&	tV$$$	r{4++W555	z6***E"""""s&   <A7C??DDAE//E36E3c              #   z  K   |j         }|                     d|t          j        |d                    }t	          | ||||d          }t	          | ||||d          }t
          d             }|                     |d          5 \  }	}
 ||	|           ||
|          fV  ddd           dS # 1 swxY w Y   dS )	a  
    A helper wrapper for for_range_slice().  This is a context manager which
    yields two for_range_slice()-alike context managers, the first for
    the positive step case, the second for the negative step case.

    Use:
        with for_range_slice_generic(...) as (pos_range, neg_range):
            with pos_range as (idx, count):
                ...
            with neg_range as (idx, count):
                ...
    >=r   T)rM  Fc              3   |   K   | 5  |5 }|V  d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S ru   r   )condinner_cmr   s      r   cm_condz(for_range_slice_generic.<locals>.cm_cond`  s       	 	 U              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s$   11	1	155r  N)r   r=  r   r,   rO  r   if_else)r   rB  rD  rL  rC  is_pos_steppos_for_rangeneg_for_rangerU  then	otherwises              r   for_range_slice_genericr\  L  s9      :D%%dD"+dA2F2FGGK#GUD$$OOOM#GUD$%PPPM  ^
 
T	2	2 N6GtYgdM**GGI},M,MMMMMN N N N N N N N N N N N N N N N N Ns   B00B47B4Cc              #     K   |dv sJ |sdV  dS |dk    rd }nd }t          |  ||          |          5 }t          |          t          |          k    sJ  ||          V  ddd           dS # 1 swxY w Y   dS )a  
    Generate a loop nest walking a N-dimensional array.
    Yields a tuple of N indices for use in the inner loop body,
    iterating over the *shape* space.

    If *order* is 'C' (the default), indices are incremented inside-out
    (i.e. (0,0), (0,1), (0,2), (1,0) etc.).
    If *order* is 'F', they are incremented outside-in
    (i.e. (0,0), (1,0), (2,0), (0,1) etc.).
    This has performance implications when walking an array as it impacts
    the spatial locality of memory accesses.
    CFr   Fc                     | d d d         S )Nr   xs    r   <lambda>zloop_nest.<locals>.<lambda>~  s    a"g r   c                     | S ru   r   rc  s    r   re  zloop_nest.<locals>.<lambda>  s    a r   N)
_loop_nestr+   )r   shaperC  order_swapindicess         r   	loop_nestrl  j  s       D==== 
!C<<%%EEKEut44 	!w<<3u::----%..   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   0A55A9<A9c              #   $  K   t          | |d         |          5 }t          |          dk    r?t          | |dd          |          5 }|j        f|z   V  d d d            n# 1 swxY w Y   n
|j        fV  d d d            d S # 1 swxY w Y   d S )Nr   rC  r
   )rI  r+   rg  r|   )r   rh  rC  looprk  s        r   rg  rg    s#     	7E!H4	0	0	0  Du::>>GU122Y55 .zmg----. . . . . . . . . . . . . . . :-                                   s5   -BA"B"A&	&B)A&	*BB	B	c                     t          |          }||d         j        } t          j        ||          t          j                  }t          |          D ]\  }}|                     |||          }|S )z
    Pack a sequence of values in a LLVM array.  *ty* should be given
    if the array may be empty, in which case the type can't be inferred
    from the values.
    Nr   )r+   r   r   r-   r!   r"   r#   )r   r$   r   r1   aryr'   r   s          r   
pack_arrayrr    su     	FA	zAY^
",r1

bl
+
+C&!! . .1""31--Jr   c                     t          j        d |D                       } |t           j                  }t          |          D ]\  }}|                     |||          }|S )z7
    Pack a sequence of values into a LLVM struct.
    c                     g | ]	}|j         
S r   r   r   s     r   r   zpack_struct.<locals>.<listcomp>  s    $<$<$<QV$<$<$<r   r   )r   r$   structtystr'   r   s         r   pack_structrw    sm     #$<$<V$<$<$<==H	",		B&!! , ,1!!"a++Ir   c                 x     |t          j        j                  } fdt          |          D             }|S )zH
    Unpack an array or structure of values, return a Python tuple.
    Nc                 <    g | ]}                     |          S r   )extract_value)r   r'   r   tups     r   r   z unpack_tuple.<locals>.<listcomp>  s9     # # # !!#q)) # # #r   )r+   r   elementsrange)r   r{  rA  valss   ``  r   unpack_tupler    sU     }CH%&&# # # # #5\\# # #DKr   c                     t          ||j        |j                  }t          ||j        |j                  }t	          | ||j        |||j        |||	  	        S )N)rA  )r4   rh  strideslayoutinds
wraparoundboundscheck)r  rh  ndimr  get_item_pointer2r4   r  )	rl   r   arytyrq  r  r  r  shapesr  s	            r   get_item_pointerr    sg    
 '39EJ???F7CKuzBBBGWgCHF%,U\(2M M M Mr   c                 &   fd}d}                     d          }t          |          5  t          j        r
 |             | j                            t          |f           d d d            n# 1 swxY w Y                        d                    d                    }t          |          5  t          j        r
 |             | j                            t          |f           d d d            d S # 1 swxY w Y   d S )Nc                       Qt           t                    r't          d                                          d S t          d            d S t          d           d S )NzFdebug: IndexError: index %d is out of bounds for axis {} with size %d
zFdebug: IndexError: index %d is out of bounds for axis %d with size %d
z9debug: IndexError: index %d is out of bounds for size %d
)rY   r   printfr_   )axisr   dimleninds   r   _dbgzdo_boundscheck.<locals>._dbg  s     $$$ w !44:F4LL#vO O O O O w !458$     7O         r   zindex is out of boundsrQ  r9  r   )r=  r%  r   FULL_TRACEBACKS	call_convreturn_user_exc
IndexErrorr   )	rl   r   r  r  r  r  msgout_of_bounds_upperout_of_bounds_lowers	    ````    r   do_boundscheckr    s                    #C!--dC@@	W1	2	2 G G! 	DFFF))':vFFFG G G G G G G G G G G G G G G "--c3DD	W1	2	2 G G! 	DFFF))':vFFFG G G G G G G G G G G G G G G G G Gs#   9A;;A?A? 9DD
D
c	                 "   |rg }	t          ||          D ]q\  }
}                    d|
|
                    d                    }                    ||
          }                    |||
          }|	                    |           rn|}	|r9t          t          |	|                    D ]\  }\  }
}t          | |
||           |	s$                    |t          d          g          S |	d         j        }|dv rLg }|dk    rft          t          |                    D ]H} |d          }||dz   d          D ]}                    ||          }|                    |           Inx|dk    rct          t          |                    D ]E} |d          }|d |         D ]}                    ||          }|                    |           Fnt          d           |d          }t          |	|          D ]1\  }}                    ||          }                    ||          }2                    ||g          }|S fdt          ||	          D             }t          j        j        |          }t!          ||          S )	Nr9  r   r_  r]  r
   r`  unreachablec                 B    g | ]\  }}                     ||          S r   )mul)r   sr'   r   s      r   r   z%get_item_pointer2.<locals>.<listcomp>  s+    GGGA7;;q!$$GGGr   )zipr=  r   r   selectr   r"   r  r   r   r}  r+   r  	Exception	functoolsreducepointer_add)rl   r   r4   rh  r  r  r  r  r  rk  r  r  negativewrappedselectedr  rC  stepsr'   lastjlocr  tmpr   dimoffsr   s    `                         r   r  r    s   
  	tU++ 	% 	%KC**3SXXa[[AAHkk&#..G~~h==HNN8$$$$		%  @#,S%-@-@#A#A 	@ 	@D-37GS&$???? /{{4'!**...1:?D~~S==3u::&& # #tAwwq1uvv 0 0A";;tQ//DDT""""	#
 s]]3u::&& # #tAwwrr 0 0A";;tQ//DDT""""	# M*** d1gg'' 	( 	(DAq++a##C++c3''CCkk$&&
 HGGGWg1F1FGGG!'+w777D&111r   c                 <   |                     d          }t          |j         t          j        t          j        f          r |||          }nNt          |j         t          j                  r|                     |||          }nt          d|j                   |S )Nr   zunexpected value type )r   rY   r   	FloatType
DoubleTyper.   r=  r^   )r   r   fpredicondnullvalisnulls         r   _scalar_pred_against_zeror    s    jjmmG%*r|R];<< Dug&&	EJ
	+	+ D$$UE7;;iejjBCCCMr   c                 V    t          | |t          j        | j        d          d          S )zK
    Return a predicate representing whether *value* is equal to zero.
    r  r  r  partialfcmp_orderedr   s     r   is_scalar_zeror  %  s2     %	)'*>EEtM M Mr   c                 V    t          | |t          j        | j        d          d          S )z
    Return a predicate representing whether a *value* is not equal to zero.
    (not exactly "not is_scalar_zero" because of nans)
    r   r  r  r  fcmp_unorderedr   s     r   is_not_scalar_zeror  -  2    
 %	)'*@$GGO O Or   c                 V    t          | |t          j        | j        d          d          S )z]
    Return a predicate representing whether *value* is equal to either zero
    or NaN.
    r  r  r   s     r   is_scalar_zero_or_nanr  6  r  r   c                 V    t          | |t          j        | j        d          d          S )z:
    Is *value* negative?  Assumes *value* is signed.
    r9  r  r   s     r   is_scalar_negr  C  s2     %	)'*>DDcK K Kr   stackc              #      K   |                     |                     |d                    \  }}|5  dV  ddd           n# 1 swxY w Y   |                     |           dS )a  
    The Python code::

        with contextlib.ExitStack() as stack:
            with early_exit_if(builder, stack, cond):
                cleanup()
            body()

    emits the code::

        if (cond) {
            <cleanup>
        }
        else {
            <body>
        }

    This can be useful for generating code with lots of early exits, without
    having to increase the indentation each time.
    Fr  N)enter_contextrV  )r   r  rS  rZ  r[  s        r   early_exit_ifr  K  s      , ))'//$u/*M*MNNOD)	                		"""""s   AA
Ac                 @    t          | |t          | |                    S )z
    A convenience wrapper for :func:`early_exit_if`, for the common case where
    the CPython API indicates an error by returning ``NULL``.
    )r  r  )r   r  objs      r   early_exit_if_nullr  g  s     
 %#)>)>???r   c                     |                     t          ||          d          5  |d         }|dd         pd}| j                            |||           ddd           dS # 1 swxY w Y   dS )zu
    Guard against *value* being null or zero.
    *exc_tuple* should be a (exception type, arguments...) tuple.
    Fr  r   r
   N)r"  r  r  r  )rl   r   r   	exc_tupleexcexc_argss         r   
guard_nullr  o  s    
 
77	F	F B BlQRR=(D))'3AAAB B B B B B B B B B B B B B B B B Bs   1A$$A(+A(c                 .   t          |j        t          j                  sJ |j                    |r|fnd}|                    t          ||          d          5  | j                            |t          |           ddd           dS # 1 swxY w Y   dS )zG
    Guard against *pointer* being NULL (and raise a MemoryError).
    r   Fr  N)	rY   r   r   PointerTyper"  r  r  r  MemoryError)rl   r   pointerr  r  s        r   guard_memory_errorr  z  s     glBN33AAW\AAA$vv"H	'225	A	A J J))';IIIJ J J J J J J J J J J J J J J J J Js   "B

BBc              #      K   |                      t          | |          |          5  dV  ddd           dS # 1 swxY w Y   dS )z>
    Execute the given block if the scalar value is zero.
    r  N)r"  r  )r   r   r   s      r   if_zeror    s      
 
77	G	G                   s   :>>c                 6    t          | t          j                  S )z7
    Whether the LLVM type *typ* is a struct type.
    )rY   r   r  )ltyps    r   rc   rc     s     dBN+++r   c                     t          | |d|          }t          |j        j                  rJ |                     ||                                          S r   )r   rc   r   re   r   rg   )r   recordr   typpvals        r   get_record_memberr    sL    F33D$)+,,,,,??4!1!1222r   c                 V    |                      d||                    d                    S )Nr9  r   )r=  r   )r   r}   s     r   
is_neg_intr    s$    sC!555r   c                 (    t          | |g|R ddi|S )z8
    Same as *gep*, but add the `inbounds` keyword.
    r   T)r   )r   r   r  kwss       r   r   r     s)     w9d999T9S999r   c                    |                     dd          }|                     dd          }|rJ g }|D ]>}t          |t                    rt          |          }n|}|                    |           ?|                     ||||          S )z
    Emit a getelementptr instruction for the given pointer and indices.
    The indices can be LLVM values or Python int constants.
    r   r   r   F)r   r   )rK   rY   r   r   r   r   )	r   r   r  r  r   r   idxr'   r  s	            r   r   r     s    
 7762Dwwz5))HNNN
C  a 	!**CCC

3;;sCdX;>>>r   c                     |                      |t                    }t          |t                    rt          |          }|                     ||          }|                     ||p|j                  S )z
    Add an integral *offset* to pointer *ptr*, and return a pointer
    of *return_type* (or, if omitted, the same type as *ptr*).

    Note the computation is done in bytes, and ignores the width of
    the pointed item type.
    )r   r   rY   r   r   inttoptrr   )r   r   r   return_typeintptrs        r   r  r    sf     c6**F&#  [[((FFK$;38<<<r   c           	          | j                             dt          |j        f          }|                     |t                    }t          |t                    rt          |          }|                     ||||t          d          g           dS )z=
    Fill *size* bytes starting from *ptr* with *value*.
    zllvm.memsetr   N)
r   declare_intrinsic	voidptr_tr   r   rY   r   int8_tcallbool_t)r   r   r   r   r   s        r   memsetr    s{     
	)	)-)TY9O	P	PB
//#y
)
)C% uLLc5$q		233333r   c                     |                      |          }t          | |t          | |j                  d           |                     ||           dS )z7
    Fill padding bytes of the pointee with zeros.
    r   N)r   r  r   r   rh   )r   r   r}   s      r   memset_paddingr    sO    
 ,,s

C
7C22A666MM#sr   internalc                     t          | t          j                  r| }n| j        }t	          ||j        |          }||_        d|_        ||_        |S )zO
    Get or create a (LLVM module-)global constant with *name* or *value*.
    T)	rY   r   Moduler   r  r   linkageglobal_constantinitializer)builder_or_moduler   r   r  r   r4   s         r   r  r    sW     #RY// *"")vuz488DDLDDKr   c                    |dk    sJ |                     |          }|                     d          }t          | |j                   }|                     t          | |                    5 \  }}|5  |                     ||          }|                     ||           ddd           n# 1 swxY w Y   |5  |                     ||          }|                     ||          }|                     |                     ||          |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |                     |          }|                     || 	                    ||                    }	||	fS )a  
    Compute the (quotient, remainder) of *val* divided by the constant
    positive *divisor*.  The semantics reflects those of Python integer
    floor division, rather than C's / LLVM's signed division and modulo.
    The difference lies with a negative *val*.
    r   r
   N)
r   rs   rV  r  sdivrh   r   subr   r  )
r   r}   divisorr.  quotif_negif_posquot_valval_plus_onerem_vals
             r   divmod_by_constantr    s    Q;;;;hhwG
((1++Cw))D	GS11	2	2 	<6Fvv 	* 	*||C11HMM(D)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*  	< 	<";;sC00L||L'::HMM'++h44d;;;		< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< ||D!!Hkk#w{{8W==>>GWs[   +D11-B*D1*B.	.D11B.	2D17ADD1D	D1!D	"D11D58D5c                     |                      d          }|                     |||           |                     |           |S )z
    Branch conditionally or continue.

    Note: a new block is created and builder is moved to the end of the new
          block.
    z	.continue)r:  r>  r@  )r   rS  bbtruebbconts       r   cbranch_or_continuer    sF     ''44FOOD&&)))F###Mr   c                 L   |j         |j         k    sJ t          | ||j                   5 }|                     ||j        g          }|                     ||j        g          }|                     |                     |          |           ddd           dS # 1 swxY w Y   dS )z
    Emit a memcpy to the builder.

    Copies each element of dst to src. Unlike the C equivalent, each element
    can be any LLVM type.

    Assumes
    -------
    * dst.type == src.type
    * count is positive
    rn  N)r   rI  r   r|   rh   r   )r   rO   rP   rA  ro  out_ptrin_ptrs          r   memcpyr
    s     8sx	7E
	3	3	3 5t++cDJ<00S4:,//gll6**G4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   A"BB Bc           	         |j         }t          |t                    rt          j        ||          }| j                            |t          t          |g          }t          }	| 	                    || 
                    |t                    | 
                    |t                    |                     ||          |	g           d S ru   )r   rY   r   r   r,   r   r  r  	false_bitr  r   r  )
r   	func_namerO   rP   rA  itemsizealignsize_tr
  is_volatiles
             r   _raw_memcpyr  1  s    ZF(C   1;vx00^--i/8)V.LN NFKLL'//#y99!//#y99!++eX66%' ( ( ( ( (r   c           	      ,    t          | d|||||          S )za
    Emit a raw memcpy() call for `count` items of size `itemsize`
    from `src` to `dest`.
    zllvm.memcpyr  r   rO   rP   rA  r  r  s         r   
raw_memcpyr  ?  s    
 wsC%PPPr   c           	      ,    t          | d|||||          S )zb
    Emit a raw memmove() call for `count` items of size `itemsize`
    from `src` to `dest`.
    zllvm.memmover  r  s         r   raw_memmover  G  s%    
 wS%( ( (r   c                 :   |                      ||          }|                     |d          }|                     |d          }|                     ||          }|                     |d          }|                     ||                     |d                    }	||	fS )zq
    Compute (a * b + c) and return a (result, overflow bit) pair.
    The operands must be signed integers.
    r   r
   )smul_with_overflowrz  sadd_with_overflowor_)
r   ar0   cpprodprod_ovfr  rA   ovfs
             r   muladd_with_overflowr#  P  s    
 	""1a((A  A&&D$$Q**H""4++A


1
%
%C
++h 5 5a ; ;
<
<C8Or   c                    t          |t                    sJ | j        }t          }t	          |dz                       d                    }t          |d|          }t          j        t          |gd          }	 |
                    d          }n'# t          $ r t          j        ||d          }Y nw xY w|                     ||          }	|                     ||	gt          |          z             S )a  
    Calls printf().
    Argument `format` is expected to be a Python string.
    Values to be printed are listed in `args`.

    Note: There is no checking to ensure there is correct number of values
    in `args` and there type matches the declaration in the format string.
     asciiprintf_formatTvar_argr  r8  )rY   r<   r   r  r2   encoder  r   FunctionTyper   
get_globalr  r  r   r  list)
r   r_   argsmodcstring	fmt_bytes
global_fmtr   r   ptr_fmts
             r   r  r  ^  s     fc"""""
.CG66w??@@I oyAAJ?7WIt<<<D3^^H%% 3 3 3[d2223 ooj'22G<<WIT

2333s   :B !B43B4c                     t          |t                    sJ | j        }t          }t	          |dz                       d                    }t          |d|          }t          j        t          |t          |gd          }	d}
t          j        rd|
z   }
	 |                    |
          }n'# t          $ r t          j        ||	|
          }Y nw xY w|                     ||          }|                     ||||gt%          |          z             S )	z8Calls libc snprintf(buffer, bufsz, format, ...args)
    r%  r&  snprintf_formatTr(  snprintfr5   r8  )rY   r<   r   r  r2   r*  r  r   r+  r   r   r   IS_WIN32r,  r  r  r   r  r-  )r   bufferbufszr_   r.  r/  r0  r1  r2  r   symbolr   r3  s                r   r6  r6  x  s    fc"""""
.CG66w??@@I &7CCJ?'67+T  D F v1^^F## 1 1 1[d0001 ooj'22G<<VUG4tDzzABBBs   B* *!CCc                    t          |t                    sJ t          j        t          j        d          |          }t          | |d          }|                     |t                    }t          | |t          |          |g|R   |S )z{Similar to `snprintf()` but the buffer is stack allocated to size
    *bufsz*.

    Returns the buffer pointer as i8*.
    r   Trq   )
rY   r   r   r-   r.   rs   r   r  r6  r   )r   r9  r_   r.  spacetyspacer8  s          r   snprintf_stackbufferr>    s     eS!!!!!l2:a==%00G555E__UI..FWffUmmV;d;;;;Mr   c                 R    |                      d                              d          S )zk
    Normalize the given string to latin1 compatible encoding that is
    suitable for use in LLVM IR.
    utf8latin1)r*  decode)texts    r   normalize_ir_textrD    s$     ;;v%%h///r   c                    d}|                      |t                    }t          | d||           t          j        d          }|                     ||                                          }t          | |          5 }|                     |j	        t          |                    }| 
                    d|t          d                    }|                     |          5  t          | d           ddd           n# 1 swxY w Y   |                     ||j	        g          }|                     |          }	t          | d|	           ddd           n# 1 swxY w Y   t          | d           dS )	zIDebug print the memory region in *ptr* to *ptr + nbytes*
    as hex.
       zhexdump p=%p n=%zur   r  r   
Nz %02x)zextr   r  r   r.   r   rg   rI  uremr|   r   r"  r   r   )
r   r   nbytesbytes_per_linebyte_tr  div_bydo_new_liner   r}   s
             r   hexdumprO    s    N\\&&))F
7(  Z]]F
//#v0022
3
3C	7F	#	# &sci)?)?@@++D&&))DD__[)) 	" 	"7D!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" S39+..ll6""w%%%& & & & & & & & & & & & & & & 7Ds8   <A"EC;/E;C?	?EC?	AEEEc                 r    | du p3t          | t          j                  pt          | t          j                  S )z returns if 'ty' is none N)rY   r   NoneTypeOmittedr   s    r   is_nonelikerT    s8     	d
 	&2u~&&	&2u}%%r   c                 f    t          | t          j                  ot          | j                  dk    S )z# returns if 'ty' is an empty tuple r   )rY   r   Tupler+   rS  s    r   is_empty_tuplerW    s-     	2u{## 	BHr   c                 l    t          j        t          j        | t          |                    |          S )z
    Create an LLVM-constant of a fixed-length array from Python values.

    The type provided is the type of the elements.
    )r   r,   r-   r+   )r   r}   s     r   create_constant_arrayrY    s(     ;r|BC113777r   ru   )r   )Nr   F)r   F)r   r   )NT)r]  )FF)F)r  )r
   )gr   collections
contextlibr   r   r  llvmliter   
numba.corer   r   r   r	   numba.core.datamodelrZ   r.   r  r  r   MACHINE_BITSr   rg   r  true_bitr  	true_byte
false_byter   r(   r2   r7   rF   rS   objectrU   r9   r:   r   rs   r   r   r   r   r  r  r  r  r  r  r%  r'  r*  r/  
namedtupler0  rI  rO  r\  rl  rg  rr  rw  r  r  r  r  r  r  r  r  is_trueis_falser  r  r  r  r  r  
guard_zerorc   r  r  r   r   r  r  r  r  r  r  r
  r  r  r  r#  r  r6  r>  rD  rO  rT  rW  rY  r   r   r   <module>rh     s   
     0 0 0 0 0 0 0 0           6 6 6 6 6 6 6 6 6 6 6 6     
A	A
"*R..	E&	'	'	6!99F1II	F1II	VAYY
= = =	 	 	 	: : :     &  "    B0 B0 B0 B0 B06 B0 B0 B0J    |   1 1 1 1 1l 1 1 1 g0 g0 g0 g0 g0 g0 g0 g0X   2, , ,       / / /A A A A
    2 2 2
2 2 2
/ / /. . ./ / /0 0 0 {f&;<< (# (# (# (#V 6# 6# 6# 6#r N N N: ! ! ! !6           	 	 	 	 EJ!&	M 	M 	M 	MG G G G> 5:72 72 72 72t  M M MO O OO O O K K K #) # # # #6@ @ @B B BJ J J J     
, , ,3 3 36 6 6: : :? ? ?&= = = =4 4 4	 	 	     >
 
 
5 5 5*( ( (Q Q Q Q( ( ( (  4 4 44C C C4  0 0 0  .    8 8 8 8 8r   