xref: /qemu/target/riscv/internals.h (revision f1eed927fb3a1212af8e324cf242cf6f4bd6fd04)
1f476f177SLIU Zhiwei /*
2f476f177SLIU Zhiwei  * QEMU RISC-V CPU -- internal functions and types
3f476f177SLIU Zhiwei  *
4f476f177SLIU Zhiwei  * Copyright (c) 2020 T-Head Semiconductor Co., Ltd. All rights reserved.
5f476f177SLIU Zhiwei  *
6f476f177SLIU Zhiwei  * This program is free software; you can redistribute it and/or modify it
7f476f177SLIU Zhiwei  * under the terms and conditions of the GNU General Public License,
8f476f177SLIU Zhiwei  * version 2 or later, as published by the Free Software Foundation.
9f476f177SLIU Zhiwei  *
10f476f177SLIU Zhiwei  * This program is distributed in the hope it will be useful, but WITHOUT
11f476f177SLIU Zhiwei  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12f476f177SLIU Zhiwei  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13f476f177SLIU Zhiwei  * more details.
14f476f177SLIU Zhiwei  *
15f476f177SLIU Zhiwei  * You should have received a copy of the GNU General Public License along with
16f476f177SLIU Zhiwei  * this program.  If not, see <http://www.gnu.org/licenses/>.
17f476f177SLIU Zhiwei  */
18f476f177SLIU Zhiwei 
19f476f177SLIU Zhiwei #ifndef RISCV_CPU_INTERNALS_H
20f476f177SLIU Zhiwei #define RISCV_CPU_INTERNALS_H
21f476f177SLIU Zhiwei 
22f476f177SLIU Zhiwei #include "hw/registerfields.h"
23f476f177SLIU Zhiwei 
24751538d5SLIU Zhiwei /* share data between vector helpers and decode code */
25f9298de5SFrank Chang FIELD(VDATA, VM, 0, 1)
26f9298de5SFrank Chang FIELD(VDATA, LMUL, 1, 3)
27*f1eed927SeopXD FIELD(VDATA, VTA, 4, 1)
28*f1eed927SeopXD FIELD(VDATA, NF, 5, 4)
29*f1eed927SeopXD FIELD(VDATA, WD, 5, 1)
30121ddbb3SLIU Zhiwei 
31121ddbb3SLIU Zhiwei /* float point classify helpers */
32121ddbb3SLIU Zhiwei target_ulong fclass_h(uint64_t frs1);
33121ddbb3SLIU Zhiwei target_ulong fclass_s(uint64_t frs1);
34121ddbb3SLIU Zhiwei target_ulong fclass_d(uint64_t frs1);
359fc08be6SLIU Zhiwei 
36f7697f0eSYifei Jiang #ifndef CONFIG_USER_ONLY
37f7697f0eSYifei Jiang extern const VMStateDescription vmstate_riscv_cpu;
38f7697f0eSYifei Jiang #endif
39f7697f0eSYifei Jiang 
40986c895dSFrank Chang enum {
41986c895dSFrank Chang     RISCV_FRM_RNE = 0,  /* Round to Nearest, ties to Even */
42986c895dSFrank Chang     RISCV_FRM_RTZ = 1,  /* Round towards Zero */
43986c895dSFrank Chang     RISCV_FRM_RDN = 2,  /* Round Down */
44986c895dSFrank Chang     RISCV_FRM_RUP = 3,  /* Round Up */
45986c895dSFrank Chang     RISCV_FRM_RMM = 4,  /* Round to Nearest, ties to Max Magnitude */
46986c895dSFrank Chang     RISCV_FRM_DYN = 7,  /* Dynamic rounding mode */
4775804f71SFrank Chang     RISCV_FRM_ROD = 8,  /* Round to Odd */
48986c895dSFrank Chang };
49986c895dSFrank Chang 
50e1a29bbdSWeiwei Li static inline uint64_t nanbox_s(CPURISCVState *env, float32 f)
519921e3d3SRichard Henderson {
52e1a29bbdSWeiwei Li     /* the value is sign-extended instead of NaN-boxing for zfinx */
53e1a29bbdSWeiwei Li     if (RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) {
54e1a29bbdSWeiwei Li         return (int32_t)f;
55e1a29bbdSWeiwei Li     } else {
569921e3d3SRichard Henderson         return f | MAKE_64BIT_MASK(32, 32);
579921e3d3SRichard Henderson     }
58e1a29bbdSWeiwei Li }
599921e3d3SRichard Henderson 
60e1a29bbdSWeiwei Li static inline float32 check_nanbox_s(CPURISCVState *env, uint64_t f)
6100e925c5SRichard Henderson {
62e1a29bbdSWeiwei Li     /* Disable NaN-boxing check when enable zfinx */
63e1a29bbdSWeiwei Li     if (RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) {
64e1a29bbdSWeiwei Li         return (uint32_t)f;
65e1a29bbdSWeiwei Li     }
66e1a29bbdSWeiwei Li 
6700e925c5SRichard Henderson     uint64_t mask = MAKE_64BIT_MASK(32, 32);
6800e925c5SRichard Henderson 
6900e925c5SRichard Henderson     if (likely((f & mask) == mask)) {
7000e925c5SRichard Henderson         return (uint32_t)f;
7100e925c5SRichard Henderson     } else {
7200e925c5SRichard Henderson         return 0x7fc00000u; /* default qnan */
7300e925c5SRichard Henderson     }
7400e925c5SRichard Henderson }
7500e925c5SRichard Henderson 
76a2464a4cSWeiwei Li static inline uint64_t nanbox_h(CPURISCVState *env, float16 f)
7700c1899fSKito Cheng {
78a2464a4cSWeiwei Li     /* the value is sign-extended instead of NaN-boxing for zfinx */
79a2464a4cSWeiwei Li     if (RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) {
80a2464a4cSWeiwei Li         return (int16_t)f;
81a2464a4cSWeiwei Li     } else {
8200c1899fSKito Cheng         return f | MAKE_64BIT_MASK(16, 48);
8300c1899fSKito Cheng     }
84a2464a4cSWeiwei Li }
8500c1899fSKito Cheng 
86a2464a4cSWeiwei Li static inline float16 check_nanbox_h(CPURISCVState *env, uint64_t f)
8700c1899fSKito Cheng {
88a2464a4cSWeiwei Li     /* Disable nanbox check when enable zfinx */
89a2464a4cSWeiwei Li     if (RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) {
90a2464a4cSWeiwei Li         return (uint16_t)f;
91a2464a4cSWeiwei Li     }
92a2464a4cSWeiwei Li 
9300c1899fSKito Cheng     uint64_t mask = MAKE_64BIT_MASK(16, 48);
9400c1899fSKito Cheng 
9500c1899fSKito Cheng     if (likely((f & mask) == mask)) {
9600c1899fSKito Cheng         return (uint16_t)f;
9700c1899fSKito Cheng     } else {
9800c1899fSKito Cheng         return 0x7E00u; /* default qnan */
9900c1899fSKito Cheng     }
10000c1899fSKito Cheng }
10100c1899fSKito Cheng 
102f476f177SLIU Zhiwei #endif
103