https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111593
Bug ID: 111593
Summary: wrong code for 128-bit multiplication on MIPS64R6
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: mikulas at artax dot karlin.mff.cuni.cz
Target Milestone: ---
MIPS64R6 has new instructions for multiplication and division. GCC uses them,
however it miscompiles 128-bit multiplication.
When you compile and run this program with -O1 or -O2 on mips64r6, you get
incorrect result 9F172AF9AEE4FDB2FD12E7537CC82A0F. The correct result is
60E3DC5DAC542B19FD12E7537CC82A0F.
#include <stdio.h>
__attribute__((noinline,noclone)) static unsigned __int128 power(unsigned
__int128 a, unsigned __int128 b)
{
unsigned __int128 c = 1;
while (b) {
if (b & 1)
c *= a;
a *= a;
b >>= 1;
}
return c;
}
int main(void)
{
int i;
unsigned __int128 a = 0x1234567890abcdefULL;
unsigned __int128 b = 0x1234567890abcdefULL;
unsigned __int128 c = power(a, b);
for (i = 124; i >= 0; i -= 4) {
printf("%X", (unsigned)(c >> i) & 0xf);
}
printf("\n");
return 0;
}
How to reproduce:
On Debian SID, install the packages gcc-13-mipsisa64r6-linux-gnuabi64,
libc6-dev-mips64r6-cross and qemu-user.
Run mipsisa64r6-linux-gnuabi64-gcc-13 -O2 power.c && /usr/bin/qemu-mips64 -L
/usr/mipsisa64r6-linux-gnuabi64/ a.out
The bug happens with gcc-10, gcc-11, gcc-12 and gcc-13 (I didn't try older
releases).