我需要使用以下代码并使用我自己的代码使用mpz_替换调用。
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);
}
我遇到的问题是我不明白mpz_import或mpz_export完成了什么,而我对该问题的回答是空的。
我也觉得我的类型是完全错误的。
我没有关闭调用Product32的主函数,因为我知道问题不在那里;上面的代码有效,下面的代码没有。
这就是我所拥有的:
/* 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;
}
}
答案 0 :(得分:1)
您正在寻找的是The GNU Multiple Precision Arithmetic Library documentation。你会在那里找到mpz_*
函数的定义。
具体而言,mpz_import
和mpz_export
。他们完成的任务(将mpz_t
变量转换为二进制数据的任意单词)在那里得到了充分的描述,它可以帮助你。