我在C中尝试两个乘以矩阵,我无法理解为什么会得到这些结果......
我想这样做: Btranspose * B
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define LOW_WORD(x) (((x) << 16) >> 16)
#define HIGH_WORD(x) ((x) >> 16)
#define ABS(x) (((x) >= 0) ? (x) : -(x))
#define SIGN(x) (((x) >= 0) ? 1 : -1)
#define UNSIGNED_MULT(a, b) \
(((LOW_WORD(a) * LOW_WORD(b)) << 0) + \
(((int64_t)((LOW_WORD((a)) * HIGH_WORD((b))) + (HIGH_WORD((a)) * LOW_WORD((b))))) << 16) + \
((int64_t)(HIGH_WORD((a)) * HIGH_WORD((b))) << 32))
#define MULT(a, b) (UNSIGNED_MULT(ABS((a)), ABS((b))) * SIGN((a)) * SIGN((b)))
int main()
{
int c,d,k;
int64_t multmatrix[3][3];
int64_t sum64 = 0;
int32_t Btranspose[3][3] = {{15643, 24466, 58751},
{54056, 26823, -25563},
{-33591, 54561, -13777}};
int32_t B[3][3] = {{15643, 54056, -33591},
{24466, 26823, 54561},
{58751, -25563, -13777}};
for ( c = 0 ; c < 3 ; c++ ){
for ( d = 0 ; d < 3 ; d++ ){
for ( k = 0 ; k < 3 ; k++ ){
sum64 = sum64 + MULT(Btranspose[c][k], B[k][d]);
printf("\n the MULT for k = %d is: %ld \n", k, MULT(Btranspose[c][k], B[k][d]));
printf("\n the sum for k = %d is: %ld \n", k, sum64);
}
multmatrix[c][d] = sum64;
sum64 = 0;
}
}
printf("\n\n multmatrix \n");
for( c = 0 ; c < 3; c++ ){
printf("\n");
for( d = 0 ; d < 3 ; d++ ){
printf(" %ld ", multmatrix[c][d]);
}
}
return 0;
}
我的输出低于put错误,我注意到错误是当第3个元素(58751 * 58751)与k = 2相乘时。 我认为不会溢出因为58751 ^ 2需要32位。
the MULT for k = 0 is: 244703449 the sum for k = 0 is: 244703449 the MULT for k = 1 is: 598585156 the sum for k = 1 is: 843288605 the MULT for k = 2 is: 46036225 // this is WRONG!!! the sum for k = 2 is: 889324830 . . . . the MULT for k = 2 is: 189805729 the sum for k = 2 is: 1330739379 multmatrix 889324830 650114833 324678230 650114833 1504730698 -308929574 324678230 -308929574 1330739379
正确的结果应该是
multmatrix - correct
4.2950e+09 -2.2870e+03 1.2886e+04
-2.2870e+03 4.2950e+09 -1.2394e+05
1.2886e+04 -1.2394e+05 4.2951e+09
为什么矩阵的乘法错误? 我应该更改上面的代码,以便两个矩阵的乘法将是溢出的?
(我正在尝试编写一个程序,将两个32位数相乘,以便在只有32位寄存器的系统上导入)
所以根据下面的答案,这实际上有效。
#define LOW_WORD(x) ((uint32_t)(x) & 0xffff)
#define HIGH_WORD(x) ((uint32_t)(x) >> 16)
#define ABS(x) (((x) >= 0) ? (x) : -(x))
#define SIGN(x) (((x) >= 0) ? 1 : -1)
#define UNSIGNED_MULT(a, b) \
(((LOW_WORD(a) * LOW_WORD(b)) << 0) + \
((int64_t)(LOW_WORD(a) * HIGH_WORD(b) + HIGH_WORD(a) * LOW_WORD(b)) << 16) + \
((int64_t)(HIGH_WORD((a)) * HIGH_WORD((b))) << 32))
#define MULT(a, b) (UNSIGNED_MULT(ABS((a)), ABS((b))) * SIGN((a)) * SIGN((b)))
感谢您帮助我了解一些事情!我会尝试将整个过程转换为函数并将其发回。
答案 0 :(得分:2)
此
(((x) << 16) >> 16)
正如您所料,不会产生无符号的16位数。此表达式的类型与x
的类型相同,即int32_t
(有符号整数)。实际上,如果对x=58751
使用任何合理的(两个补码)C实现:
x = 00000000000000001110010101111111
(x) << 16 = 11100101011111110000000000000000 (negative number)
(((x) << 16) >> 16) = 11111111111111111110010101111111 (negative number)
要正确提取低16位,请使用无符号算术:
((uint32_t)(x) & 0xffff)
或(保留你的风格)
((uint32_t)(x) << 16 >> 16)
要获得高分,你必须使用无符号算术:
((uint32_t)(x) >> 16)
此外,编译器可能需要帮助确定此表达式的范围(进行优化):
(uint16_t)((uint32_t)(x) & 0xffff)
有些(所有?)编译器非常聪明,可以自己做到这一点。
另外,正如doynax所指出的,低字和高字的乘积是32位数(或31位,但并不重要)。要将它向左移16位,你必须将其转换为64位类型,就像你用高位字一样:
((int64_t)(LOW_WORD(a) * HIGH_WORD(b) + HIGH_WORD(a) * LOW_WORD(b)) << 16)