Remove building with NOCRYPTO option
[minix3.git] / lib / libm / arch / aarch64 / fenv.c
blobaf9faa064295e5415cb34dc7479f0850559dd9d6
1 /* $NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $ */
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <assert.h>
38 #include <fenv.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <inttypes.h>
43 #include <aarch64/armreg.h>
45 const fenv_t __fe_dfl_env = {
46 .__fpsr = 0,
47 .__fpcr = FPCR_FZ|FPCR_DN|FPCR_RN,
51 * The feclearexcept() function shall attempt to clear the supported
52 * floating-point exceptions represented by excepts.
54 int
55 feclearexcept(int excepts)
57 #ifndef lint
58 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
59 #endif
60 unsigned int tmp = reg_fpsr_read() & ~__SHIFTIN(excepts, FPSR_CSUM);
61 reg_fpsr_write(tmp);
62 return 0;
66 * The fegetexceptflag() function shall attempt to store an
67 * implementation-defined representation of the states of the floating-point
68 * status flags indicated by the argument excepts in the object pointed to by
69 * the argument flagp.
71 int
72 fegetexceptflag(fexcept_t *flagp, int excepts)
74 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
75 *flagp = __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
76 return 0;
80 * The feraiseexcept() function shall attempt to raise the supported
81 * floating-point exceptions represented by the argument excepts. The order
82 * in which these floating-point exceptions are raised is unspecified.
84 int
85 feraiseexcept(int excepts)
87 #ifndef lint
88 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
89 #endif
90 #ifdef __SOFTFP__
91 excepts &= fpgetsticky();
93 if (excepts) {
94 siginfo_t info;
95 memset(&info, 0, sizeof info);
96 info.si_signo = SIGFPE;
97 info.si_pid = getpid();
98 info.si_uid = geteuid();
99 if (excepts & FE_UNDERFLOW)
100 info.si_code = FPE_FLTUND;
101 else if (excepts & FE_OVERFLOW)
102 info.si_code = FPE_FLTOVF;
103 else if (excepts & FE_DIVBYZERO)
104 info.si_code = FPE_FLTDIV;
105 else if (excepts & FE_INVALID)
106 info.si_code = FPE_FLTINV;
107 else if (excepts & FE_INEXACT)
108 info.si_code = FPE_FLTRES;
109 sigqueueinfo(getpid(), &info);
111 #else
112 unsigned int fpsr = reg_fpsr_read();
113 fpsr = (fpsr & ~FPSR_CSUM) | __SHIFTIN(excepts, FPSR_CSUM);
114 reg_fpsr_write(fpsr);
115 unsigned int fpcr = reg_fpcr_read();
116 fpcr = (fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM);
117 reg_fpcr_write(fpcr);
118 #endif
119 return 0;
123 * The fesetexceptflag() function shall attempt to set the floating-point
124 * status flags indicated by the argument excepts to the states stored in the
125 * object pointed to by flagp. The value pointed to by flagp shall have been
126 * set by a previous call to fegetexceptflag() whose second argument
127 * represented at least those floating-point exceptions represented by the
128 * argument excepts. This function does not raise floating-point exceptions,
129 * but only sets the state of the flags.
132 fesetexceptflag(const fexcept_t *flagp, int excepts)
134 #ifndef lint
135 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
136 #endif
137 unsigned int fpsr = reg_fpsr_read();
138 fpsr &= ~__SHIFTIN(excepts, FPSR_CSUM);
139 fpsr |= __SHIFTIN((*flagp & excepts), FPSR_CSUM);
140 reg_fpsr_write(fpsr);
141 return 0;
145 * The fetestexcept() function shall determine which of a specified subset of
146 * the floating-point exception flags are currently set. The excepts argument
147 * specifies the floating-point status flags to be queried.
150 fetestexcept(int excepts)
152 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
153 return __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
157 * The fegetround() function shall get the current rounding direction.
160 fegetround(void)
162 return __SHIFTOUT(reg_fpcr_read(), FPCR_RMODE);
166 * The fesetround() function shall establish the rounding direction represented
167 * by its argument round. If the argument is not equal to the value of a
168 * rounding direction macro, the rounding direction is not changed.
171 fesetround(int round)
173 #ifndef lint
174 _DIAGASSERT(!(round & ~__SHIFTOUT(FPCR_RMODE, FPCR_RMODE)));
175 #endif
176 unsigned int fpcr = reg_fpcr_read() & ~FPCR_RMODE;
177 fpcr |= __SHIFTIN(round, FPCR_RMODE);
178 reg_fpcr_write(fpcr);
179 return 0;
183 * The fegetenv() function shall attempt to store the current floating-point
184 * environment in the object pointed to by envp.
187 fegetenv(fenv_t *envp)
189 envp->__fpcr = reg_fpcr_read();
190 envp->__fpsr = reg_fpsr_read();
191 return 0;
195 * The feholdexcept() function shall save the current floating-point
196 * environment in the object pointed to by envp, clear the floating-point
197 * status flags, and then install a non-stop (continue on floating-point
198 * exceptions) mode, if available, for all floating-point exceptions.
201 feholdexcept(fenv_t *envp)
203 envp->__fpsr = reg_fpsr_read();
204 envp->__fpcr = reg_fpcr_read();
205 reg_fpsr_write(envp->__fpsr & ~FPSR_CSUM);
206 reg_fpcr_write(envp->__fpcr & ~FPCR_ESUM);
207 return 0;
211 * The fesetenv() function shall attempt to establish the floating-point
212 * environment represented by the object pointed to by envp. The fesetenv()
213 * function does not raise floating-point exceptions, but only installs the
214 * state of the floating-point status flags represented through its argument.
217 fesetenv(const fenv_t *envp)
219 reg_fpsr_write(envp->__fpsr);
220 return 0;
224 * The feupdateenv() function shall attempt to save the currently raised
225 * floating-point exceptions in its automatic storage, attempt to install the
226 * floating-point environment represented by the object pointed to by envp,
227 * and then attempt to raise the saved floating-point exceptions.
230 feupdateenv(const fenv_t *envp)
232 #ifndef lint
233 _DIAGASSERT(envp != NULL);
234 #endif
235 reg_fpsr_write(envp->__fpsr);
236 reg_fpcr_write(envp->__fpcr);
238 /* Success */
239 return 0;
243 feenableexcept(int excepts)
245 const uint32_t __fpcr = reg_fpcr_read();
246 reg_fpcr_write((__fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM));
247 return __SHIFTOUT(__fpcr, FPCR_ESUM);
251 fedisableexcept(int excepts)
253 const uint32_t __fpcr = reg_fpcr_read();
254 reg_fpcr_write(__fpcr & ~__SHIFTIN(excepts, FPCR_ESUM));
255 return __SHIFTOUT(__fpcr, FPCR_ESUM);
259 fegetexcept(void)
261 const uint32_t __fpcr = reg_fpcr_read();
262 return __SHIFTOUT(__fpcr, FPCR_ESUM);