summaryrefslogtreecommitdiff
path: root/tests/test-progs/insttest/src/riscv/rv64c.cpp
blob: 869784d72df0944393ff79156f1e25f9dfce8a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2017 The University of Virginia
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Alec Roelke
 */

#include <limits>

#include "insttest.h"
#include "rv64c.h"
#include "rv64d.h"

int main()
{
    using namespace insttest;
    using namespace std;

    // C.LWSP
    expect<bool>(true, []{
        uint64_t lw = 0, lwsp = -1;
        int64_t i = 16;
        asm volatile("lw %0,%2(sp);"
                     "c.lwsp %1,%2(sp);"
                     : "=r" (lw), "=r" (lwsp)
                     : "i" (i));
        return lw == lwsp;
    }, "c.lwsp");

    // C.LDSP
    expect<bool>(true, []{
        uint64_t ld = 0, ldsp = -1;
        int64_t i = 8;
        asm volatile("ld %0,%2(sp);"
                     "c.ldsp %1,%2(sp);"
                     : "=r" (ld), "=r" (ldsp)
                     : "i" (i));
        return ld == ldsp;
    }, "c.ldsp");

    // C.FLDSP
    expect<bool>(true, []{
        double fld = 0.0, fldsp = -1.0;
        int64_t i = 32;
        asm volatile("fld %0,%2(sp);"
                     "c.fldsp %1,%2(sp);"
                     : "=f" (fld), "=f" (fldsp)
                     : "i" (i));
        return D::bits(fld) == D::bits(fldsp);
    }, "c.fldsp");

    // C.SWSP
    expect<bool>(true, []{
        int64_t value = -1, result = 0;
        asm volatile("addi sp,sp,-8;"
                     "c.swsp %1,8(sp);"
                     "lw %0,8(sp);"
                     "addi sp,sp,8;"
                     : "=r" (result)
                     : "r" (value)
                     : "memory");
        return value == result;
    }, "c.swsp");

    // C.SDSP
    expect<bool>(true, []{
        int64_t value = -1, result = 0;
        asm volatile("addi sp,sp,-8;"
                     "c.sdsp %1,8(sp);"
                     "ld %0,8(sp);"
                     "addi sp,sp,8;"
                     : "=r" (result)
                     : "r" (value)
                     : "memory");
        return value == result;
    }, "c.sdsp");

    // C.FSDSP
    expect<bool>(true, []{
        double value = 0.1, result = numeric_limits<double>::signaling_NaN();
        asm volatile("addi sp,sp,-8;"
                     "c.fsdsp %1,8(sp);"
                     "fld %0,8(sp);"
                     "addi sp,sp,8;"
                     : "=f" (result)
                     : "f" (value)
                     : "memory");
        return value == result;
    }, "c.fsdsp");

    // C.LW, C.LD, C.FLD
    expect<int64_t>(458752,
            []{return C::c_load<int32_t, int64_t>(0x00070000);},
            "c.lw, positive");
    expect<int64_t>(numeric_limits<int32_t>::min(),
            []{return C::c_load<int32_t, int64_t>(0x80000000);},
            "c.lw, negative");
    expect<int64_t>(30064771072,
            []{return C::c_load<int64_t, int64_t>(30064771072);}, "c.ld");
    expect<double>(3.1415926, []{return C::c_load<double, double>(3.1415926);},
        "c.fld");

    // C.SW, C.SD, C.FSD
    expect<uint32_t>(0xFFFFFFFF, []{return C::c_store<int32_t>(-1);}, "c.sw");
    expect<uint64_t>(-1, []{return C::c_store<int64_t>(-1);}, "c.sd");
    expect<double>(1.61803398875,
            []{return C::c_store<double>(1.61803398875);}, "c.fsd");

    // C.J, C.JR, C.JALR
    expect<bool>(true, []{return C::c_j();}, "c.j");
    expect<bool>(true, []{return C::c_jr();}, "c.jr");
    expect<bool>(true, []{return C::c_jalr();}, "c.jalr");

    // C.BEQZ
    expect<bool>(true, []{return C::c_beqz(0);}, "c.beqz, zero");
    expect<bool>(false, []{return C::c_beqz(7);}, "c.beqz, not zero");

    // C.BNEZ
    expect<bool>(true, []{return C::c_bnez(15);}, "c.bnez, not zero");
    expect<bool>(false, []{return C::c_bnez(0);}, "c.bnez, zero");

    // C.LI
    expect<int64_t>(1, []{return C::c_li(1);}, "c.li");
    expect<int64_t>(-1, []{return C::c_li(-1);}, "c.li, sign extend");

    // C.LUI
    expect<int64_t>(4096, []{return C::c_lui(1);}, "c.lui");
    // Note that sign extension can't be tested here because apparently the
    // compiler doesn't allow the 6th (sign) bit of the immediate to be 1

    // C.ADDI
    expect<int64_t>(15, []{return C::c_addi(7, 8);}, "c.addi");

    // C.ADDIW
    expect<int64_t>(15, []{return C::c_addiw(8, 7);}, "c.addiw");
    expect<int64_t>(1, []{return C::c_addiw(0xFFFFFFFF, 2);},
            "c.addiw, overflow");
    expect<int64_t>(1, []{return C::c_addiw(0x100000001, 0);},
            "c.addiw, truncate");

    // C.ADDI16SP
    expect<bool>(true, []{
        uint64_t sp = 0, rd = 0;
        const int16_t i = 4;
        asm volatile("mv %0,sp;"
                     "c.addi16sp sp,%2;"
                     "mv %1,sp;"
                     "mv sp,%0;"
                     : "+r" (sp), "=r" (rd)
                     : "i" (i*16));
        return rd == sp + i*16;
    }, "c.addi16sp");

    // C.ADDI4SPN
    expect<bool>(true, []{
        uint64_t sp = 0, rd = 0;
        const int16_t i = 3;
        asm volatile("mv %0,sp;"
                     "c.addi4spn %1,sp,%2;"
                     : "=r" (sp), "=r" (rd)
                     : "i" (i*4));
        return rd == sp + i*4;
    }, "c.addi4spn");

    // C.SLLI
    expect<uint64_t>(16, []{return C::c_slli(1, 4);}, "c.slli");
    expect<uint64_t>(0, []{return C::c_slli(8, 61);}, "c.slli, overflow");

    // C.SRLI
    expect<uint64_t>(4, []{return C::c_srli(128, 5);}, "c.srli");
    expect<uint64_t>(0, []{return C::c_srli(128, 8);}, "c.srli, overflow");
    expect<uint64_t>(1, []{return C::c_srli(-1, 63);}, "c.srli, -1");

    // C.SRAI
    expect<uint64_t>(4, []{return C::c_srai(128, 5);}, "c.srai");
    expect<uint64_t>(0, []{return C::c_srai(128, 8);}, "c.srai, overflow");
    expect<uint64_t>(-1, []{return C::c_srai(-2, 63);}, "c.srai, -1");

    // C.ANDI
    expect<uint64_t>(0, []{return C::c_andi(-1, 0);}, "c.andi (0)");
    expect<uint64_t>(0x1234567812345678ULL,
            []{return C::c_andi(0x1234567812345678ULL, -1);}, "c.andi (1)");

    // C.MV
    expect<int64_t>(1024, []{return C::c_mv(1024);}, "c.mv");

    // C.ADD
    expect<int64_t>(15, []{return C::c_add(10, 5);}, "c.add");

    // C.AND
    expect<uint64_t>(0, []{return C::c_and(-1, 0);}, "c.and (0)");
    expect<uint64_t>(0x1234567812345678ULL,
            []{return C::c_and(0x1234567812345678ULL, -1);}, "c.and (-1)");

    // C.OR
    expect<uint64_t>(-1,
            []{return C::c_or(0xAAAAAAAAAAAAAAAAULL,
                    0x5555555555555555ULL);},
            "c.or (1)");
    expect<uint64_t>(0xAAAAAAAAAAAAAAAAULL,
            []{return C::c_or(0xAAAAAAAAAAAAAAAAULL,
                    0xAAAAAAAAAAAAAAAAULL);},
            "c.or (A)");

    // C.XOR
    expect<uint64_t>(-1,
            []{return C::c_xor(0xAAAAAAAAAAAAAAAAULL,
                    0x5555555555555555ULL);},
            "c.xor (1)");
    expect<uint64_t>(0,
            []{return C::c_xor(0xAAAAAAAAAAAAAAAAULL,
                    0xAAAAAAAAAAAAAAAAULL);},
            "c.xor (0)");

    // C.SUB
    expect<int64_t>(65535, []{return C::c_sub(65536, 1);}, "c.sub");

    // C.ADDW
    expect<int64_t>(1073742078, []{return C::c_addw(0x3FFFFFFF, 255);},
            "c.addw");
    expect<int64_t>(-1, []{return C::c_addw(0x7FFFFFFF, 0x80000000);},
            "c.addw, overflow");
    expect<int64_t>(65536, []{return C::c_addw(0xFFFFFFFF0000FFFFLL, 1);},
            "c.addw, truncate");

    // C.SUBW
    expect<int64_t>(65535, []{return C::c_subw(65536, 1);}, "c.subw");
    expect<int64_t>(-1, []{return C::c_subw(0x7FFFFFFF, 0x80000000);},
            "c.subw, \"overflow\"");
    expect<int64_t>(0,
            []{return C::c_subw(0xAAAAAAAAFFFFFFFFULL,0x55555555FFFFFFFFULL);},
            "c.subw, truncate");
}