xref: /openbmc/qemu/target/riscv/m128_helper.c (revision 73c19706)
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/exec-all.h"
23 #include "exec/helper-proto.h"
24 
HELPER(divu_i128)25 target_ulong HELPER(divu_i128)(CPURISCVState *env,
26                                target_ulong ul, target_ulong uh,
27                                target_ulong vl, target_ulong vh)
28 {
29     target_ulong ql, qh;
30     Int128 q;
31 
32     if (vl == 0 && vh == 0) { /* Handle special behavior on div by zero */
33         ql = ~0x0;
34         qh = ~0x0;
35     } else {
36         q = int128_divu(int128_make128(ul, uh), int128_make128(vl, vh));
37         ql = int128_getlo(q);
38         qh = int128_gethi(q);
39     }
40 
41     env->retxh = qh;
42     return ql;
43 }
44 
HELPER(remu_i128)45 target_ulong HELPER(remu_i128)(CPURISCVState *env,
46                                target_ulong ul, target_ulong uh,
47                                target_ulong vl, target_ulong vh)
48 {
49     target_ulong rl, rh;
50     Int128 r;
51 
52     if (vl == 0 && vh == 0) {
53         rl = ul;
54         rh = uh;
55     } else {
56         r = int128_remu(int128_make128(ul, uh), int128_make128(vl, vh));
57         rl = int128_getlo(r);
58         rh = int128_gethi(r);
59     }
60 
61     env->retxh = rh;
62     return rl;
63 }
64 
HELPER(divs_i128)65 target_ulong HELPER(divs_i128)(CPURISCVState *env,
66                                target_ulong ul, target_ulong uh,
67                                target_ulong vl, target_ulong vh)
68 {
69     target_ulong qh, ql;
70     Int128 q;
71 
72     if (vl == 0 && vh == 0) { /* Div by zero check */
73         ql = ~0x0;
74         qh = ~0x0;
75     } else if (uh == (1ULL << (TARGET_LONG_BITS - 1)) && ul == 0 &&
76                vh == ~0x0 && vl == ~0x0) {
77         /* Signed div overflow check (-2**127 / -1) */
78         ql = ul;
79         qh = uh;
80     } else {
81         q = int128_divs(int128_make128(ul, uh), int128_make128(vl, vh));
82         ql = int128_getlo(q);
83         qh = int128_gethi(q);
84     }
85 
86     env->retxh = qh;
87     return ql;
88 }
89 
HELPER(rems_i128)90 target_ulong HELPER(rems_i128)(CPURISCVState *env,
91                                target_ulong ul, target_ulong uh,
92                                target_ulong vl, target_ulong vh)
93 {
94     target_ulong rh, rl;
95     Int128 r;
96 
97     if (vl == 0 && vh == 0) {
98         rl = ul;
99         rh = uh;
100     } else {
101         r = int128_rems(int128_make128(ul, uh), int128_make128(vl, vh));
102         rl = int128_getlo(r);
103         rh = int128_gethi(r);
104     }
105 
106     env->retxh = rh;
107     return rl;
108 }
109