7 ms·
Godbolt: typedef __int128_t int128_t; int128_t add(int128_t left, int128_t right) { return left + right; } GCC 10, -O2, RISC-V: add(__int128,
by kazinator 5y ago
Godbolt:
typedef __int128_t int128_t;
int128_t add(int128_t left, int128_t right)
{
return left + right;
}
GCC 10, -O2, RISC-V:
add(__int128, __int128):
mv a5,a0
add a0,a0,a2
sltu a5,a0,a5
add a1,a1,a3
add a1,a5,a1
ret
ARM64:
add(__int128, __int128):
adds x0, x0, x2
adc x1, x1, x3
ret
This issue hurts the wider types that are compiler built-ins.
Even though C has a programming model that is devoid of any carry flag concept, canned types like a 128 bit integer can take advantage of it.
Portable C code to simulate a 128 bit integer will probably emit bad code across the board. The code will explicitly calculate the carry as an additional operand and pull it into the result. The RISC-V won't look any worse, then, in all likelihood.
(The above RISC-V instruction set sequence is shorter than the mailing list post author's 7 line sequence because it doesn't calculate a carry out: the result is truncated. You'd need a carry out to continue a wider addition.)