2

Lets say I have: (data) mod (polynomial)

1110 0101 mod 1001

I understand that I will need to shift the polynomial to the left most bit of the data and execute a XOR operation.

1110 0101
1001

and i will get a result of

0111 0101

Then I will need the set the polynomial to find the next '1' on the result and move the polynomial to the position and perform the next XOR operation, and repeat the steps until I get the remainder.

So, I understand that I will need to copy my data to an array and using the array I can do a shifting and use a AND operator and compare the first bit of the data with the first bit of the polynomial, if I get a result of '1' and I will then know that I can shift the polynomial to that position.

Here's a snippet of my code:

   uint8_t polyarray[4];
   uint32_t dataarray[32];

   uint64_t mod(int data, int poly, int i) {

        memcpy(polyarray, (int[]) {1}, sizeof polyarray);
        memcpy(dataarray, (int[]) {1,2,3,4}, sizeof dataarray);

        for (i=127; i>=0; i--){
            poly << i;
            dataarray[4]>>31;
            polyarray[1]>>3;
            if(dataarray[4] & polyarray[1]=1){
                data = data ^ poly;
            }
        }

I am quite certain that my codes are incomplete but I am not sure where, anyone can help me?

i redo my codes again, will this be better?

    void mod(uint8_t i, uint64_t *pPoly, uint64_t *pData)
{
        uint64_t Data[128];
        uint64_t Poly[4];

    for(i=127; i>=0; i--)
    {
            Poly << i;
            pData = &Data[i];
            pPoly = &Poly[3];
                if (pData = 1)
                {
                    Data = Data^Poly;
                }
                else
                {
                    Poly>>1;
                    i--;
                }
    }
}
Anne
  • 123
  • 8

1 Answers1

0
  1. You said 128 bit, but you have 128*64 bit.
  2. Poly << i; shifts poly without effect; You have to store the result.
  3. Data = Data^Poly; XORs two pointers to arrays instead of bits in the bit array
  4. pData = &Data[i]; and pPoly = &Poly[3]; are unused
  5. if( pData = 1 ) have missing "="

Here is my code

int main()
{
    uint64_t hi = 0;
    uint64_t lo = 0b11100101;
    uint64_t div = 0b1001;

    uint64_t mask = 1L << 63;
    uint8_t offset = 0;

    /* search te MSB */
    for (int i = 0; i < 64; i++)
    {
        if ( (mask >> i) & div )
        {
            offset = i;
            break;
        }

        /* 
        before:
            div = 00001001
            mask= 10000000

        after:
                  |---|
            mask= 00001000
            off = 4    
        */
    }

    /* 00001001 */
    div <<= offset;
    /* 10010000 */

    for(int i = 0; i < 64; i++)
    {
        /* XOR if MSB of HI is 1 */
        if ( hi & (mask >> i) )
        {
            hi ^= (div >> i);

        /* if we have to xor in HI and LO together, cause by overlap */
        /* HI           LO                                           */
        /* 000000000000 000000001001                                 */
        /*   DIV 000100 00000                                        */
            if ( i > 0 )
            { 
                lo ^= (div << (64 - i));
            }           
        }
    }

    for(int i = 0; i <= offset; i++)
    {
        if ( lo & (mask >> i) )
        {
            lo ^= (div >> i);
        }
    }

    printf("mod = %" PRIX64 "%" PRIX64 "\n", hi, lo); 
}

The point is that, you have to properly move mask and divisor and xor it with dividend. The modulo is stored in last offset bytes in lo.

Probably exists some 128 bit instruction which can do all work for you in single step.

sgflt
  • 68
  • 3
  • 10