
    xKg(                        d dl 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Z ej                  dej                  fdej                  fgd      Z e	e      Zej                   xZZej                    Z e
eee      d        Z e
eee      d	        Z e
eee      d
        Z e
eee      d        Z e
eee      d        Z e
eee      d        Z e
eee      d        Z e
eee      d        Z ej                  de j8                  z        Z ej
                  de j8                  z        Z e
eee      d        Z e
eee      d        Z  e
eee      d        Z!ddZ"ddZ#y)    N)	configcudafloat32float64uint32int64uint64
from_dtypejits0s1T)align)forceobjloopliftnopythonc                    t        |      }t        |      }|t        d      z   }||t        d      z	  z  t        d      z  }||t        d      z	  z  t        d      z  }||t        d      z	  z  }|| |   d<   || |   d<   y	)
a  Use SplitMix64 to generate an xoroshiro128p state from 64-bit seed.

    This ensures that manually set small seeds don't result in a predictable
    initial sequence from the random number generator.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: uint64
    :param index: offset in states to update
    :type seed: int64
    :param seed: seed value to use when initializing state
    l   |~fq	    l   e9z    l   b&&&	    r   r   N)r   r	   r   )statesindexseedzs       U/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/numba/cuda/random.pyinit_xoroshiro128p_stater   .   s     %LE$<Dv())A	
a6":o	&);"<<A	
a6":o	&);"<<A	Q&*_AF5M$F5M$    c                 \    t        |       } t        |      }| |z  | t        d|z
        z	  z  S )zLeft rotate x by k bits.@   )r	   r   )xks     r   rotlr!   H   s2     	q	Aq	AFqF26N*++r   c                    t        |      }| |   d   }| |   d   }||z   }||z  }t        t        |t        d                  |z  |t        d      z  z  | |   d<   t        t        |t        d                  | |   d<   |S )zReturn the next random uint64 and advance the RNG in states[index].

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    :rtype: uint64
    r   r   7      $   )r   r	   r!   r   )r   r   r   r   results        r   xoroshiro128p_nextr'   P   s     %LE	t	B	t	B"WF"HB b&*!56;rVBZ?OPF5M$ b&*!56F5M$Mr   c                 P   t        |      }t        d      t        d      f}t        d      }t        d      }t        d      D ]Q  }t        d      D ]A  }||   t        d      t        |      z  z  r|| |   d   z  }|| |   d   z  }t	        | |       C S || |   d<   || |   d<   y	)
