1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <fenv.h>
30 #include <stdint.h>
31 
32 const fenv_t __fe_dfl_env = 0;
33 
fegetenv(fenv_t * envp)34 int fegetenv(fenv_t* envp) {
35   __asm__ __volatile__("frcsr %0" : "=r"(*envp));
36   return 0;
37 }
38 
fesetenv(const fenv_t * envp)39 int fesetenv(const fenv_t* envp) {
40   fenv_t env;
41   fegetenv(&env);
42   if (*envp != env) {
43     __asm__ __volatile__("fscsr %z0" : : "r"(*envp));
44   }
45   return 0;
46 }
47 
feclearexcept(int excepts)48 int feclearexcept(int excepts) {
49   __asm__ __volatile__("csrc fflags, %0" : : "r"(excepts & FE_ALL_EXCEPT));
50   return 0;
51 }
52 
fegetexceptflag(fexcept_t * flagp,int excepts)53 int fegetexceptflag(fexcept_t* flagp, int excepts) {
54   *flagp = fetestexcept(excepts & FE_ALL_EXCEPT);
55   return 0;
56 }
57 
fesetexceptflag(const fexcept_t * flagp,int excepts)58 int fesetexceptflag(const fexcept_t* flagp, int excepts) {
59   feclearexcept((~*flagp) & excepts);
60   feraiseexcept(*flagp & excepts);
61   return 0;
62 }
63 
feraiseexcept(int excepts)64 int feraiseexcept(int excepts) {
65   __asm__ __volatile__("csrs fflags, %0" : : "r"(excepts));
66   return 0;
67 }
68 
fetestexcept(int excepts)69 int fetestexcept(int excepts) {
70   int flags;
71   __asm__ __volatile__("frflags %0" : "=r"(flags));
72   return flags & excepts;
73 }
74 
fegetround(void)75 int fegetround(void) {
76   int rm;
77   __asm__ __volatile__("frrm %0" : "=r"(rm));
78   return rm;
79 }
80 
fesetround(int round)81 int fesetround(int round) {
82   if (round < FE_TONEAREST || round > FE_UPWARD) return -1;
83   __asm__ __volatile__("fsrm %z0" : : "r"(round));
84   return 0;
85 }
86 
feholdexcept(fenv_t * envp)87 int feholdexcept(fenv_t* envp) {
88   fegetenv(envp);
89   feclearexcept(FE_ALL_EXCEPT);
90   return 0;
91 }
92 
feupdateenv(const fenv_t * envp)93 int feupdateenv(const fenv_t* envp) {
94   int excepts = fetestexcept(FE_ALL_EXCEPT);
95   fesetenv(envp);
96   feraiseexcept(excepts);
97   return 0;
98 }
99 
feenableexcept(int mask __unused)100 int feenableexcept(int mask __unused) {
101   return -1;
102 }
103 
fedisableexcept(int mask __unused)104 int fedisableexcept(int mask __unused) {
105   return 0;
106 }
107 
fegetexcept(void)108 int fegetexcept(void) {
109   return 0;
110 }
111