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
feclearexcept(int excepts)32 feclearexcept(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
fegetexceptflag(fexcept_t * flagp,int excepts)45 fegetexceptflag(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
fesetexceptflag(const fexcept_t * flagp,int excepts)57 fesetexceptflag(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
feraiseexcept(int excepts)71 feraiseexcept(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
fetestexcept(int excepts)84 fetestexcept(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
fegetround(void)95 fegetround(void)
96 {
97 	fexcept_t fcsr;
98 
99 	__cfc1(fcsr);
100 
101 	return (fcsr & _ROUND_MASK);
102 }
103 
104 __fenv_static inline int
fesetround(int rounding_mode)105 fesetround(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
fegetenv(fenv_t * envp)121 fegetenv(fenv_t *envp)
122 {
123 
124 	__cfc1(*envp);
125 
126 	return (0);
127 }
128 
129 __fenv_static inline int
feholdexcept(fenv_t * envp)130 feholdexcept(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
fesetenv(const fenv_t * envp)143 fesetenv(const fenv_t *envp)
144 {
145 
146 	__ctc1(*envp);
147 
148 	return (0);
149 }
150 
151 __fenv_static inline int
feupdateenv(const fenv_t * envp)152 feupdateenv(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 
163 #if __BSD_VISIBLE
164 
165 /* We currently provide no external definitions of the functions below. */
166 
167 __fenv_static inline int
feenableexcept(int __mask)168 feenableexcept(int __mask)
169 {
170 	fenv_t __old_fcsr, __new_fcsr;
171 
172 	__cfc1(__old_fcsr);
173 	__new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
174 	__ctc1(__new_fcsr);
175 
176 	return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
177 }
178 
179 __fenv_static inline int
fedisableexcept(int __mask)180 fedisableexcept(int __mask)
181 {
182 	fenv_t __old_fcsr, __new_fcsr;
183 
184 	__cfc1(__old_fcsr);
185 	__new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
186 	__ctc1(__new_fcsr);
187 
188 	return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
189 }
190 
191 __fenv_static inline int
fegetexcept(void)192 fegetexcept(void)
193 {
194 	fexcept_t fcsr;
195 
196 	__cfc1(fcsr);
197 
198 	return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
199 }
200 
201 #endif /* __BSD_VISIBLE */
202