zAdvance the RNG in ``states[index]`` by 2**64 steps.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    l   zKW`u l   "U.XC r      r      r   r   N)r   r	   ranger   r'   )r   r   jumpr   r   ibs          r   xoroshiro128p_jumpr/   f   s     %LE%&/A(BCD	B	B1XrAAw&)vay01fUmD))fUmD))vu-	   F5M$F5M$r   c                 |    t        |       } | t        d      z	  t        d      t        d      t        d      z  z  z  S )z7Convert uint64 to float64 value in the range [0.0, 1.0)   r*   5   )r	   r   r   r   s    r   uint64_to_unit_float64r4      s6     	q	AO
fQi6":.E FGGr   c                 @    t        |       } t        t        |             S )z7Convert uint64 to float32 value in the range [0.0, 1.0))r	   r   r4   r3   s    r   uint64_to_unit_float32r6      s     	q	A)!,--r   c                 B    t        |      }t        t        | |            S )zReturn a float32 in range [0.0, 1.0) and advance ``states[index]``.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    :rtype: float32
    )r   r6   r'   r   r   s     r   xoroshiro128p_uniform_float32r9            %LE!"4VU"CDDr   c                 B    t        |      }t        t        | |            S )zReturn a float64 in range [0.0, 1.0) and advance ``states[index]``.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    :rtype: float64
    )r   r4   r'   r8   s     r   xoroshiro128p_uniform_float64r<      r:   r   r)   c                     t        |      }t        | |      }t        | |      }t        j                  t	        d       t        j
                  |      z        t        j                  t        |z        z  }|S )a  Return a normally distributed float32 and advance ``states[index]``.

    The return value is drawn from a Gaussian of mean=0 and sigma=1 using the
    Box-Muller transform.  This advances the RNG sequence by two steps.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    :rtype: float32
           @)r   r9   mathsqrtr   logcosTWO_PI_FLOAT32r   r   u1u2z0s        r   xoroshiro128p_normal_float32rH      `     %LE	&vu	5B	&vu	5B	GCL=488B</	0488NR<O3P	PB Ir   c                     t        |      }t        | |      }t        | |      }t        j                  t	        d       t        j
                  |      z        t        j                  t        |z        z  }|S )a  Return a normally distributed float32 and advance ``states[index]``.

    The return value is drawn from a Gaussian of mean=0 and sigma=1 using the
    Box-Muller transform.  This advances the RNG sequence by two steps.

    :type states: 1D array, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type index: int64
    :param index: offset in states to update
    :rtype: float64
    r>   )r   r9   r?   r@   r   rA   rB   TWO_PI_FLOAT64rD   s        r   xoroshiro128p_normal_float64rL      rI   r   c                     | j                   d   }t        |      }t        |      }|dk\  rRt        | d|       t        |      D ]  }t	        | d        t        d|      D ]  }| |dz
     | |<   t	        | |        y y )Nr   r*   )shaper	   r   r+   r/   )r   r   subsequence_startn_r-   s         r   init_xoroshiro128p_states_cpurR      s    QA$<D01Av D1 ()Avq) * q!Aq1uF1Ivq)  r   c                     t        j                  | j                  t              }t	        |||       | j                  ||       y)a  Initialize RNG states on the GPU for parallel generators.

    This initializes the RNG states so that each state in the array corresponds
    subsequences in the separated by 2**64 steps from each other in the main
    sequence.  Therefore, as long no CUDA thread requests more than 2**64
    random numbers, all of the RNG states produced by this function are
    guaranteed to be independent.

    The subsequence_start parameter can be used to advance the first RNG state
    by a multiple of 2**64 steps.

    :type states: 1D DeviceNDArray, dtype=xoroshiro128p_dtype
    :param states: array of RNG states
    :type seed: uint64
    :param seed: starting seed for list of generators
    )rN   dtype)streamN)npemptyrN   xoroshiro128p_dtyperR   copy_to_device)r   r   rO   rU   
states_cpus        r   init_xoroshiro128p_statesr[      s9    & 4GHJ!*d4EF
*V4r   c                 Z    t        j                  | t        |      }t        ||||       |S )a5  Returns a new device array initialized for n random number generators.

    This initializes the RNG states so that each state in the array corresponds
    subsequences in the separated by 2**64 steps from each other in the main
    sequence.  Therefore, as long no CUDA thread requests more than 2**64
    random numbers, all of the RNG states produced by this function are
    guaranteed to be independent.

    The subsequence_start parameter can be used to advance the first RNG state
    by a multiple of 2**64 steps.

    :type n: int
    :param n: number of RNG states to create
    :type seed: uint64
    :param seed: starting seed for list of generators
    :type subsequence_start: uint64
    :param subsequence_start:
    :type stream: CUDA stream
    :param stream: stream to run initialization kernel on
    )rT   rU   )r   device_arrayrX   r[   )rP   r   rO   rU   r   s        r   create_xoroshiro128p_statesr^     s-    * q(;FKFfd,=vFMr   )r   r   )$r?   numbar   r   r   r   r   r   r	   r
   r   numpyrV   rT   rX   xoroshiro128p_typeENABLE_CUDASIM	_forceobj	_looplift	_nopythonr   r!   r'   r/   r4   r6   r9   r<   pirC   rK   rH   rL   rR   r[   r^    r   r   <module>rh      s   $ $ $ 4 bhhryy 1D"))3DE%)+  34  -- -	I%%%	 i)i@ A2 i)i@, A, i)i@ A* i)i@ A4 i)i@H AH i)i@. A. i)i@
E A
E i)i@
E A
E AK(AK( i)i@ A0 i)i@ A0 i)i@* A*$52r   