PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
fenv-fp.h
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31__fenv_static inline int
32feclearexcept(int excepts)
33{
34 fexcept_t fcsr;
35
36 excepts &= FE_ALL_EXCEPT;
37 __cfc1(fcsr);
38 fcsr &= ~(excepts | (excepts << _FCSR_CAUSE_SHIFT));
39 __ctc1(fcsr);
40
41 return (0);
42}
43
44__fenv_static inline int
45fegetexceptflag(fexcept_t *flagp, int excepts)
46{
47 fexcept_t fcsr;
48
49 excepts &= FE_ALL_EXCEPT;
50 __cfc1(fcsr);
51 *flagp = fcsr & excepts;
52
53 return (0);
54}
55
56__fenv_static inline int
57fesetexceptflag(const fexcept_t *flagp, int excepts)
58{
59 fexcept_t fcsr;
60
61 excepts &= FE_ALL_EXCEPT;
62 __cfc1(fcsr);
63 fcsr &= ~excepts;
64 fcsr |= *flagp & excepts;
65 __ctc1(fcsr);
66
67 return (0);
68}
69
70__fenv_static inline int
71feraiseexcept(int excepts)
72{
73 fexcept_t fcsr;
74
75 excepts &= FE_ALL_EXCEPT;
76 __cfc1(fcsr);
77 fcsr |= excepts | (excepts << _FCSR_CAUSE_SHIFT);
78 __ctc1(fcsr);
79
80 return (0);
81}
82
83__fenv_static inline int
84fetestexcept(int excepts)
85{
86 fexcept_t fcsr;
87
88 excepts &= FE_ALL_EXCEPT;
89 __cfc1(fcsr);
90
91 return (fcsr & excepts);
92}
93
94__fenv_static inline int
95fegetround(void)
96{
97 fexcept_t fcsr;
98
99 __cfc1(fcsr);
100
101 return (fcsr & _ROUND_MASK);
102}
103
104__fenv_static inline int
105fesetround(int rounding_mode)
106{
107 fexcept_t fcsr;
108
109 if (rounding_mode & ~_ROUND_MASK)
110 return (-1);
111
112 __cfc1(fcsr);
113 fcsr &= ~_ROUND_MASK;
114 fcsr |= rounding_mode;
115 __ctc1(fcsr);
116
117 return (0);
118}
119
120__fenv_static inline int
121fegetenv(fenv_t *envp)
122{
123
124 __cfc1(*envp);
125
126 return (0);
127}
128
129__fenv_static inline int
130feholdexcept(fenv_t *envp)
131{
132 fexcept_t fcsr;
133
134 __cfc1(fcsr);
135 *envp = fcsr;
136 fcsr &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
137 __ctc1(fcsr);
138
139 return (0);
140}
141
142__fenv_static inline int
143fesetenv(const fenv_t *envp)
144{
145
146 __ctc1(*envp);
147
148 return (0);
149}
150
151__fenv_static inline int
152feupdateenv(const fenv_t *envp)
153{
154 fexcept_t fcsr;
155
156 __cfc1(fcsr);
157 fesetenv(envp);
158 feraiseexcept(fcsr);
159
160 return (0);
161}
162#if __BSD_VISIBLE
163
164/* We currently provide no external definitions of the functions below. */
165
166#ifdef __mips_soft_float
167int feenableexcept(int __mask);
168int fedisableexcept(int __mask);
169int fegetexcept(void);
170#else
171static inline int
172feenableexcept(int __mask)
173{
174 fenv_t __old_fcsr, __new_fcsr;
175
176 __cfc1(__old_fcsr);
177 __new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
178 __ctc1(__new_fcsr);
179
180 return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
181}
182
183static inline int
184fedisableexcept(int __mask)
185{
186 fenv_t __old_fcsr, __new_fcsr;
187
188 __cfc1(__old_fcsr);
189 __new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
190 __ctc1(__new_fcsr);
191
192 return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
193}
194
195static inline int
196fegetexcept(void)
197{
198 fexcept_t fcsr;
199
200 __cfc1(fcsr);
201
202 return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
203}
204
205#endif /* !__mips_soft_float */
206
207#endif /* __BSD_VISIBLE */