1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #define MACL_S_MIN (-(1ll << 47))
8 #define MACL_S_MAX ((1ll << 47) - 1)
9
mac_l(int64_t mac,const int32_t * a,const int32_t * b)10 int64_t mac_l(int64_t mac, const int32_t *a, const int32_t *b)
11 {
12 register uint32_t macl __asm__("macl") = mac;
13 register uint32_t mach __asm__("mach") = mac >> 32;
14
15 asm volatile("mac.l @%0+,@%1+"
16 : "+r"(a), "+r"(b), "+x"(macl), "+x"(mach));
17
18 return ((uint64_t)mach << 32) | macl;
19 }
20
21 typedef struct {
22 int64_t mac;
23 int32_t a, b;
24 int64_t res[2];
25 } Test;
26
27 __attribute__((noinline))
test(const Test * t,int sat)28 void test(const Test *t, int sat)
29 {
30 int64_t res;
31
32 if (sat) {
33 asm volatile("sets");
34 } else {
35 asm volatile("clrs");
36 }
37 res = mac_l(t->mac, &t->a, &t->b);
38
39 if (res != t->res[sat]) {
40 fprintf(stderr, "%#llx + (%#x * %#x) = %#llx -- got %#llx\n",
41 t->mac, t->a, t->b, t->res[sat], res);
42 abort();
43 }
44 }
45
main()46 int main()
47 {
48 static const Test tests[] = {
49 { 0x00007fff12345678ll, INT32_MAX, INT32_MAX,
50 { 0x40007ffe12345679ll, MACL_S_MAX } },
51 { MACL_S_MIN, -1, 1,
52 { 0xffff7fffffffffffll, MACL_S_MIN } },
53 { INT64_MIN, -1, 1,
54 { INT64_MAX, MACL_S_MIN } },
55 { 0x00007fff00000000ll, INT32_MAX, INT32_MAX,
56 { 0x40007ffe00000001ll, MACL_S_MAX } },
57 { 4, 1, 2, { 6, 6 } },
58 { -4, -1, -2, { -2, -2 } },
59 };
60
61 for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
62 for (int j = 0; j < 2; ++j) {
63 test(&tests[i], j);
64 }
65 }
66 return 0;
67 }
68