1 |
2 |
guangxi.li |
/*
|
2 |
|
|
* Generate Combined Tausworthe number
|
3 |
|
|
*
|
4 |
|
|
* The calling syntax is:
|
5 |
|
|
* x = ctg_gen(z, n)
|
6 |
|
|
* [x, zf] = ctg_gen(z, n)
|
7 |
|
|
*
|
8 |
|
|
* Input:
|
9 |
|
|
* z: initial internal state, 3 x 1 vector of unsigned 64-bit integer
|
10 |
|
|
* n: length of pseudorandom numbers
|
11 |
|
|
*
|
12 |
|
|
* Output:
|
13 |
|
|
* x: pseudorandom values, n x 1 vector of unsigned 64-bit integer
|
14 |
|
|
* zf: final internal state after output n numbers
|
15 |
|
|
*/
|
16 |
|
|
|
17 |
|
|
/*
|
18 |
|
|
* Copyright (C) 2014, Guangxi Liu <guangxi.liu@opencores.org>
|
19 |
|
|
*
|
20 |
|
|
* This source file may be used and distributed without restriction provided
|
21 |
|
|
* that this copyright statement is not removed from the file and that any
|
22 |
|
|
* derivative work contains the original copyright notice and the associated
|
23 |
|
|
* disclaimer.
|
24 |
|
|
*
|
25 |
|
|
* This source file is free software; you can redistribute it and/or modify it
|
26 |
|
|
* under the terms of the GNU Lesser General Public License as published by
|
27 |
|
|
* the Free Software Foundation; either version 2.1 of the License,
|
28 |
|
|
* or (at your option) any later version.
|
29 |
|
|
*
|
30 |
|
|
* This source is distributed in the hope that it will be useful, but
|
31 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
32 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
33 |
|
|
* License for more details.
|
34 |
|
|
*
|
35 |
|
|
* You should have received a copy of the GNU Lesser General Public License
|
36 |
|
|
* along with this source; if not, download it from
|
37 |
|
|
* http://www.opencores.org/lgpl.shtml
|
38 |
|
|
*/
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
#include "mex.h"
|
42 |
|
|
#include "taus176.h"
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
|
46 |
|
|
{
|
47 |
|
|
/* Local variables */
|
48 |
|
|
taus_state_t state;
|
49 |
|
|
unsigned long long *z;
|
50 |
|
|
double *n;
|
51 |
|
|
unsigned len;
|
52 |
|
|
unsigned long long *x;
|
53 |
|
|
unsigned long long *zf;
|
54 |
|
|
unsigned i;
|
55 |
|
|
|
56 |
|
|
/* Check for proper number of arguments */
|
57 |
|
|
if (nrhs != 2)
|
58 |
|
|
mexErrMsgTxt("Two input arguments required.");
|
59 |
|
|
else if (nlhs > 2)
|
60 |
|
|
mexErrMsgTxt("Too many output arguments.");
|
61 |
|
|
|
62 |
|
|
if (!mxIsUint64(prhs[0]) || (mxGetM(prhs[0]) != 3) || (mxGetN(prhs[0]) != 1))
|
63 |
|
|
mexErrMsgTxt("ctg_gen requires that z be a 3 x 1 vector of unsigned 64-bit integer.");
|
64 |
|
|
|
65 |
|
|
if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ||
|
66 |
|
|
(mxGetM(prhs[1]) != 1) || (mxGetN(prhs[1]) != 1))
|
67 |
|
|
mexErrMsgTxt("ctg_gen requires that n be a positive integer.");
|
68 |
|
|
|
69 |
|
|
n = mxGetPr(prhs[1]);
|
70 |
|
|
len = (unsigned)(*n);
|
71 |
|
|
if (len < 1)
|
72 |
|
|
mexErrMsgTxt("ctg_gen requires that n be a positive integer.");
|
73 |
|
|
|
74 |
|
|
/* Create a vector for the return argument */
|
75 |
|
|
plhs[0] = mxCreateNumericMatrix(len, 1, mxUINT64_CLASS, 0);
|
76 |
|
|
if (nlhs == 2)
|
77 |
|
|
plhs[1] = mxCreateNumericMatrix(3, 1, mxUINT64_CLASS, 0);
|
78 |
|
|
|
79 |
|
|
/* Assign pointers to the various parameters */
|
80 |
|
|
z = (unsigned long long*)mxGetData(prhs[0]);
|
81 |
|
|
x = (unsigned long long*)mxGetData(plhs[0]);
|
82 |
|
|
if (nlhs == 2)
|
83 |
|
|
zf = (unsigned long long*)mxGetData(plhs[1]);
|
84 |
|
|
|
85 |
|
|
/* Call function taus_set and assign return value */
|
86 |
|
|
state.z1 = z[0];
|
87 |
|
|
state.z2 = z[1];
|
88 |
|
|
state.z3 = z[2];
|
89 |
|
|
for (i = 0; i < len; i++)
|
90 |
|
|
x[i] = taus_get(&state);
|
91 |
|
|
|
92 |
|
|
if (nlhs == 2) {
|
93 |
|
|
zf[0] = state.z1;
|
94 |
|
|
zf[1] = state.z2;
|
95 |
|
|
zf[2] = state.z3;
|
96 |
|
|
}
|
97 |
|
|
}
|