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