Let’s add two numbers

Here’s a complete C function that adds two unsigned 128-bit integers:

typedef unsigned _BitInt(128) u128;
u128 add(u128 a, u128 b) {
    return a + b;
}

Now let’s compile it for 32-bit RISC-V:

clang --target=riscv32 -march=rv32imac -mabi=ilp32 -O2 -S add.c -o -

You can see the output on Compiler Explorer, alongside versions compiled with GCC and with the Zicond extension we’ll look at below.

Here’s the relevant snippet:

        add     a1, t1, t0
        add     a2, a7, t2
        sltu    t0, a1, t1
        add     a2, a2, t0
        beq     a2, a7, .LBB0_2
        sltu    t0, a2, a7
.LBB0_2:
        add     a5, a5, a3
        add     a4, a4, a6
        add     t0, t0, a5
        sltu    a3, a5, a3
        sltu    a5, t0, a5