-3

I need to take the following code and replace the calls using mpz_ with my own code.

void Product32(void *a, void *b, void *c, unsigned int wa,
unsigned int ba, unsigned int wb, unsigned int bb, unsigned int
*wc, unsigned int *bc){

mpz_t x,y,z;
mpz_init(x);
mpz_init(y);
mpz_init(z); 

/* Cast a and b into short integers of size 32 bits */
unsigned int *int_a = (unsigned int *) a;
unsigned int *int_b = (unsigned int *) b;

/* Now int_a can be view as an array of words of size 32
 * bits */
/* Similarly for int_b */
//printf("%lu %lu \n", int_a[0], int_a[*sa - 1]);
//printf("%lu %lu \n", int_b[0], int_b[*sb - 1]);

mpz_import(x, wa, ORDER, WORDBYTES, ENDIAN, NAILS, a);
mpz_import(y, wb, ORDER, WORDBYTES, ENDIAN, NAILS, b);
mpz_mul(z,x,y);

c = mpz_export(c, wc, ORDER, WORDBYTES, ENDIAN, NAILS, z);
}

The problem I am having is that I do not understand what mpz_import or mpz_export accomplish, and my search for an answer to that question has come up empty.

I also feel like my types are completely wrong.

I have left off the main function that calls Product32 because I know the problem isn't there; the above code works, the below code does not.

This is what I have:

/* Since we are working with string of potentially different lengths, 
first we need to be able to make the two strings of equal length. */

int makeEqualLength(int arr1[], int arr2[])
{
int len1 = sizeof(arr1);
int len2 = sizeof(arr2);
int i;
if (len1 < len2)
{
for (i = 0 ; i < len2 - len1 ; i++)
    arr1[i] = arr1[i+1];
    arr1[0] = 0;
return len2;
}
else if (len1 > len2)
{
for (i = 0 ; i < len1 - len2 ; i++)
    arr2[i] = arr2[i+1];
    arr2[0] = 0;
}
return len1; // If len1 >= len2
}


void Product32(void *a, void *b, void *c, unsigned int wa,
unsigned int ba, unsigned int wb, unsigned int bb, unsigned int
*wc, unsigned int *bc){

/* Cast a and b into short integers of size 32 bits */
unsigned int *int_a = (unsigned int *) a;
unsigned int *int_b = (unsigned int *) b;
unsigned int *int_c = (unsigned int *) c;    

/* Now int_a can be view as an array of words of size 32
 * bits */
/* Similarly for int_b */
//printf("%lu %lu \n", int_a[0], int_a[*sa - 1]);
//printf("%lu %lu \n", int_b[0], int_b[*sb - 1]);

int n = makeEqualLength(int_a, int_b);
unsigned int i,j,k;
double p;

for (k = 0; k < n; i++){
int_c[k] = 0;
}
for (i = n - 1; i >= 0; i--){
double d = 0;
for (j = n - 1; j >= 0; j--){
    p = (int_a[i]) * (int_b[j]) + int_c[i + j] + d;
    int_c[i + j] = p % 32;
    int_c = p/32
    }
int_c[i + n] = d;
}
}
Chris
  • 940
  • 10
  • 24
sydwys8
  • 1
  • 1
  • Why don't you declare `a` etc as `unsigned int*` in the function argument, seeing as the function knows what it is? – Weather Vane Dec 04 '14 at 21:34
  • I assume you mean change the declaration as void to unsigned int*? I cannot change the call to Product32 in the main function. In fact I am not allowed to change the main function at all. Therefore, I cannot change the function arguments. – sydwys8 Dec 04 '14 at 21:48
  • Why not? Is the caller using arguments that are not `unsigned int *`? – Weather Vane Dec 04 '14 at 21:55

1 Answers1

1

What you're looking for is the The GNU Multiple Precision Arithmetic Library documentation. You'll find definitions of the mpz_* functions there.

Specifically, mpz_import and mpz_export. What they accomplish (converting mpz_t variables to and from arbitrary words of binary data) is fully described there and it should help you out.

Chris
  • 940
  • 10
  • 24
  • Why vote this down? I specifically answered "the problem I'm having" from the question. – Chris Dec 04 '14 at 21:40
  • 1
    I didn't downvote, but generally when you link to the answer on a 3rd party website, it is good to include a summary of said answer. This can ameliorate problems with dead links in the future because a reader can still get the gist even if the link you provide is no longer active. – Nicu Stiurca Dec 04 '14 at 21:54
  • Thanks @SchighSchagh - that's a good point. I've expanded my answer somewhat. Unfortunately it's hard to summarize concise GNU documentation without simply re-posting it. :) – Chris Dec 04 '14 at 22:05
  • How do I import or export non-mpz variables? – sydwys8 Dec 05 '14 at 03:28
  • You import an array of data into an mpz variable, and you export an mpz variable back out into an array. The example shown on the mpz_import page shows what to do if your number is so large that you need twenty long integers to hold the value. The documentation also lists some example programs that you might find useful. – Chris Dec 05 '14 at 05:38