1 /*
2  * Test R5900-specific three-operand MADD and MADD1.
3  */
4 
5 #include <stdio.h>
6 #include <inttypes.h>
7 #include <assert.h>
8 
9 int64_t madd(int64_t a, int32_t rs, int32_t rt)
10 {
11     int32_t lo = a;
12     int32_t hi = a >> 32;
13     int32_t rd;
14     int64_t r;
15 
16     __asm__ __volatile__ (
17             "    mtlo %5\n"
18             "    mthi %6\n"
19             "    madd %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 = ((int64_t)hi << 32) | (uint32_t)lo;
25 
26     assert(a + (int64_t)rs * rt == r);
27     assert(rd == lo);
28 
29     return r;
30 }
31 
32 int64_t madd1(int64_t a, int32_t rs, int32_t rt)
33 {
34     int32_t lo = a;
35     int32_t hi = a >> 32;
36     int32_t rd;
37     int64_t r;
38 
39     __asm__ __volatile__ (
40             "    mtlo1 %5\n"
41             "    mthi1 %6\n"
42             "    madd1 %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 = ((int64_t)hi << 32) | (uint32_t)lo;
48 
49     assert(a + (int64_t)rs * rt == r);
50     assert(rd == lo);
51 
52     return r;
53 }
54 
55 static int64_t madd_variants(int64_t a, int32_t rs, int32_t rt)
56 {
57     int64_t rd  = madd(a, rs, rt);
58     int64_t rd1 = madd1(a, rs, rt);
59 
60     assert(rd == rd1);
61 
62     return rd;
63 }
64 
65 static void verify_madd(int64_t a, int32_t rs, int32_t rt, int64_t expected)
66 {
67     assert(madd_variants(a, rs, rt) == expected);
68     assert(madd_variants(a, -rs, rt) == a + a - expected);
69     assert(madd_variants(a, rs, -rt) == a + a - expected);
70     assert(madd_variants(a, -rs, -rt) == expected);
71 }
72 
73 int main()
74 {
75     verify_madd(13, 17, 19, 336);
76 
77     return 0;
78 }
79