# Best way to do multi-precision integer compares on RISC-V?

**URL:** <https://forums.sifive.com/t/best-way-to-do-multi-precision-integer-compares-on-risc-v/484>\
**Category:** RISC-V\
**Created:** [April 14, 2017, 6:58am UTC](https://forums.sifive.com/t/best-way-to-do-multi-precision-integer-compares-on-risc-v/484 "2017-04-14T06:58:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nimblemachines](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@nimblemachines](https://forums.sifive.com/u/nimblemachines)\
**Post date:** [April 14, 2017, 6:58am UTC](https://forums.sifive.com/t/best-way-to-do-multi-precision-integer-compares-on-risc-v/484/1 "2017-04-14T06:58:43Z")

</div>

I’m writing my own 64-bit integer math routines for RV32. Call me crazy. Actually, it’s for the RISC-V target of my Forth cross-compiler project, [muforth](https://github.com/nimblemachines/muforth).

Having never written assembler code for an architecture that lacks condition codes, I was at first at a loss about how to approach the problem. After some thinking, I realized that I could use the SLTU instruction to calculate the carry (for add) and borrow (for subtract). I tested that code, and it seems to work correctly.

But doing 64-bit signed and unsigned _compares_ - especially doing it without any branch instructions - mystified me even more. I initially tried to modify my 64-bit subtraction code, but I kept getting wrong answers.

After yet _more_ thinking, I realized a way to get the correct answer, but it requires the use of a conditional branch.

Here is my method:

- Compare the high-order words. If they are _equal_, return as the result the _unsigned_ comparison of the low-order words.

- Otherwise, ignore the low-order words and:

I tested this code and it seems to work, but it requires a BEQ or BNE instruction and two separate code paths. What I am wondering is, am I missing something? Is there a better way?

Sorry about the re-invention of the wheel here. 😉

---

<div class="post-metadata">

**Author:** ![brucehoult](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/brucehoult/32/24_2.png) [@brucehoult](https://forums.sifive.com/u/brucehoult)\
**Post date:** [April 14, 2017, 7:59am UTC](https://forums.sifive.com/t/best-way-to-do-multi-precision-integer-compares-on-risc-v/484/2 "2017-04-14T07:59:13Z")

</div>

With no condition codes (and with SLTU etc), it means if you can write it in C then you can write it exactly the same in assembler 🙂

```
struct intdp {
    unsigned int h, l;
};

bool lt(intdp a, intdp b){
    bool h_lt = a.h < b.h;
    bool h_eq = a.h == b.h;
    bool l_lt = a.l < b.l;
    return h_lt | h_eq & l_lt;
}

   2: 00d5b6b3 sltu a3,a1,a3
   6: 40c505b3 sub a1,a0,a2
   a: 0015b593 seqz a1,a1
   e: 8eed and a3,a3,a1
  10: 00c53533 sltu a0,a0,a2
  14: 8d55 or a0,a0,a3

```

Not very short, but what can you do?

---

<div class="post-metadata">

**Author:** ![brucehoult](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/brucehoult/32/24_2.png) [@brucehoult](https://forums.sifive.com/u/brucehoult)\
**Post date:** [April 14, 2017, 11:41am UTC](https://forums.sifive.com/t/best-way-to-do-multi-precision-integer-compares-on-risc-v/484/3 "2017-04-14T11:41:23Z")

</div>

x86\_64 doesn’t do any better on this algorithm, by the way – one instruction and two bytes longer:

```
 0:	48 39 ce cmp %rcx,%rsi
 3:	0f 92 c1 setb %cl
 6:	48 39 d7 cmp %rdx,%rdi
 9:	0f 94 c0 sete %al
 c:	21 c8 and %ecx,%eax
 e:	48 39 d7 cmp %rdx,%rdi
11:	0f 92 c2 setb %dl
14:	09 d0 or %edx,%eax

```

Using gcc’s built in \_\_int128 with the “\<” operator on x86 also gives one instruction more than the branchless RISC-V code … including three conditional branches! Of course if you get lucky and the high words differ then you only execute three or four of them. And it’s four bytes shorter than the branchless RISC-V code.

```
22:	48 39 ce cmp %rcx,%rsi
25:	b0 01 mov $0x1,%al
27:	7c 09 jl 32 <_Z3iltnn+0x10>
29:	7f 05 jg 30 <_Z3iltnn+0xe>
2b:	48 39 d7 cmp %rdx,%rdi
2e:	72 02 jb 32 <_Z3iltnn+0x10>
30:	31 c0 xor %eax,%eax
```
