blob: 6f2c9599c6c8c42a95722d797815efb2f54557f2 [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 Albert989d8042018-01-18 19:38:50 +000032#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 Albert989d8042018-01-18 19:38:50 +000036#include <fenv.h>
37
Dan Albert75097b12016-11-04 11:39:16 -070038__BEGIN_DECLS
39
Dan Albert75097b12016-11-04 11:39:16 -070040#define FPSCR_RMODE_SHIFT 22
41
42static __inline int fegetenv(fenv_t* __envp) {
43 fenv_t _fpscr;
44 __asm__ __volatile__("vmrs %0,fpscr" : "=r" (_fpscr));
45 *__envp = _fpscr;
46 return 0;
47}
48
49static __inline int fesetenv(const fenv_t* __envp) {
50 fenv_t _fpscr = *__envp;
51 __asm__ __volatile__("vmsr fpscr,%0" : :"ri" (_fpscr));
52 return 0;
53}
54
55static __inline int feclearexcept(int __excepts) {
56 fexcept_t __fpscr;
57 fegetenv(&__fpscr);
58 __fpscr &= ~__excepts;
59 fesetenv(&__fpscr);
60 return 0;
61}
62
63static __inline int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
64 fexcept_t __fpscr;
65 fegetenv(&__fpscr);
66 *__flagp = __fpscr & __excepts;
67 return 0;
68}
69
70static __inline int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
71 fexcept_t __fpscr;
72 fegetenv(&__fpscr);
73 __fpscr &= ~__excepts;
74 __fpscr |= *__flagp & __excepts;
75 fesetenv(&__fpscr);
76 return 0;
77}
78
79static __inline int feraiseexcept(int __excepts) {
80 fexcept_t __ex = __excepts;
81 fesetexceptflag(&__ex, __excepts);
82 return 0;
83}
84
85static __inline int fetestexcept(int __excepts) {
86 fexcept_t __fpscr;
87 fegetenv(&__fpscr);
88 return (__fpscr & __excepts);
89}
90
91static __inline int fegetround(void) {
92 fenv_t _fpscr;
93 fegetenv(&_fpscr);
94 return ((_fpscr >> FPSCR_RMODE_SHIFT) & 0x3);
95}
96
97static __inline int fesetround(int __round) {
98 fenv_t _fpscr;
99 fegetenv(&_fpscr);
100 _fpscr &= ~(0x3 << FPSCR_RMODE_SHIFT);
101 _fpscr |= (__round << FPSCR_RMODE_SHIFT);
102 fesetenv(&_fpscr);
103 return 0;
104}
105
106static __inline int feholdexcept(fenv_t* __envp) {
107 fenv_t __env;
108 fegetenv(&__env);
109 *__envp = __env;
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700110 __env &= ~FE_ALL_EXCEPT;
Dan Albert75097b12016-11-04 11:39:16 -0700111 fesetenv(&__env);
112 return 0;
113}
114
115static __inline int feupdateenv(const fenv_t* __envp) {
116 fexcept_t __fpscr;
117 fegetenv(&__fpscr);
118 fesetenv(__envp);
119 feraiseexcept(__fpscr & FE_ALL_EXCEPT);
120 return 0;
121}
122
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700123static __inline int feenableexcept(int __mask __unused) {
124 return -1;
Dan Albert75097b12016-11-04 11:39:16 -0700125}
126
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700127static __inline int fedisableexcept(int __mask __unused) {
128 return 0;
Dan Albert75097b12016-11-04 11:39:16 -0700129}
130
131static __inline int fegetexcept(void) {
Elliott Hughesb6c7f6e2017-11-03 16:46:32 -0700132 return 0;
Dan Albert75097b12016-11-04 11:39:16 -0700133}
134
Dan Albert75097b12016-11-04 11:39:16 -0700135#undef FPSCR_RMODE_SHIFT
136
137__END_DECLS
138
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800139#endif /* __ANDROID_API__ < __ANDROID_API_L__ && defined(__arm__) */
Dan Albert75097b12016-11-04 11:39:16 -0700140
141#endif /* ANDROID_LEGACY_FENV_INLINES_ARM_H */