blob: f5b2828e1e6d502930f36e2ce62085794ad1692d [file] [log] [blame]
Dan Albert75097b12016-11-04 11:39:16 -07001/*-
2 * Copyright (c) 2004 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.c,v 1.1 2004/06/06 10:03:59 das Exp $
27 */
28
29#ifndef ANDROID_LEGACY_FENV_INLINES_ARM_H
30#define ANDROID_LEGACY_FENV_INLINES_ARM_H
31
Dan Albertf945fb62017-10-17 11:07:26 -070032#include <sys/cdefs.h>
Dan Albert75097b12016-11-04 11:39:16 -070033
Elliott Hughes5bc78c82016-11-16 11:35:43 -080034#if __ANDROID_API__ < __ANDROID_API_L__ && defined(__arm__)
Dan Albert75097b12016-11-04 11:39:16 -070035
Dan Albertf945fb62017-10-17 11:07:26 -070036#include <fenv.h>
37
Dan Albert75097b12016-11-04 11:39:16 -070038__BEGIN_DECLS
39
40#define FPSCR_ENABLE_SHIFT 8
41#define FPSCR_ENABLE_MASK (FE_ALL_EXCEPT << FPSCR_ENABLE_SHIFT)
42
43#define FPSCR_RMODE_SHIFT 22
44
45static __inline int fegetenv(fenv_t* __envp) {
46 fenv_t _fpscr;
47 __asm__ __volatile__("vmrs %0,fpscr" : "=r" (_fpscr));
48 *__envp = _fpscr;
49 return 0;
50}
51
52static __inline int fesetenv(const fenv_t* __envp) {
53 fenv_t _fpscr = *__envp;
54 __asm__ __volatile__("vmsr fpscr,%0" : :"ri" (_fpscr));
55 return 0;
56}
57
58static __inline int feclearexcept(int __excepts) {
59 fexcept_t __fpscr;
60 fegetenv(&__fpscr);
61 __fpscr &= ~__excepts;
62 fesetenv(&__fpscr);
63 return 0;
64}
65
66static __inline int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
67 fexcept_t __fpscr;
68 fegetenv(&__fpscr);
69 *__flagp = __fpscr & __excepts;
70 return 0;
71}
72
73static __inline int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
74 fexcept_t __fpscr;
75 fegetenv(&__fpscr);
76 __fpscr &= ~__excepts;
77 __fpscr |= *__flagp & __excepts;
78 fesetenv(&__fpscr);
79 return 0;
80}
81
82static __inline int feraiseexcept(int __excepts) {
83 fexcept_t __ex = __excepts;
84 fesetexceptflag(&__ex, __excepts);
85 return 0;
86}
87
88static __inline int fetestexcept(int __excepts) {
89 fexcept_t __fpscr;
90 fegetenv(&__fpscr);
91 return (__fpscr & __excepts);
92}
93
94static __inline int fegetround(void) {
95 fenv_t _fpscr;
96 fegetenv(&_fpscr);
97 return ((_fpscr >> FPSCR_RMODE_SHIFT) & 0x3);
98}
99
100static __inline int fesetround(int __round) {
101 fenv_t _fpscr;
102 fegetenv(&_fpscr);
103 _fpscr &= ~(0x3 << FPSCR_RMODE_SHIFT);
104 _fpscr |= (__round << FPSCR_RMODE_SHIFT);
105 fesetenv(&_fpscr);
106 return 0;
107}
108
109static __inline int feholdexcept(fenv_t* __envp) {
110 fenv_t __env;
111 fegetenv(&__env);
112 *__envp = __env;
113 __env &= ~(FE_ALL_EXCEPT | FPSCR_ENABLE_MASK);
114 fesetenv(&__env);
115 return 0;
116}
117
118static __inline int feupdateenv(const fenv_t* __envp) {
119 fexcept_t __fpscr;
120 fegetenv(&__fpscr);
121 fesetenv(__envp);
122 feraiseexcept(__fpscr & FE_ALL_EXCEPT);
123 return 0;
124}
125
126static __inline int feenableexcept(int __mask) {
127 fenv_t __old_fpscr, __new_fpscr;
128 fegetenv(&__old_fpscr);
129 __new_fpscr = __old_fpscr | (__mask & FE_ALL_EXCEPT) << FPSCR_ENABLE_SHIFT;
130 fesetenv(&__new_fpscr);
131 return ((__old_fpscr >> FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
132}
133
134static __inline int fedisableexcept(int __mask) {
135 fenv_t __old_fpscr, __new_fpscr;
136 fegetenv(&__old_fpscr);
137 __new_fpscr = __old_fpscr & ~((__mask & FE_ALL_EXCEPT) << FPSCR_ENABLE_SHIFT);
138 fesetenv(&__new_fpscr);
139 return ((__old_fpscr >> FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
140}
141
142static __inline int fegetexcept(void) {
143 fenv_t __fpscr;
144 fegetenv(&__fpscr);
145 return ((__fpscr & FPSCR_ENABLE_MASK) >> FPSCR_ENABLE_SHIFT);
146}
147
148#undef FPSCR_ENABLE_SHIFT
149#undef FPSCR_ENABLE_MASK
150#undef FPSCR_RMODE_SHIFT
151
152__END_DECLS
153
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800154#endif /* __ANDROID_API__ < __ANDROID_API_L__ && defined(__arm__) */
Dan Albert75097b12016-11-04 11:39:16 -0700155
156#endif /* ANDROID_LEGACY_FENV_INLINES_ARM_H */