xref: /aosp_15_r20/bionic/libm/fenv-arm.c (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*-
2  * Copyright (c) 2004 David Schultz <[email protected]>
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 #include <fenv.h>
30 
31 #define FPSCR_RMODE_SHIFT 22
32 
33 const fenv_t __fe_dfl_env = 0;
34 
fegetenv(fenv_t * __envp)35 int fegetenv(fenv_t* __envp) {
36   fenv_t _fpscr;
37   __asm__ __volatile__("vmrs %0,fpscr" : "=r"(_fpscr));
38   *__envp = _fpscr;
39   return 0;
40 }
41 
fesetenv(const fenv_t * __envp)42 int fesetenv(const fenv_t* __envp) {
43   fenv_t _fpscr = *__envp;
44   __asm__ __volatile__("vmsr fpscr,%0" : : "ri"(_fpscr));
45   return 0;
46 }
47 
feclearexcept(int __excepts)48 int feclearexcept(int __excepts) {
49   fexcept_t __fpscr;
50   fegetenv(&__fpscr);
51   __fpscr &= ~__excepts;
52   fesetenv(&__fpscr);
53   return 0;
54 }
55 
fegetexceptflag(fexcept_t * __flagp,int __excepts)56 int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
57   fexcept_t __fpscr;
58   fegetenv(&__fpscr);
59   *__flagp = __fpscr & __excepts;
60   return 0;
61 }
62 
fesetexceptflag(const fexcept_t * __flagp,int __excepts)63 int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
64   fexcept_t __fpscr;
65   fegetenv(&__fpscr);
66   __fpscr &= ~__excepts;
67   __fpscr |= *__flagp & __excepts;
68   fesetenv(&__fpscr);
69   return 0;
70 }
71 
feraiseexcept(int __excepts)72 int feraiseexcept(int __excepts) {
73   fexcept_t __ex = __excepts;
74   fesetexceptflag(&__ex, __excepts);
75   return 0;
76 }
77 
fetestexcept(int __excepts)78 int fetestexcept(int __excepts) {
79   fexcept_t __fpscr;
80   fegetenv(&__fpscr);
81   return (__fpscr & __excepts);
82 }
83 
fegetround(void)84 int fegetround(void) {
85   fenv_t _fpscr;
86   fegetenv(&_fpscr);
87   return ((_fpscr >> FPSCR_RMODE_SHIFT) & 0x3);
88 }
89 
fesetround(int __round)90 int fesetround(int __round) {
91   fenv_t _fpscr;
92   fegetenv(&_fpscr);
93   _fpscr &= ~(0x3 << FPSCR_RMODE_SHIFT);
94   _fpscr |= (__round << FPSCR_RMODE_SHIFT);
95   fesetenv(&_fpscr);
96   return 0;
97 }
98 
feholdexcept(fenv_t * __envp)99 int feholdexcept(fenv_t* __envp) {
100   fenv_t __env;
101   fegetenv(&__env);
102   *__envp = __env;
103   __env &= ~FE_ALL_EXCEPT;
104   fesetenv(&__env);
105   return 0;
106 }
107 
feupdateenv(const fenv_t * __envp)108 int feupdateenv(const fenv_t* __envp) {
109   fexcept_t __fpscr;
110   fegetenv(&__fpscr);
111   fesetenv(__envp);
112   feraiseexcept(__fpscr & FE_ALL_EXCEPT);
113   return 0;
114 }
115 
feenableexcept(int __mask __unused)116 int feenableexcept(int __mask __unused) {
117   return -1;
118 }
119 
fedisableexcept(int __mask __unused)120 int fedisableexcept(int __mask __unused) {
121   return 0;
122 }
123 
fegetexcept(void)124 int fegetexcept(void) {
125   return 0;
126 }
127