fenv.c raw
1 #include <fenv.h>
2 #include <features.h>
3
4 static inline unsigned get_fpc(void)
5 {
6 unsigned fpc;
7 __asm__ __volatile__("efpc %0" : "=r"(fpc));
8 return fpc;
9 }
10
11 static inline void set_fpc(unsigned fpc)
12 {
13 __asm__ __volatile__("sfpc %0" :: "r"(fpc));
14 }
15
16 int feclearexcept(int mask)
17 {
18 mask &= FE_ALL_EXCEPT;
19 set_fpc(get_fpc() & ~mask);
20 return 0;
21 }
22
23 int feraiseexcept(int mask)
24 {
25 mask &= FE_ALL_EXCEPT;
26 set_fpc(get_fpc() | mask);
27 return 0;
28 }
29
30 int fetestexcept(int mask)
31 {
32 return get_fpc() & mask & FE_ALL_EXCEPT;
33 }
34
35 int fegetround(void)
36 {
37 return get_fpc() & 3;
38 }
39
40 hidden int __fesetround(int r)
41 {
42 set_fpc(get_fpc() & ~3L | r);
43 return 0;
44 }
45
46 int fegetenv(fenv_t *envp)
47 {
48 *envp = get_fpc();
49 return 0;
50 }
51
52 int fesetenv(const fenv_t *envp)
53 {
54 set_fpc(envp != FE_DFL_ENV ? *envp : 0);
55 return 0;
56 }
57