0

So I have this code snippet in C

int unit_test_case08(int a, int b)
{
    int success = 1336;

    if(a != b)
    {
      success = 1337;
    }
    else
    {
      success = -1;
    }

    return success;
}

Which translates in MIPS to

unit_test_case08:
        addiu   $sp,$sp,-24
        sw      $fp,20($sp)
        move    $fp,$sp
        sw      $4,24($fp)
        sw      $5,28($fp)
        sw      $6,32($fp)
        li      $2,1336           # 0x538
        sw      $2,8($fp)
        lw      $3,24($fp)
        lw      $2,28($fp)
        nop
        beq     $3,$2,$L2
        nop

        li      $2,1337           # 0x539
        sw      $2,8($fp)
        b       $L3
        nop

$L2:
        li      $2,-1                 # 0xffffffffffffffff
        sw      $2,8($fp)
$L3:
        lw      $2,8($fp)
        move    $sp,$fp
        lw      $fp,20($sp)
        addiu   $sp,$sp,24
        jr      $31
        nop

But for my question, im trying to create some test cases for my emulator, but am finding it hard to generate some code in C that will get me BEQL instead of BEQ?

beq     $3,$2,$L2

This question extends to most of the "Likelys" if possible. I am a very new to MIPS.

I would like to learn how to code a test case in C that would translate to BEQL instead of BEQ.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • maybe directly write asm code and assemble it with an assembler would be simpler – Jean-François Fabre Jun 04 '22 at 21:25
  • very unlikely any compiler to generate it. Only hand written assembler – 0___________ Jun 04 '22 at 23:19
  • 1
    You could try: `if (__builtin_expect(a != b, 1))` or the same with 0 – Craig Estey Jun 05 '22 at 00:22
  • @kaylum `beql` is "branch on equal likely" and not "branch on less than or equal to" – Craig Estey Jun 05 '22 at 00:25
  • 1
    this is unoptimized code because `success = 1336;` should never happen if you compiled with optimization – phuclv Jun 05 '22 at 02:07
  • [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) has some tips on writing functions that are useful to look at with optimization enabled. In this case, a store to 2 different variables in different if/else branches might work, or just a conditional store (like https://godbolt.org/z/PWaqoozWx - `-march=mips32r6` uses a beqc no-branch-delay branch, but not beql). With your current code, compilers use `movn` (conditional mov) to select between two constants in a register, no branching. https://godbolt.org/z/ha7W6jcrP – Peter Cordes Jun 05 '22 at 06:05
  • 1
    [Apparently](https://www.cs.cmu.edu/afs/cs/academic/class/15740-f97/public/doc/mips-isa.pdf) MIPS II introduced `beql`. Oh, but only Likely, not Unlikely, so @CraigEstey, it makes sense that GCC just lays out the code so the likely path has no taken branches, because that's what you want anyway. https://godbolt.org/z/onbq7GnzY. Will have to think some more about something that will get GCC to actually put a taken conditional branch on the fast path, not just tail-duplicate. – Peter Cordes Jun 05 '22 at 06:14
  • With GCC and probably Clang too, `__asm__ goto` is one possible aproach if you must do it. – xiver77 Jun 05 '22 at 06:17

0 Answers0