blob: 32c3b1d06cf3bc098997c868aed24a4678906b0f [file] [log] [blame]
Serban Constantinescu1c4f1012013-10-11 10:44:43 +01001/*-
2 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/lib/msun/arm/fenv.h,v 1.5 2005/03/16 19:03:45 das Exp $
27 */
28
29/*
30 * Rewritten for Android.
31 *
32 * The ARM FPSCR (Floating-point Status and Control Register) described here:
33 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0344b/Chdfafia.html
34 * has been split into the FPCR (Floating-point Control Register) and FPSR
35 * (Floating-point Status Register) on the ARMv8. These are described briefly in
36 * "Procedure Call Standard for the ARM 64-bit Architecture"
37 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055a/IHI0055A_aapcs64.pdf
38 * section 5.1.2 SIMD and Floating-Point Registers
39 */
40
41#ifndef _FENV_H_
42#define _FENV_H_
43
44#include <sys/types.h>
45
46__BEGIN_DECLS
47
48typedef __uint32_t fenv_t;
49typedef __uint32_t fexcept_t;
50
51/* Exception flags. */
52#define FE_INVALID 0x01
53#define FE_DIVBYZERO 0x02
54#define FE_OVERFLOW 0x04
55#define FE_UNDERFLOW 0x08
56#define FE_INEXACT 0x10
57#define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
58#define _FPSCR_ENABLE_SHIFT 8
59#define _FPSCR_ENABLE_MASK (FE_ALL_EXCEPT << _FPSCR_ENABLE_SHIFT)
60
61/* Rounding modes. */
62#define FE_TONEAREST 0x0
63#define FE_UPWARD 0x1
64#define FE_DOWNWARD 0x2
65#define FE_TOWARDZERO 0x3
66#define _FPSCR_RMODE_SHIFT 22
67
68#define FPCR_IOE (1 << 8)
69#define FPCR_DZE (1 << 9)
70#define FPCR_OFE (1 << 10)
71#define FPCR_UFE (1 << 11)
72#define FPCR_IXE (1 << 12)
73#define FPCR_IDE (1 << 15)
74#define FPCR_LEN (7 << 16)
75#define FPCR_STRIDE (3 << 20)
76#define FPCR_RMODE (3 << 22)
77#define FPCR_FZ (1 << 24)
78#define FPCR_DN (1 << 25)
79#define FPCR_AHP (1 << 26)
80#define FPCR_MASK (FPCR_IOE | \
81 FPCR_DZE | \
82 FPCR_OFE | \
83 FPCR_UFE | \
84 FPCR_IXE | \
85 FPCR_IDE | \
86 FPCR_LEN | \
87 FPCR_STRIDE | \
88 FPCR_RMODE | \
89 FPCR_FZ | \
90 FPCR_DN | \
91 FPCR_AHP )
92
93#define FPSR_IOC (1 << 0)
94#define FPSR_DZC (1 << 1)
95#define FPSR_OFC (1 << 2)
96#define FPSR_UFC (1 << 3)
97#define FPSR_IXC (1 << 4)
98#define FPSR_IDC (1 << 7)
99#define FPSR_QC (1 << 27)
100#define FPSR_V (1 << 28)
101#define FPSR_C (1 << 29)
102#define FPSR_Z (1 << 30)
103#define FPSR_N (1 << 31)
104#define FPSR_MASK (FPSR_IOC | \
105 FPSR_DZC | \
106 FPSR_OFC | \
107 FPSR_UFC | \
108 FPSR_IXC | \
109 FPSR_IDC | \
110 FPSR_QC | \
111 FPSR_V | \
112 FPSR_C | \
113 FPSR_Z | \
114 FPSR_N )
115
116/* Default floating-point environment. */
117extern const fenv_t __fe_dfl_env;
118#define FE_DFL_ENV (&__fe_dfl_env)
119
120static __inline int fegetenv(fenv_t* __envp) {
121 fenv_t _fpcr, _fpsr;
122 __asm__ __volatile__("mrs %0,fpcr" : "=r" (_fpcr));
123 __asm__ __volatile__("mrs %0,fpsr" : "=r" (_fpsr));
124 *__envp = (_fpcr | _fpsr);
125 return 0;
126}
127
128static __inline int fesetenv(const fenv_t* __envp) {
129 fenv_t _fpcr = (*__envp & FPCR_MASK);
130 fenv_t _fpsr = (*__envp & FPSR_MASK);
131 __asm__ __volatile__("msr fpcr,%0" : :"ri" (_fpcr));
132 __asm__ __volatile__("msr fpsr,%0" : :"ri" (_fpsr));
133 return 0;
134}
135
136static __inline int feclearexcept(int __excepts) {
137 fexcept_t __fpscr;
138 fegetenv(&__fpscr);
139 __fpscr &= ~__excepts;
140 fesetenv(&__fpscr);
141 return 0;
142}
143
144static __inline int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
145 fexcept_t __fpscr;
146 fegetenv(&__fpscr);
147 *__flagp = __fpscr & __excepts;
148 return 0;
149}
150
151static __inline int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
152 fexcept_t __fpscr;
153 fegetenv(&__fpscr);
154 __fpscr &= ~__excepts;
155 __fpscr |= *__flagp & __excepts;
156 fesetenv(&__fpscr);
157 return 0;
158}
159
160static __inline int feraiseexcept(int __excepts) {
161 fexcept_t __ex = __excepts;
162 fesetexceptflag(&__ex, __excepts);
163 return 0;
164}
165
166static __inline int fetestexcept(int __excepts) {
167 fexcept_t __fpscr;
168 fegetenv(&__fpscr);
169 return (__fpscr & __excepts);
170}
171
172static __inline int fegetround(void) {
173 fenv_t _fpscr;
174 fegetenv(&_fpscr);
175 return ((_fpscr >> _FPSCR_RMODE_SHIFT) & 0x3);
176}
177
178static __inline int fesetround(int __round) {
179 fenv_t _fpscr;
180 fegetenv(&_fpscr);
181 _fpscr &= ~(0x3 << _FPSCR_RMODE_SHIFT);
182 _fpscr |= (__round << _FPSCR_RMODE_SHIFT);
183 fesetenv(&_fpscr);
184 return 0;
185}
186
187static __inline int feholdexcept(fenv_t* __envp) {
188 fenv_t __env;
189 fegetenv(&__env);
190 *__envp = __env;
191 __env &= ~(FE_ALL_EXCEPT | _FPSCR_ENABLE_MASK);
192 fesetenv(&__env);
193 return 0;
194}
195
196static __inline int feupdateenv(const fenv_t* __envp) {
197 fexcept_t __fpscr;
198 fegetenv(&__fpscr);
199 fesetenv(__envp);
200 feraiseexcept(__fpscr & FE_ALL_EXCEPT);
201 return 0;
202}
203
204#if __BSD_VISIBLE
205
206static __inline int feenableexcept(int __mask) {
207 fenv_t __old_fpscr, __new_fpscr;
208 fegetenv(&__old_fpscr);
209 __new_fpscr = __old_fpscr | (__mask & FE_ALL_EXCEPT) << _FPSCR_ENABLE_SHIFT;
210 fesetenv(&__new_fpscr);
211 return ((__old_fpscr >> _FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
212}
213
214static __inline int fedisableexcept(int __mask) {
215 fenv_t __old_fpscr, __new_fpscr;
216 fegetenv(&__old_fpscr);
217 __new_fpscr = __old_fpscr & ~((__mask & FE_ALL_EXCEPT) << _FPSCR_ENABLE_SHIFT);
218 fesetenv(&__new_fpscr);
219 return ((__old_fpscr >> _FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
220}
221
222static __inline int fegetexcept(void) {
223 fenv_t __fpscr;
224 fegetenv(&__fpscr);
225 return ((__fpscr & _FPSCR_ENABLE_MASK) >> _FPSCR_ENABLE_SHIFT);
226}
227
228#endif /* __BSD_VISIBLE */
229
230__END_DECLS
231
232#endif /* !_FENV_H_ */