
    Xh                        d dl mZ ddlmZ ddlmZ dZeefdej        fd            Zeefdej        fd            Z	eefdej        fd	            Z
eefdej        fd
            Zed             Zeefdej        fd            Zeefdej        fd            Zed             Zeefdej        fd            Zeefdej        fd            ZdS )   )jit   )core)math
   n_roundsc                    | j         t          j        k    r	d}d}d}	d}
n0t          j        | j         t          j        k    d           d}d}d}	d	}
t          j        |          D ]}|	}|
}| |}}t          j        ||          |z  |z  } t          j        ||          |z  |z  }t          j        ||d
          }t          j        ||d
          }t          j	        ||d
          }t          j	        ||d
          }| |||fS )zV
    Run `n_rounds` rounds of Philox for state (c0, c1, c2, c3) and key (k0, k1).
    l   yn< l   .v l   S$ l   W= z"dtype not supported in philox_impll   |~fq	 l   ;'	:=[ l   lB;: l   W$*	R F)sanitize_overflow)
dtypetluint32static_assertuint64static_ranger   umulhimuladd)c0c1c2c3k0k1r   PHILOX_KEY_APHILOX_KEY_BPHILOX_ROUND_APHILOX_ROUND_B_AB_c0_c2s                   h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/triton/language/random.pyphilox_implr$      s&   
 
x29%/%/'1'1
RY.0TUUU%7%7'9'9_X&& ? ? rS[C  2%*[C  2%*VAse444VAse444VB>>>VB>>>r2r>    c           	         t          j        |           } t          j        | j                                                   |                     t           j                  } t          j        |          }t          j        |          }t          j        |          }t          j        |          }t          j        |j        j                  dk    rTt           j	        }| dz	  dz                      t           j	                  }| dz                      t           j	                  }nZt          j        t          j        |j        j                  dk    d           t           j        }t          j
        dd|          }| }|                    |d	          }|                    |d	          }|                    |d	          }|                    |d	          }t          |||||||          S )
N    l    @   z bitwidth not supported in philox)r       )r   Tbitcast)r   	to_tensorr   r   is_inttor   	constexprprimitive_bitwidthr   fullr$   )	seedr   r   r   r   r   	int_dtypeseed_hiseed_los	            r#   philoxr6   -   s   <DTZ&&(()))7729D	b		B	b		B	b		B	b		B	|BH/00B66I	BJ*,00;;*$((33
bh&ABBbHJlmmmI	'%)444	y$	'	'B	y$	'	'B	y$	'	'B	y$	'	'Br2r2wBBBr%   c                 2    t          | ||          \  }}}}|S )aq  
    Given a :code:`seed` scalar and an :code:`offset` block, returns a single
    block of random :code:`int32`.

    If you need multiple streams of random numbers,
    using `randint4x` is likely to be faster than calling `randint` 4 times.

    :param seed: The seed for generating random numbers.
    :param offset: The offsets to generate random numbers for.
    )	randint4x)r2   offsetr   retr   s        r#   randintr;   H   s"     T6844LCAqJr%   c                     |                     t          j                  }|dz  }t          j        |j        j                  dk    r#|dz	                       t          j                  }n|}t          | |||||          S )aU  
    Given a :code:`seed` scalar and an :code:`offset` block, returns four
    blocks of random :code:`int32`.

    This is the maximally efficient entry point
    to Triton's Philox pseudo-random number generator.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    r)   r'   )r.   r   r   r/   r   r0   r6   )r2   r9   r   	offset_lo_0	offset_his         r#   r8   r8   X   sr     		")$$I	QB	|FL344r99r\%%bi00			$	9b"h???r%   c                 2   t          j        | j        t           j        k              s't          j        | j        t           j        k              r$|                     t           j        d          } d}nt          j        t          j        | j        t           j        k              p&t          j        | j        t           j        k                         |                     t           j        d          } d}t          j	        | dk     |  dz
  |           } | |z  S )zo
    Numerically stable function to convert a random uint into a random float uniformly sampled in [0, 1).
    Tr*   g=g   ;r)   r   )
r   r/   r   r   int32r.   r   r   int64where)xscales     r#   uint_to_uniform_floatrF   ~   s     
|AGry()) 'R\!'RX:M-N-N 'DD4D(( 
ag&:;;`r|AGWYW_L_?`?`aaaDD4D((&
QQ""Au9r%   c                 B    t          | ||          }t          |          S )z
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns a block of random :code:`float32` in :math:`U(0, 1)`.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )r;   rF   )r2   r9   r   sources       r#   randrI      s#     T68,,F (((r%   c                     t          | ||          \  }}}}t          |          }t          |          }t          |          }	t          |          }
|||	|
fS )a   
    Given a :code:`seed` scalar and an :code:`offsets` block,
    returns 4 blocks of random :code:`float32` in :math:`U(0, 1)`.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )r8   rF   )r2   offsetsr   i1i2i3i4u1u2u3u4s              r#   rand4xrT      s_     tWh77NBB	r	"	"B	r	"	"B	r	"	"B	r	"	"Br2r>r%   c                     t          j        d|           } d|z  }t          j        dt          j        |           z            }|t          j        |          z  |t          j        |          z  fS )zBox-Muller transformgHz>g-DT!@g       )r   maximumr   sqrtlogcossin)rP   rQ   thrs       r#   pair_uniform_to_normalr]      s`     
FB		B	R	B	$"%&&Atx||Q"---r%   c                     t          | ||          \  }}}}t          |          }t          |          }t          ||          \  }}|S )a  
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns a block of random :code:`float32` in :math:`\mathcal{N}(0, 1)`.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )r8   rF   r]   )	r2   r9   r   rL   rM   r   rP   rQ   n1s	            r#   randnr`      sO     T6844LBAq	r	"	"B	r	"	"B"2r**EBIr%   c                     t          | ||          \  }}}}t          ||          \  }}t          ||          \  }	}
|||	|
fS )a	  
    Given a :code:`seed` scalar and an :code:`offset` block,
    returns 4 blocks of random :code:`float32` in :math:`\mathcal{N}(0, 1)`.

    :param seed: The seed for generating random numbers.
    :param offsets: The offsets to generate random numbers for.
    )rT   r]   )r2   r9   r   rP   rQ   rR   rS   r_   n2n3n4s              r#   randn4xre      sQ     D&(33NBB#B++FB#B++FBr2r>r%   N)runtime.jitr    r   r   r   N_ROUNDS_DEFAULTr/   r$   r6   r;   r8   rF   rI   rT   r]   r`   re    r%   r#   <module>rj      s                      AQ  ",    @ :J C C2< C C C C4 3C  BL     5E @ @bl @ @ @ @J   & 0@ 	) 	) 	) 	) 	) 	) 3C  BL    * . . . 1A  ",     3C  BL      r%   