/* * C/Matlab/mex replacement for FSM_inc.m * * FSM_inc( x, n) - increment x, base n * where x is an index row vector containing values in the range 1..n * * Note that unlike FSM_inc.m, this routine increments x directly * and does not return a separate vector result. * * A test using isi_t/get_ML.m with N=13 shows that use of FSM_inc_C.c * decreases the runtime by about 7% compared to FSM_inc.m * * R. Perry, 9 June 2000 */ #include "mex.h" /* for Matlab/mex stuff */ static int MN; /* result size */ static double *x; /* arguments */ static double n; static void FSM_inc( void) { double *px = x + MN; double s, c = 1.0; while( px > x) { --px; s = c + *px; if( s > n) { c = 1.0; *px = 1.0; } else { *px = s; break; } } } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* get rhs */ if( nrhs != 2) mexErrMsgTxt("two arguments required"); x = mxGetPr( prhs[0]); n = mxGetScalar( prhs[1]); MN = mxGetM(prhs[0]) * mxGetN(prhs[0]); /* doit */ FSM_inc(); }