xref: /qemu/target/riscv/m128_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
1 /*
2  * RISC-V Emulation Helpers for QEMU.
3  *
4  * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5  * Copyright (c) 2017-2018 SiFive, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms and conditions of the GNU General Public License,
9  * version 2 or later, as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 
HELPER(divu_i128)24 target_ulong HELPER(divu_i128)(CPURISCVState *env,
25                                target_ulong ul, target_ulong uh,
26                                target_ulong vl, target_ulong vh)
27 {
28     target_ulong ql, qh;
29     Int128 q;
30 
31     if (vl == 0 && vh == 0) { /* Handle special behavior on div by zero */
32         ql = ~0x0;
33         qh = ~0x0;
34     } else {
35         q = int128_divu(int128_make128(ul, uh), int128_make128(vl, vh));
36         ql = int128_getlo(q);
37         qh = int128_gethi(q);
38     }
39 
40     env->retxh = qh;
41     return ql;
42 }
43 
HELPER(remu_i128)44 target_ulong HELPER(remu_i128)(CPURISCVState *env,
45                                target_ulong ul, target_ulong uh,
46                                target_ulong vl, target_ulong vh)
47 {
48     target_ulong rl, rh;
49     Int128 r;
50 
51     if (vl == 0 && vh == 0) {
52         rl = ul;
53         rh = uh;
54     } else {
55         r = int128_remu(int128_make128(ul, uh), int128_make128(vl, vh));
56         rl = int128_getlo(r);
57         rh = int128_gethi(r);
58     }
59 
60     env->retxh = rh;
61     return rl;
62 }
63 
HELPER(divs_i128)64 target_ulong HELPER(divs_i128)(CPURISCVState *env,
65                                target_ulong ul, target_ulong uh,
66                                target_ulong vl, target_ulong vh)
67 {
68     target_ulong qh, ql;
69     Int128 q;
70 
71     if (vl == 0 && vh == 0) { /* Div by zero check */
72         ql = ~0x0;
73         qh = ~0x0;
74     } else if (uh == (1ULL << (TARGET_LONG_BITS - 1)) && ul == 0 &&
75                vh == ~0x0 && vl == ~0x0) {
76         /* Signed div overflow check (-2**127 / -1) */
77         ql = ul;
78         qh = uh;
79     } else {
80         q = int128_divs(int128_make128(ul, uh), int128_make128(vl, vh));
81         ql = int128_getlo(q);
82         qh = int128_gethi(q);
83     }
84 
85     env->retxh = qh;
86     return ql;
87 }
88 
HELPER(rems_i128)89 target_ulong HELPER(rems_i128)(CPURISCVState *env,
90                                target_ulong ul, target_ulong uh,
91                                target_ulong vl, target_ulong vh)
92 {
93     target_ulong rh, rl;
94     Int128 r;
95 
96     if (vl == 0 && vh == 0) {
97         rl = ul;
98         rh = uh;
99     } else {
100         r = int128_rems(int128_make128(ul, uh), int128_make128(vl, vh));
101         rl = int128_getlo(r);
102         rh = int128_gethi(r);
103     }
104 
105     env->retxh = rh;
106     return rl;
107 }
108