• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_INTRINSICS_GUEST_RISCV64_FPSTATE_H_
18 #define BERBERIS_INTRINSICS_GUEST_RISCV64_FPSTATE_H_
19 
20 #include <cfenv>
21 #include <cstdint>
22 
23 namespace berberis {
24 
25 namespace FPFlags {
26 
27 inline constexpr uint64_t NV = 1 << 4;
28 inline constexpr uint64_t DZ = 1 << 3;
29 inline constexpr uint64_t OF = 1 << 2;
30 inline constexpr uint64_t UF = 1 << 1;
31 inline constexpr uint64_t NX = 1 << 0;
32 inline constexpr uint64_t RM_POS = 5;
33 inline constexpr uint64_t RM_MASK = 0b111;
34 inline constexpr uint64_t RM_MAX = 0b100;
35 inline constexpr uint64_t RNE = 0b000;
36 inline constexpr uint64_t RTZ = 0b001;
37 inline constexpr uint64_t RDN = 0b010;
38 inline constexpr uint64_t RUP = 0b011;
39 inline constexpr uint64_t RMM = 0b100;
40 inline constexpr uint64_t DYN = 0b111;
41 
42 }  // namespace FPFlags
43 
44 namespace intrinsics {
45 
46 // Note that not all RISC-V rounding modes are supported on popular architectures.
47 // FE_TIESAWAY is emulated, but proper emulation needs FE_TOWARDZERO mode.
ToHostRoundingMode(int8_t rm)48 inline int ToHostRoundingMode(int8_t rm) {
49   static constexpr int kRounding[FPFlags::RM_MAX + 1] = {
50       FE_TONEAREST, FE_TOWARDZERO, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO};
51   return kRounding[rm];
52 }
53 
54 }  // namespace intrinsics
55 
56 }  // namespace berberis
57 
58 #endif  // BERBERIS_INTRINSICS_GUEST_RISCV64_FPSTATE_H_
59