LFSR calcs, passing params.

Discussion in 'C Programming' started by Niv (KP), May 13, 2009.

  1. Niv (KP)

    Niv (KP) Guest

    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;
    }
    Niv (KP), May 13, 2009
    #1
    1. Advertising

  2. Niv (KP)

    Fred Guest

    On May 13, 11:59 am, "Niv (KP)" <> 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
    Fred, May 13, 2009
    #2
    1. Advertising

  3. "Niv (KP)" <> 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 == 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.

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


    --
    Ben.
    Ben Bacarisse, May 14, 2009
    #3
  4. On May 13, 2:23 pm, Fred <> wrote:
    > On May 13, 11:59 am, "Niv (KP)" <> 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 -
    Barry Schwarz, May 16, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spartan815

    LFSR

    Spartan815, Jan 28, 2004, in forum: VHDL
    Replies:
    10
    Views:
    8,052
    VhdlCohen
    Jan 29, 2004
  2. Niv

    cosine calcs

    Niv, Aug 11, 2006, in forum: VHDL
    Replies:
    9
    Views:
    2,092
    Jonathan Bromley
    Aug 16, 2006
  3. Grant Edwards

    Single precision floating point calcs?

    Grant Edwards, May 9, 2007, in forum: Python
    Replies:
    8
    Views:
    532
    Beliavsky
    May 10, 2007
  4. George

    Field calcs with OnBlur

    George, Jul 20, 2004, in forum: ASP General
    Replies:
    2
    Views:
    117
    Rob Collyer
    Jul 20, 2004
  5. Barry
    Replies:
    9
    Views:
    444
    Ara.T.Howard
    Sep 15, 2005
Loading...

Share This Page