1/*
2 * Copyright (c) 2016 The University of Virginia
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Alec Roelke
29 */
30
31#include <cstdint>
32#include <limits>
33
34#include "insttest.h"
35#include "rv64m.h"
36
37int main()
38{
39    using namespace std;
40    using namespace insttest;
41
42    // MUL
43    expect<int64_t>(39285, []{return M::mul(873, 45);}, "mul");
44    expect<int64_t>(0, []{return M::mul(0x4000000000000000LL, 4);},
45            "mul, overflow");
46
47    // MULH
48    expect<int64_t>(1, []{return M::mulh(0x4000000000000000LL, 4);}, "mulh");
49    expect<int64_t>(-1, []{return M::mulh(numeric_limits<int64_t>::min(), 2);},
50            "mulh, negative");
51    expect<int64_t>(0, []{return M::mulh(-1, -1);}, "mulh, all bits set");
52
53    // MULHSU
54    expect<int64_t>(-1, []{return M::mulhsu(-1, -1);}, "mulhsu, all bits set");
55    expect<int64_t>(-1,
56            []{return M::mulhsu(numeric_limits<int64_t>::min(), 2);},\
57            "mulhsu");
58
59    // MULHU
60    expect<uint64_t>(1, []{return M::mulhu(0x8000000000000000ULL, 2);},
61            "mulhu");
62    expect<uint64_t>(0xFFFFFFFFFFFFFFFEULL, []{return M::mulhu(-1, -1);},
63            "mulhu, all bits set");
64
65    // DIV
66    expect<int64_t>(-7, []{return M::div(-59, 8);}, "div");
67    expect<int64_t>(-1, []{return M::div(255, 0);}, "div/0");
68    expect<int64_t>(numeric_limits<int64_t>::min(),
69            []{return M::div(numeric_limits<int64_t>::min(), -1);},
70            "div, overflow");
71
72    // DIVU
73    expect<uint64_t>(2305843009213693944LL, []{return M::divu(-59, 8);},
74            "divu");
75    expect<uint64_t>(numeric_limits<uint64_t>::max(),
76            []{return M::divu(255, 0);}, "divu/0");
77    expect<uint64_t>(0,
78            []{return M::divu(numeric_limits<uint64_t>::min(), -1);},
79            "divu, \"overflow\"");
80
81    // REM
82    expect<int64_t>(-3, []{return M::rem(-59, 8);}, "rem");
83    expect<int64_t>(255, []{return M::rem(255, 0);}, "rem/0");
84    expect<int64_t>(0, []{return M::rem(numeric_limits<int64_t>::min(), -1);},
85            "rem, overflow");
86
87    // REMU
88    expect<uint64_t>(5, []{return M::remu(-59, 8);}, "remu");
89    expect<uint64_t>(255, []{return M::remu(255, 0);}, "remu/0");
90    expect<uint64_t>(0x8000000000000000ULL,
91            []{return M::remu(0x8000000000000000ULL, -1);},
92            "remu, \"overflow\"");
93
94    // MULW
95    expect<int64_t>(-100,
96            []{return M::mulw(0x7FFFFFFF00000005LL, 0x80000000FFFFFFECLL);},
97            "mulw, truncate");
98    expect<int64_t>(0, []{return M::mulw(0x40000000, 4);}, "mulw, overflow");
99
100    // DIVW
101    expect<int64_t>(-7,
102            []{return M::divw(0x7FFFFFFFFFFFFFC5LL, 0xFFFFFFFF00000008LL);},
103            "divw, truncate");
104    expect<int64_t>(-1, []{return M::divw(65535, 0);}, "divw/0");
105    expect<int64_t>(numeric_limits<int32_t>::min(),
106            []{return M::divw(numeric_limits<int32_t>::min(), -1);},
107            "divw, overflow");
108
109    // DIVUW
110    expect<int64_t>(536870904,
111            []{return M::divuw(0x7FFFFFFFFFFFFFC5LL, 0xFFFFFFFF00000008LL);},
112            "divuw, truncate");
113    expect<int64_t>(numeric_limits<uint64_t>::max(),
114            []{return M::divuw(65535, 0);}, "divuw/0");
115    expect<int64_t>(0,
116            []{return M::divuw(numeric_limits<int32_t>::min(), -1);},
117            "divuw, \"overflow\"");
118    expect<int64_t>(-1,
119            []{return M::divuw(numeric_limits<uint32_t>::max(), 1);},
120            "divuw, sign extend");
121
122    // REMW
123    expect<int64_t>(-3,
124            []{return M::remw(0x7FFFFFFFFFFFFFC5LL, 0xFFFFFFFF00000008LL);},
125            "remw, truncate");
126    expect<int64_t>(65535, []{return M::remw(65535, 0);}, "remw/0");
127    expect<int64_t>(0, []{return M::remw(numeric_limits<int32_t>::min(), -1);},
128            "remw, overflow");
129
130    // REMUW
131    expect<int64_t>(5,
132            []{return M::remuw(0x7FFFFFFFFFFFFFC5LL, 0xFFFFFFFF00000008LL);},
133            "remuw, truncate");
134    expect<int64_t>(65535, []{return M::remuw(65535, 0);}, "remuw/0");
135    expect<int64_t>(numeric_limits<int32_t>::min(),
136            []{return M::remuw(numeric_limits<int32_t>::min(), -1);},
137            "remuw, \"overflow\"");
138    expect<int64_t>(0xFFFFFFFF80000000,
139            []{return M::remuw(0x80000000, 0xFFFFFFFF);},
140            "remuw, sign extend");
141
142    return 0;
143}
144