LFSR calcs, passing params.

N

Niv (KP)

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

if (lfsr == 32768)
{
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;
xorbit = ( (temp>>15) ^ (temp>>7) ^ (temp>>6) ^ (temp>>4) ^
0x01u ) & 0x01u;
lfsr = (lfsr << 1) | xorbit;
}
return 0;
}
 
F

Fred

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>
 
B

Ben Bacarisse

Niv (KP) said:
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.

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 == 32768)
{
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.
 
B

Barry Schwarz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top