1 /*
2  * Test R5900-specific three-operand MADDU and MADDU1.
3  */
4 
5 #include <stdio.h>
6 #include <inttypes.h>
7 #include <assert.h>
8 
9 uint64_t maddu(uint64_t a, uint32_t rs, uint32_t rt)
10 {
11     uint32_t lo = a;
12     uint32_t hi = a >> 32;
13     uint32_t rd;
14     uint64_t r;
15 
16     __asm__ __volatile__ (
17             "    mtlo  %5\n"
18             "    mthi  %6\n"
19             "    maddu %0, %3, %4\n"
20             "    mflo  %1\n"
21             "    mfhi  %2\n"
22             : "=r" (rd), "=r" (lo), "=r" (hi)
23             : "r" (rs), "r" (rt), "r" (lo), "r" (hi));
24     r = ((uint64_t)hi << 32) | (uint32_t)lo;
25 
26     assert(a + (uint64_t)rs * rt == r);
27     assert(rd == lo);
28 
29     return r;
30 }
31 
32 uint64_t maddu1(uint64_t a, uint32_t rs, uint32_t rt)
33 {
34     uint32_t lo = a;
35     uint32_t hi = a >> 32;
36     uint32_t rd;
37     uint64_t r;
38 
39     __asm__ __volatile__ (
40             "    mtlo1  %5\n"
41             "    mthi1  %6\n"
42             "    maddu1 %0, %3, %4\n"
43             "    mflo1  %1\n"
44             "    mfhi1  %2\n"
45             : "=r" (rd), "=r" (lo), "=r" (hi)
46             : "r" (rs), "r" (rt), "r" (lo), "r" (hi));
47     r = ((uint64_t)hi << 32) | (uint32_t)lo;
48 
49     assert(a + (uint64_t)rs * rt == r);
50     assert(rd == lo);
51 
52     return r;
53 }
54 
55 static int64_t maddu_variants(int64_t a, int32_t rs, int32_t rt)
56 {
57     int64_t rd  = maddu(a, rs, rt);
58     int64_t rd1 = maddu1(a, rs, rt);
59 
60     assert(rd == rd1);
61 
62     return rd;
63 }
64 
65 int main()
66 {
67     assert(maddu_variants(13, 17, 19) == 336);
68 
69     return 0;
70 }
71