fenv.c revision 1.2 1 1.2 nakayama /* $NetBSD: fenv.c,v 1.2 2011/05/20 21:42:49 nakayama Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos */
26 1.1 christos #include <sys/cdefs.h>
27 1.2 nakayama __RCSID("$NetBSD: fenv.c,v 1.2 2011/05/20 21:42:49 nakayama Exp $");
28 1.1 christos
29 1.1 christos #include <assert.h>
30 1.1 christos #include <fenv.h>
31 1.1 christos
32 1.2 nakayama #ifdef __arch64__
33 1.2 nakayama
34 1.1 christos /* Load floating-point state register (all 64bits) */
35 1.1 christos #define __ldxfsr(__r) __asm__ __volatile__ \
36 1.1 christos ("ldx %0, %%fsr" : : "m" (__r))
37 1.1 christos
38 1.1 christos /* Save floating-point state register (all 64bits) */
39 1.1 christos #define __stxfsr(__r) __asm__ __volatile__ \
40 1.1 christos ("stx %%fsr, %0" : "=m" (*(__r)))
41 1.1 christos
42 1.2 nakayama #else /* !__arch64__ */
43 1.2 nakayama
44 1.2 nakayama /* Load floating-point state register (32bits) */
45 1.2 nakayama #define __ldxfsr(__r) __asm__ __volatile__ \
46 1.2 nakayama ("ld %0, %%fsr" : : "m" (__r))
47 1.2 nakayama
48 1.2 nakayama /* Save floating-point state register (32bits) */
49 1.2 nakayama #define __stxfsr(__r) __asm__ __volatile__ \
50 1.2 nakayama ("st %%fsr, %0" : "=m" (*(__r)))
51 1.2 nakayama
52 1.2 nakayama #endif /* __arch64__ */
53 1.2 nakayama
54 1.1 christos /*
55 1.1 christos * The feclearexcept() function clears the supported floating-point exceptions
56 1.1 christos * represented by `excepts'.
57 1.1 christos */
58 1.1 christos int
59 1.1 christos feclearexcept(int excepts)
60 1.1 christos {
61 1.1 christos fexcept_t r;
62 1.1 christos int ex;
63 1.1 christos
64 1.1 christos _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
65 1.1 christos
66 1.1 christos ex = excepts & FE_ALL_EXCEPT;
67 1.1 christos
68 1.1 christos __stxfsr(&r);
69 1.1 christos r &= ~ex;
70 1.1 christos __ldxfsr(r);
71 1.1 christos
72 1.1 christos /* Success */
73 1.1 christos return 0;
74 1.1 christos }
75 1.1 christos
76 1.1 christos /*
77 1.1 christos * The fegetexceptflag() function stores an implementation-defined
78 1.1 christos * representation of the states of the floating-point status flags indicated
79 1.1 christos * by the argument excepts in the object pointed to by the argument flagp.
80 1.1 christos */
81 1.1 christos int
82 1.1 christos fegetexceptflag(fexcept_t *flagp, int excepts)
83 1.1 christos {
84 1.1 christos fexcept_t r;
85 1.1 christos int ex;
86 1.1 christos
87 1.1 christos _DIAGASSERT(flagp != NULL);
88 1.1 christos _DIAGASSERT((excepts & ~_FE_ALL_EXCEPT) == 0);
89 1.1 christos
90 1.1 christos ex = excepts & FE_ALL_EXCEPT;
91 1.1 christos
92 1.1 christos __stxfsr(&r);
93 1.1 christos *flagp = r & ex;
94 1.1 christos
95 1.1 christos /* Success */
96 1.1 christos return 0;
97 1.1 christos }
98 1.1 christos
99 1.1 christos
100 1.1 christos /*
101 1.1 christos * This function sets the floating-point status flags indicated by the argument
102 1.1 christos * `excepts' to the states stored in the object pointed to by `flagp'. It does
103 1.1 christos * NOT raise any floating-point exceptions, but only sets the state of the flags.
104 1.1 christos */
105 1.1 christos int
106 1.1 christos fesetexceptflag(const fexcept_t *flagp, int excepts)
107 1.1 christos {
108 1.1 christos fexcept_t r;
109 1.1 christos int ex;
110 1.1 christos
111 1.1 christos _DIAGASSERT(flagp != NULL);
112 1.1 christos _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
113 1.1 christos
114 1.1 christos ex = excepts & FE_ALL_EXCEPT;
115 1.1 christos
116 1.1 christos __stxfsr(&r);
117 1.1 christos r &= ~ex;
118 1.1 christos r |= *flagp & ex;
119 1.1 christos __ldxfsr(r);
120 1.1 christos
121 1.1 christos /* Success */
122 1.1 christos return 0;
123 1.1 christos }
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * The feraiseexcept() function raises the supported floating-point exceptions
127 1.1 christos * represented by the argument `excepts'.
128 1.1 christos *
129 1.1 christos * The order in which these floating-point exceptions are raised is unspecified
130 1.1 christos * (by the standard).
131 1.1 christos */
132 1.1 christos int
133 1.1 christos feraiseexcept(int excepts)
134 1.1 christos {
135 1.1 christos volatile double d;
136 1.1 christos int ex;
137 1.1 christos
138 1.1 christos _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
139 1.1 christos
140 1.1 christos ex = excepts & FE_ALL_EXCEPT;
141 1.1 christos
142 1.1 christos /*
143 1.1 christos * With a compiler that supports the FENV_ACCESS pragma properly, simple
144 1.1 christos * expressions like '0.0 / 0.0' should be sufficient to generate traps.
145 1.1 christos * Unfortunately, we need to bring a volatile variable into the equation
146 1.1 christos * to prevent incorrect optimizations.
147 1.1 christos */
148 1.1 christos if (ex & FE_INVALID) {
149 1.1 christos d = 0.0;
150 1.1 christos d = 0.0 / d;
151 1.1 christos }
152 1.1 christos if (ex & FE_DIVBYZERO) {
153 1.1 christos d = 0.0;
154 1.1 christos d = 1.0 / d;
155 1.1 christos }
156 1.1 christos if (ex & FE_OVERFLOW) {
157 1.1 christos d = 0x1.ffp1023;
158 1.1 christos d *= 2.0;
159 1.1 christos }
160 1.1 christos if (ex & FE_UNDERFLOW) {
161 1.1 christos d = 0x1p-1022;
162 1.1 christos d /= 0x1p1023;
163 1.1 christos }
164 1.1 christos if (ex & FE_INEXACT) {
165 1.1 christos d = 0x1p-1022;
166 1.1 christos d += 1.0;
167 1.1 christos }
168 1.1 christos
169 1.1 christos /* Success */
170 1.1 christos return 0;
171 1.1 christos }
172 1.1 christos
173 1.1 christos /*
174 1.1 christos * The fetestexcept() function determines which of a specified subset of the
175 1.1 christos * floating-point exception flags are currently set. The `excepts' argument
176 1.1 christos * specifies the floating-point status flags to be queried.
177 1.1 christos */
178 1.1 christos int
179 1.1 christos fetestexcept(int excepts)
180 1.1 christos {
181 1.1 christos fexcept_t r;
182 1.1 christos
183 1.1 christos _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
184 1.1 christos
185 1.1 christos __stxfsr(&r);
186 1.1 christos
187 1.1 christos return r & (excepts & FE_ALL_EXCEPT);
188 1.1 christos }
189 1.1 christos
190 1.1 christos /*
191 1.1 christos * The fegetround() function gets the current rounding direction.
192 1.1 christos */
193 1.1 christos int
194 1.1 christos fegetround(void)
195 1.1 christos {
196 1.1 christos fenv_t r;
197 1.1 christos
198 1.1 christos __stxfsr(&r);
199 1.1 christos
200 1.1 christos return (r >> _ROUND_SHIFT) & _ROUND_MASK;
201 1.1 christos }
202 1.1 christos
203 1.1 christos /*
204 1.1 christos * The fesetround() function establishes the rounding direction represented by
205 1.1 christos * its argument `round'. If the argument is not equal to the value of a rounding
206 1.1 christos * direction macro, the rounding direction is not changed.
207 1.1 christos */
208 1.1 christos int
209 1.1 christos fesetround(int round)
210 1.1 christos {
211 1.1 christos fenv_t r;
212 1.1 christos
213 1.1 christos _DIAGASSERT((round & ~_ROUND_MASK) == 0);
214 1.1 christos if (round & ~_ROUND_MASK)
215 1.1 christos return -1;
216 1.1 christos
217 1.1 christos __stxfsr(&r);
218 1.1 christos r &= ~(_ROUND_MASK << _ROUND_SHIFT);
219 1.1 christos r |= round << _ROUND_SHIFT;
220 1.1 christos __ldxfsr(r);
221 1.1 christos
222 1.1 christos /* Success */
223 1.1 christos return 0;
224 1.1 christos }
225 1.1 christos
226 1.1 christos /*
227 1.1 christos * The fegetenv() function attempts to store the current floating-point
228 1.1 christos * environment in the object pointed to by envp.
229 1.1 christos */
230 1.1 christos int
231 1.1 christos fegetenv(fenv_t *envp)
232 1.1 christos {
233 1.1 christos _DIAGASSERT(envp != NULL);
234 1.1 christos
235 1.1 christos __stxfsr(envp);
236 1.1 christos
237 1.1 christos /* Success */
238 1.1 christos return 0;
239 1.1 christos }
240 1.1 christos
241 1.1 christos
242 1.1 christos /*
243 1.1 christos * The feholdexcept() function saves the current floating-point environment
244 1.1 christos * in the object pointed to by envp, clears the floating-point status flags, and
245 1.1 christos * then installs a non-stop (continue on floating-point exceptions) mode, if
246 1.1 christos * available, for all floating-point exceptions.
247 1.1 christos */
248 1.1 christos int
249 1.1 christos feholdexcept(fenv_t *envp)
250 1.1 christos {
251 1.1 christos fenv_t r;
252 1.1 christos
253 1.1 christos _DIAGASSERT(envp != NULL);
254 1.1 christos
255 1.1 christos __stxfsr(&r);
256 1.1 christos *envp = r;
257 1.1 christos r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
258 1.1 christos __ldxfsr(r);
259 1.1 christos
260 1.1 christos /* Success */
261 1.1 christos return 0;
262 1.1 christos }
263 1.1 christos
264 1.1 christos /*
265 1.1 christos * The fesetenv() function attempts to establish the floating-point environment
266 1.1 christos * represented by the object pointed to by envp. The argument `envp' points
267 1.1 christos * to an object set by a call to fegetenv() or feholdexcept(), or equal a
268 1.1 christos * floating-point environment macro. The fesetenv() function does not raise
269 1.1 christos * floating-point exceptions, but only installs the state of the floating-point
270 1.1 christos * status flags represented through its argument.
271 1.1 christos */
272 1.1 christos int
273 1.1 christos fesetenv(const fenv_t *envp)
274 1.1 christos {
275 1.1 christos _DIAGASSERT(envp != NULL);
276 1.1 christos
277 1.1 christos __ldxfsr(*envp);
278 1.1 christos
279 1.1 christos /* Success */
280 1.1 christos return 0;
281 1.1 christos }
282 1.1 christos
283 1.1 christos
284 1.1 christos /*
285 1.1 christos * The feupdateenv() function saves the currently raised floating-point
286 1.1 christos * exceptions in its automatic storage, installs the floating-point environment
287 1.1 christos * represented by the object pointed to by `envp', and then raises the saved
288 1.1 christos * floating-point exceptions. The argument `envp' shall point to an object set
289 1.1 christos * by a call to feholdexcept() or fegetenv(), or equal a floating-point
290 1.1 christos * environment macro.
291 1.1 christos */
292 1.1 christos int
293 1.1 christos feupdateenv(const fenv_t *envp)
294 1.1 christos {
295 1.1 christos fexcept_t r;
296 1.1 christos
297 1.1 christos _DIAGASSERT(envp != NULL);
298 1.1 christos
299 1.1 christos __stxfsr(&r);
300 1.1 christos __ldxfsr(*envp);
301 1.1 christos
302 1.1 christos _DIAGASSERT((r & ~FE_ALL_EXCEPT) == 0);
303 1.1 christos feraiseexcept(r & FE_ALL_EXCEPT);
304 1.1 christos
305 1.1 christos /* Success */
306 1.1 christos return 0;
307 1.1 christos }
308 1.1 christos
309 1.1 christos /*
310 1.1 christos * The following functions are extentions to the standard
311 1.1 christos */
312 1.1 christos int
313 1.1 christos feenableexcept(int mask)
314 1.1 christos {
315 1.1 christos fenv_t old_r, new_r;
316 1.1 christos
317 1.1 christos __stxfsr(&old_r);
318 1.1 christos new_r = old_r | ((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
319 1.1 christos __ldxfsr(new_r);
320 1.1 christos
321 1.1 christos return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
322 1.1 christos }
323 1.1 christos
324 1.1 christos int
325 1.1 christos fedisableexcept(int mask)
326 1.1 christos {
327 1.1 christos fenv_t old_r, new_r;
328 1.1 christos
329 1.1 christos __stxfsr(&old_r);
330 1.1 christos new_r = old_r & ~((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
331 1.1 christos __ldxfsr(new_r);
332 1.1 christos
333 1.1 christos return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
334 1.1 christos }
335 1.1 christos
336 1.1 christos int
337 1.1 christos fegetexcept(void)
338 1.1 christos {
339 1.1 christos fenv_t r;
340 1.1 christos
341 1.1 christos __stxfsr(&r);
342 1.1 christos return (r & _ENABLE_MASK) >> _FPUSW_SHIFT;
343 1.1 christos }
344