1 | /* Copyright (c) 2017 SiFive Inc. All rights reserved. |
---|
2 | |
---|
3 | This copyrighted material is made available to anyone wishing to use, |
---|
4 | modify, copy, or redistribute it subject to the terms and conditions |
---|
5 | of the FreeBSD License. This program is distributed in the hope that |
---|
6 | it will be useful, but WITHOUT ANY WARRANTY expressed or implied, |
---|
7 | including the implied warranties of MERCHANTABILITY or FITNESS FOR |
---|
8 | A PARTICULAR PURPOSE. A copy of this license is available at |
---|
9 | http://www.opensource.org/licenses. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <ieeefp.h> |
---|
13 | |
---|
14 | #ifdef __riscv_flen |
---|
15 | static void |
---|
16 | fssr(unsigned value) |
---|
17 | { |
---|
18 | asm volatile ("fssr %0" :: "r"(value)); |
---|
19 | } |
---|
20 | |
---|
21 | static unsigned |
---|
22 | frsr() |
---|
23 | { |
---|
24 | unsigned value; |
---|
25 | asm volatile ("frsr %0" : "=r" (value)); |
---|
26 | return value; |
---|
27 | } |
---|
28 | |
---|
29 | static fp_rnd |
---|
30 | frm_fp_rnd (unsigned frm) |
---|
31 | { |
---|
32 | switch (frm) |
---|
33 | { |
---|
34 | case 0: return FP_RN; |
---|
35 | case 1: return FP_RZ; |
---|
36 | case 2: return FP_RM; |
---|
37 | case 3: return FP_RP; |
---|
38 | /* 4 ~ 7 is invalid value, so just retun FP_RP. */ |
---|
39 | default:return FP_RP; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | #endif /* __riscv_flen */ |
---|
44 | |
---|
45 | fp_except |
---|
46 | fpgetmask(void) |
---|
47 | { |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
51 | fp_rnd |
---|
52 | fpgetround(void) |
---|
53 | { |
---|
54 | #ifdef __riscv_flen |
---|
55 | unsigned rm = (frsr () >> 5) & 0x7; |
---|
56 | return frm_fp_rnd (rm); |
---|
57 | #else |
---|
58 | return FP_RZ; |
---|
59 | #endif /* __riscv_flen */ |
---|
60 | } |
---|
61 | |
---|
62 | fp_except |
---|
63 | fpgetsticky(void) |
---|
64 | { |
---|
65 | #ifdef __riscv_flen |
---|
66 | return frsr () & 0x1f; |
---|
67 | #else |
---|
68 | return 0; |
---|
69 | #endif /* __riscv_flen */ |
---|
70 | } |
---|
71 | |
---|
72 | fp_except |
---|
73 | fpsetmask(fp_except mask) |
---|
74 | { |
---|
75 | return -1; |
---|
76 | } |
---|
77 | |
---|
78 | fp_rnd |
---|
79 | fpsetround(fp_rnd rnd_dir) |
---|
80 | { |
---|
81 | #ifdef __riscv_flen |
---|
82 | unsigned fsr = frsr (); |
---|
83 | unsigned rm = (fsr >> 5) & 0x7; |
---|
84 | unsigned new_rm; |
---|
85 | switch (rnd_dir) |
---|
86 | { |
---|
87 | case FP_RN: new_rm = 0; |
---|
88 | case FP_RZ: new_rm = 1; |
---|
89 | case FP_RM: new_rm = 2; |
---|
90 | case FP_RP: new_rm = 3; |
---|
91 | default: return -1; |
---|
92 | } |
---|
93 | fssr (new_rm << 5 | fsr & 0x1f); |
---|
94 | return frm_fp_rnd (rm); |
---|
95 | #else |
---|
96 | return -1; |
---|
97 | #endif /* __riscv_flen */ |
---|
98 | } |
---|
99 | |
---|
100 | fp_except |
---|
101 | fpsetsticky(fp_except sticky) |
---|
102 | { |
---|
103 | #ifdef __riscv_flen |
---|
104 | unsigned fsr = frsr (); |
---|
105 | fssr (sticky & 0x1f | fsr & ~0x1f); |
---|
106 | return fsr & 0x1f; |
---|
107 | #else |
---|
108 | return -1; |
---|
109 | #endif /* __riscv_flen */ |
---|
110 | } |
---|