DSP_urand32.h

Go to the documentation of this file.
00001 /* ======================================================================= */
00002 /* DSP_urand32.h -- Optimized C source interface header file               */
00003 /*                                                                         */
00004 /* Rev 0.0.1                                                               */
00005 /*                                                                         */
00006 /* Copyright (c) 2011 Mutsuo Saito, Makoto Matsumoto, Hiroshima            */
00007 /* University and The University of Tokyo. All rights reserved.            */
00008 /*                                                                         */
00009 /* Copyright (C) 2013 Texas Instruments Incorporated - https://www.ti.com/  */
00010 /*                                                                         */
00011 /*                                                                         */
00012 /*  Redistribution and use in source and binary forms, with or without     */
00013 /*  modification, are permitted provided that the following conditions     */
00014 /*  are met:                                                               */
00015 /*                                                                         */
00016 /*    Redistributions of source code must retain the above copyright       */
00017 /*    notice, this list of conditions and the following disclaimer.        */
00018 /*                                                                         */
00019 /*    Redistributions in binary form must reproduce the above copyright    */
00020 /*    notice, this list of conditions and the following disclaimer in the  */
00021 /*    documentation and/or other materials provided with the               */
00022 /*    distribution.                                                        */
00023 /*                                                                         */
00024 /*    Neither the name of Texas Instruments Incorporated nor the names of  */
00025 /*    its contributors may be used to endorse or promote products derived  */
00026 /*    from this software without specific prior written permission.        */
00027 /*                                                                         */
00028 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    */
00029 /*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      */
00030 /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  */
00031 /*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   */
00032 /*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  */
00033 /*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       */
00034 /*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  */
00035 /*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  */
00036 /*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    */
00037 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  */
00038 /*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   */
00039 /*                                                                         */
00040 /* ======================================================================= */
00041 
00042 #ifndef DSP_URAND32_H_
00043 #define DSP_URAND32_H_
00044 
00045 
00046 #ifndef __TI_COMPILER_VERSION__           // for non TI compiler
00047 #include "assert.h"                       // intrinsics prototypes
00048 #include "C6xSimulator.h"                 // intrinsics prototypes
00049 #include "C6xSimulator_type_modifiers.h"  // define/undefine typing keywords
00050 #endif
00051 
00052 
00053 /*===========================================================================*/
00054 /* External Function Prototypes and Documentation                            */
00055 /*===========================================================================*/
00056 
00058 /* @{ */
00059 
00062 /* @{ */
00063 
00105 void DSP_urand32_init(
00106     unsigned int seed,        /*Arbitrary number used for initialization     */
00107     void* restrict polynomial,/*Array of length 3 containing polynomial data */
00108     void* restrict state);    /*Array of length 7 allocated and uninitialized*/
00109 
00110 
00111 
00112 
00115 /* @{ */
00116 
00146 /* }@ */ /* ingroup */
00147 /* }@ */ /* ingroup */
00148 
00149 void DSP_urand32(unsigned int size,  /*   Size of the vector to be filled*/
00150                  void* vector,       /* Pointer to the destination vector*/
00151                  void* state);       /*   State vector maintained by user*/
00152 
00153 /*===========================================================================*/
00154 /* Internal Code                                                             */
00155 /*===========================================================================*/
00156 
00157 /* All code below this disclaimer is for internal DSPLIB use only and should
00158  * not be used by the user.
00159  */
00160 
00161 /*================================ Typedefs ================================*/
00162 
00163 /* Internal Portable 32-bit type */
00164 #ifndef TI_UINT32_TYPE_
00165 #define TI_UINT32_TYPE_
00166 
00167 typedef unsigned int _ti_uint32;
00168 typedef int _ti_int32;
00169 
00170 #endif /* TI_UINT32_TYPE_*/
00171 
00172 /*================================ Constants ===============================*/
00173 /*State*/
00174 #define DSP_urand32_STATUS_0 (0)
00175 #define DSP_urand32_STATUS_1 (1)
00176 #define DSP_urand32_STATUS_2 (2)
00177 #define DSP_urand32_STATUS_3 (3)
00178 
00179 /*Polynomial*/
00180 #define DSP_urand32_MAT_1 (4)
00181 #define DSP_urand32_MAT_2 (5)
00182 #define DSP_urand32_TMAT  (6)
00183 
00184 #define DSP_urand32_TINYMT_MASK  (0x7ffffffful)
00185 #define DSPF_sp_urand_UINT_TO_FLOAT 2.3283063e-010
00186 
00187 /*================================ Macros ==================================*/
00188 
00189 
00190 #define DSP_urand32_SAVE_STATE(s0,s1,s2,s3)                          \
00191            {v_state[DSP_urand32_STATUS_0] = (s0);                    \
00192             v_state[DSP_urand32_STATUS_1] = (s1);                    \
00193             v_state[DSP_urand32_STATUS_2] = (s2);                    \
00194             v_state[DSP_urand32_STATUS_3] = (s3);}
00195 
00196 
00197 /*================================ Functions ===============================*/
00198 
00199 /* Function Name: DSP_urand32_get_ti_uint32
00200  *
00201  *  @Purpose: This function is used internally to generate a random number
00202  *  based on the current state It should not be called by the user. It
00203  *  basically concatenates the 4 status variables into one @b _ti_uint32.
00204  *  It should not be called by the user.
00205  *
00206  *  @Precondition: The calling function has passed valid state variable
00207  *  pointers and polynomial constants and "DSP_urand32_next_state()"
00208  *  was previously called
00209  *
00210  *  @Postcondition: A random integer based on the current state of the RNG has
00211  *  been returned
00212  *
00213  *  @param s0 : Pointer to the STATUS_0 variable of type @b _ti_uint32
00214  *  @param s2 : Pointer to the STATUS_2 variable of type @b _ti_uint32
00215  *  @param s3 : Pointer to the STATUS_3 variable of type @b _ti_uint32
00216  *
00217  *  @param tmat : constant polynomial characteristic number TMAT
00218  *   of type @b _ti_uint32
00219  *
00220  *  @returns A uniformly distributed random @b _ti_uint32.
00221  *
00222  */
00223 #pragma CODE_SECTION(DSP_urand32_get_ti_uint32,".text:optimized");
00224 #pragma FUNC_ALWAYS_INLINE(DSP_urand32_get_ti_uint32)
00225 static inline _ti_uint32 DSP_urand32_get_ti_uint32(
00226     _ti_uint32 s0,          /*Status_0*/
00227     _ti_uint32 s2,          /*Status_2*/
00228     _ti_uint32 s3,          /*Status_3*/
00229     const _ti_uint32 tmat)  /*Polynomial constant tmat*/
00230 {
00231   /* Temporary variables */
00232   _ti_uint32 j, k;
00233 
00234   /* Compute next random integer from the state */
00235   j = s3;
00236   k = s0 + (s2 >> 8);
00237   j ^= k;
00238   j ^= -((_ti_int32) (k & 1)) & tmat;
00239   return j;
00240 } /* DSP_urand32_get_ti_uint32 */
00241 
00242 
00243 
00244 /* Function Name : DSP_urand32_next_state
00245  *
00246  *  @Purpose : This function is used internally to modify the state so that
00247  *  the next random number may be generated. It should not be called by the
00248  *  user. This implementation is taken verbatim from the original algorithm.
00249  *
00250  *  @Precondition : The calling function has passed valid state variable
00251  *  pointers and polynomial constants.
00252  *
00253  *  @Postcondition : The "state machine" of the RNG has been modified
00254  *  so that the next random transition can occur.
00255  *
00256  *  @param s0 : Pointer to the STATUS_0 variable of type @b _ti_uint32
00257  *  @param s1 : Pointer to the STATUS_1 variable of type @b _ti_uint32
00258  *  @param s2 : Pointer to the STATUS_2 variable of type @b _ti_uint32
00259  *  @param s3 : Pointer to the STATUS_3 variable of type @b _ti_uint32
00260  *
00261  *  @param mat1 : constant polynomial characteristic number MAT_1
00262  *   of type @b _ti_uint32
00263  *
00264  *  @param mat2 : constant polynomial characteristic number MAT_2
00265  *   of type @b _ti_uint32
00266  *
00267  */
00268 #pragma CODE_SECTION(DSP_urand32_next_state,".text:optimized");
00269 #pragma FUNC_ALWAYS_INLINE(DSP_urand32_next_state)
00270 static inline void DSP_urand32_next_state(
00271     _ti_uint32 *s0,         /*Status_0*/
00272     _ti_uint32 *s1,         /*Status_1*/
00273     _ti_uint32 *s2,         /*Status_2*/
00274     _ti_uint32 *s3,         /*Status_3*/
00275     const _ti_uint32 mat1,  /*Polynomial constant mat1*/
00276     const _ti_uint32 mat2)  /*Polynomial constant mat2*/
00277 {
00278   /* Compute Next State*/
00279 
00280   /* Temporary variables */
00281     _ti_uint32 temp;
00282 
00283     temp = (*s0 & DSP_urand32_TINYMT_MASK) ^ *s1 ^ *s2;
00284     temp ^= (temp << 1);
00285     *s3 ^= (*s3 >> 1) ^ temp;
00286     *s0 = *s1;
00287     *s1 = *s2;
00288     *s2 = temp ^ (*s3 << 10);
00289     *s1 ^= -((_ti_int32) (*s3 & 1)) & mat1;
00290     *s2 ^= -((_ti_int32) (*s3 & 1)) & mat2;
00291 } /* DSP_urand32_next_state */
00292 
00293 /* Function Name : DSP_urand32_generate_random_ti_uint32
00294  *
00295  *  @Purpose : This function is used to generate and return a single
00296  *   pseudo-random @b _ti_uint32 number on the interval [0,2^32-1].
00297  *   It should not be called by the user.
00298  *
00299  *  @Precondition : The calling function has passed valid state variable
00300  *  pointers and polynomial constants.
00301  *
00302  *  @Postcondition : The "state machine" of the RNG has been modified
00303  *  so that the next random transition can occur and a pseudo-random
00304  *  @b _ti_uint32 has been returned.
00305  *
00306  *  @param s0 : Pointer to the STATUS_0 variable of type @b _ti_uint32
00307  *  @param s1 : Pointer to the STATUS_1 variable of type @b _ti_uint32
00308  *  @param s2 : Pointer to the STATUS_2 variable of type @b _ti_uint32
00309  *  @param s3 : Pointer to the STATUS_3 variable of type @b _ti_uint32
00310  *
00311  *  @param mat1 : constant polynomial characteristic number MAT_1
00312  *   of type @b _ti_uint32
00313  *
00314  *  @param mat2 : constant polynomial characteristic number MAT_2
00315  *   of type @b _ti_uint32
00316  *
00317  *  @param tmat : constant polynomial characteristic number TMAT
00318  *   of type @b _ti_uint32
00319  *
00320  *  @returns : pseudo-random @b _ti_uint32
00321  */
00322 
00323 #pragma CODE_SECTION(DSP_urand32_generate_random_ti_uint32,".text:optimized");
00324 #pragma FUNC_ALWAYS_INLINE(DSP_urand32_generate_random_ti_uint32);
00325 static inline _ti_uint32 DSP_urand32_generate_random_ti_uint32(
00326        _ti_uint32 *s0, /* STATUS 0 */
00327        _ti_uint32 *s1, /* STATUS 1 */
00328        _ti_uint32 *s2, /* STATUS 2 */
00329        _ti_uint32 *s3, /* STATUS 3 */
00330        const _ti_uint32 mat1,/* Polynomial Constant MAT1 */
00331        const _ti_uint32 mat2,/* Polynomial Constant MAT2 */
00332        const _ti_uint32 tmat)/* Polynomial Constant TMAT */
00333 {
00334   /*Hash the state vector*/
00335   DSP_urand32_next_state(s0, s1, s2, s3, mat1, mat2);
00336 
00337   /*Concatenate random number from state variables */
00338   return DSP_urand32_get_ti_uint32(*s0, *s2, *s3, tmat);
00339 
00340 } /* DSP_urand32_generate_random_ti_uint32 */
00341 
00342 #endif /* DSP_URAND32_H_ */
00343 
00344 /* ======================================================================== */
00345 /*  End of file:  DSP_urand32.h                                             */
00346 /* ------------------------------------------------------------------------ */
00347 /*            Copyright (c) 2013 Texas Instruments, Incorporated.           */
00348 /*                           All Rights Reserved.                           */
00349 /* ======================================================================== */

Copyright 2014, Texas Instruments Incorporated