Velocity Reviews > LFSR calcs, passing params.

# LFSR calcs, passing params.

Niv (KP)
Guest
Posts: n/a

 05-13-2009
I have the following code, which generates the sequence I require, but
I have several questions:
(code at end, note, this snippet only puts out a few of the values):

1. Is there a better/faster way to do the LFSR calc.
2. I need to modify the code so it is executed as part of a tcl "exec"
and the current value is passed in with the new value returned.

NOTE: This is a maximal length lfsr, starting with all '0's, and
forcing the all '1's condition, so I get 2^n values, not 2^n - 1.

main( )
{
unsigned short lfsr = 32768; // A start value to check all ones
case is forced OK.
unsigned short temp;
unsigned short xorbit = 0x0u;
unsigned short all_ones = 0;
unsigned short i;

for ( i=0; i<20; i++) // just do a few values to test for now.
{
printf("%10d %10x %10d\n", lfsr, temp, xorbit); // debug aid

if (lfsr == 3276
{
lfsr = 65535;
all_ones = 1;
}
else
if (all_ones == 1)
{
all_ones = 0;
lfsr = 32768;
}
// now do the usual lfsr calc, (a bit different as it starts all
'0's, but XOR's a '1' with the feedback taps.
xorbit = ( (temp>>15) ^ (temp>>7) ^ (temp>>6) ^ (temp>>4) ^
0x01u ) & 0x01u;
lfsr = (lfsr << 1) | xorbit;
}
return 0;
}

Fred
Guest
Posts: n/a

 05-13-2009
On May 13, 11:59*am, "Niv (KP)" <(E-Mail Removed)> wrote:
> I have the following code, which generates the sequence I require, but
> I have several questions:
> (code at end, note, this snippet only puts out a few of the values):
>
> 1. Is there a better/faster way to do the LFSR calc.
> 2. I need to modify the code so it is executed as part of a tcl "exec"
> and the current value is passed in with the new value returned.
>
> NOTE: This is a maximal length lfsr, starting with all '0's, and
> forcing the all '1's condition, so I get 2^n values, not 2^n - 1.
>
> main( )
> {
> * unsigned short lfsr * = 32768; *// A start value to check all ones
> case is forced OK.
> * unsigned short mask * = 0x80D0u;
> * unsigned short temp;
> * unsigned short xorbit = 0x0u;
> * unsigned short all_ones = 0;
> * unsigned short i;
>
> for ( i=0; i<20; i++) * // just do a few values to test for now.
> * {
> * * printf("%10d %10x %10d\n", lfsr, temp, xorbit); * // debug aid
>

You declared 'lfsr' as an unsigned short,
but you tell printf it is an int

'temp' is not initialized

You declared 'xorbit' is an unsigned short,
but you tell printf it is an int

<snip>
--
Fred K

Ben Bacarisse
Guest
Posts: n/a

 05-13-2009
"Niv (KP)" <(E-Mail Removed)> writes:

> I have the following code, which generates the sequence I require, but
> I have several questions:
> (code at end, note, this snippet only puts out a few of the values):
>
> 1. Is there a better/faster way to do the LFSR calc.

I would not worry abut faster since:

> 2. I need to modify the code so it is executed as part of a tcl "exec"
> and the current value is passed in with the new value returned.

the cost of exec will dwarf anything you do at the bit level for a
single iteration.

You need the int main(int argc, char **argv) form so you can
get the current value from argv[1]. You need to check that argc > 1
(or == 2 if you want to outlaw extra unused arguments). The function
strtoul can do the conversion.

> NOTE: This is a maximal length lfsr, starting with all '0's, and
> forcing the all '1's condition, so I get 2^n values, not 2^n - 1.

I don't follow that but you'll need to fix for the exec'd version
because the all_ones variable will never have any effect in a single
execution.

> main( )

I'd say something, but you are going to change this anyway!

> {
> unsigned short lfsr = 32768; // A start value to check all ones
> case is forced OK.
> unsigned short mask = 0x80D0u;
> unsigned short temp;
> unsigned short xorbit = 0x0u;
> unsigned short all_ones = 0;
> unsigned short i;
>
> for ( i=0; i<20; i++) // just do a few values to test for now.
> {
> printf("%10d %10x %10d\n", lfsr, temp, xorbit); // debug aid

Someone else has already said this but the formats don't match the
arguments.

> if (lfsr == 3276
> {
> lfsr = 65535;
> all_ones = 1;
> }
> else
> if (all_ones == 1)
> {
> all_ones = 0;
> lfsr = 32768;
> }
> // now do the usual lfsr calc, (a bit different as it starts all
> '0's, but XOR's a '1' with the feedback taps.
> temp = mask & lfsr;

I don't see any value in this mask operation. You'd need it if you
were going to use some bit counting trick to set xorbit but the code
below works without it.

> xorbit = ( (temp>>15) ^ (temp>>7) ^ (temp>>6) ^ (temp>>4) ^
> 0x01u ) & 0x01u;
> lfsr = (lfsr << 1) | xorbit;
> }
> return 0;
> }

--
Ben.

Barry Schwarz
Guest
Posts: n/a

 05-16-2009
On May 13, 2:23*pm, Fred <(E-Mail Removed)> wrote:
> On May 13, 11:59*am, "Niv (KP)" <(E-Mail Removed)> wrote:
>
>
>
>
>
> > I have the following code, which generates the sequence I require, but
> > I have several questions:
> > (code at end, note, this snippet only puts out a few of the values):

>
> > 1. Is there a better/faster way to do the LFSR calc.
> > 2. I need to modify the code so it is executed as part of a tcl "exec"
> > and the current value is passed in with the new value returned.

>
> > NOTE: This is a maximal length lfsr, starting with all '0's, and
> > forcing the all '1's condition, so I get 2^n values, not 2^n - 1.

>
> > main( )
> > {
> > * unsigned short lfsr * = 32768; *// A start value to check all ones
> > case is forced OK.
> > * unsigned short mask * = 0x80D0u;
> > * unsigned short temp;
> > * unsigned short xorbit = 0x0u;
> > * unsigned short all_ones = 0;
> > * unsigned short i;

>
> > for ( i=0; i<20; i++) * // just do a few values to test for now.
> > * {
> > * * printf("%10d %10x %10d\n", lfsr, temp, xorbit); * // debug aid

>
> You declared 'lfsr' as an unsigned short,
> but you tell printf it is an int
>
> 'temp' is not initialized
>
> You declared 'xorbit' is an unsigned short,
> but you tell printf it is an int

xorbit will be promoted to either int or unsigned int (system
specific) because printf is a variadic function. The will occur for
all integers with lower rank than int. Floats will also be promoted
to double.

>
> <snip>
> --
> Fred K- Hide quoted text -
>
> - Show quoted text -