How do I convert an array of UInt64 to an array of UInt16 to perform multi-precision multiplication?

417 Views Asked by At

I need to perform fast Galois field arithmetic in my application. I have a multiplication function written in assembly that has been optimized for my platform, an MSP430 microcontroller. That function computes the product of two large numbers of arbitrary size, but each number must be represented as an array of 16-bit integers. However, in my project, a Galois field element is represented as an array of 16 64-bit integers. How do I convert my array of 16 64-bit integers into a representation needed by my optimized, assembly-based multiplication function (i.e. an array of 64 16-bit integers)? Of course, simply casting the array as a (UInt16 *) does not work.

The MSP430 is a little-endian architecture. Thanks in advance for any suggestions.

2

There are 2 best solutions below

0
Keron On BEST ANSWER

As mentioned by @JohnBollinger I was able to simply reinterpret the bytes of the array of uint64_t as an array of uint16_t by casting. For some reason, I was thinking the bytes had to be reordered somehow, but after testing I am getting the correct results. This didn't work for me initially because of other unrelated issues.

0
Koorosh Hajiani On

I'm not certain if this is what you want and this solution is incomplete in the sense that it just serves as an example. Additionally it is highly platform dependable. It works on my machine (little_endian). I'm using Code:Blocks under windows.

   typedef struct {
             uint16_t lo_word0;
             uint16_t hi_word0;
             uint16_t lo_word1;
             uint16_t hi_word1;
              }struct_t;



    int main()
    {
       uint64_t buff_64[4]={0xaaaabbbbccccdddd,0xbbbbccccddddeeee,0x1111222233334444,0x8888aaaabbbbcccc};
       uint16_t buff_16[16];
      /*Please note that you may use simply:
        memcpy(buff_16,buff_64,32); 
        however that would result in reverse order
        with respect to the code below */

       struct_t *ptr = (struct_t *)buff_64;

       for(int j=0; j<16; ptr++)
       {
         buff_16[(j++)%16]=ptr->hi_word1;
         buff_16[(j++)%16]=ptr->lo_word1;
         buff_16[(j++)%16]=ptr->hi_word0;
         buff_16[(j++)%16]=ptr->lo_word0;

       } 
        // The check
        for(int j=0;j<16;j++)
        printf("%x\n",buff_16[j]);  

        return 0;
      }