gpt4 book ai didi

c - 通过改变起始代码来实现c中的基本乘法

转载 作者:行者123 更新时间:2023-11-30 21:02:57 24 4
gpt4 key购买 nike

我需要使用以下代码并将使用 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;
}
}

最佳答案

您正在寻找的是 The GNU Multiple Precision Arithmetic Library documentation 。您将在那里找到 mpz_* 函数的定义。

具体来说,mpz_importmpz_export 。它们所完成的工作(将 mpz_t 变量与二进制数据的任意字相互转换)在此处进行了完整描述,它应该可以帮助您。

关于c - 通过改变起始代码来实现c中的基本乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com