blob: a0108e8d63857114de2e2fdb10a309b8113b02da [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*-
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#include <fenv.h>
30
31/*
32 * Hopefully the system ID byte is immutable, so it's valid to use
33 * this as a default environment.
34 */
35const fenv_t __fe_dfl_env = 0;
Calin Juravle2d367902014-02-25 14:49:41 +000036
37int fegetenv(fenv_t* __envp) {
38 fenv_t _fpscr;
39 __asm__ __volatile__("vmrs %0,fpscr" : "=r" (_fpscr));
40 *__envp = _fpscr;
41 return 0;
42}
43
44int fesetenv(const fenv_t* __envp) {
45 fenv_t _fpscr = *__envp;
46 __asm__ __volatile__("vmsr fpscr,%0" : :"ri" (_fpscr));
47 return 0;
48}
49
50int feclearexcept(int __excepts) {
51 fexcept_t __fpscr;
52 fegetenv(&__fpscr);
53 __fpscr &= ~__excepts;
54 fesetenv(&__fpscr);
55 return 0;
56}
57
58int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
59 fexcept_t __fpscr;
60 fegetenv(&__fpscr);
61 *__flagp = __fpscr & __excepts;
62 return 0;
63}
64
65int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
66 fexcept_t __fpscr;
67 fegetenv(&__fpscr);
68 __fpscr &= ~__excepts;
69 __fpscr |= *__flagp & __excepts;
70 fesetenv(&__fpscr);
71 return 0;
72}
73
74int feraiseexcept(int __excepts) {
75 fexcept_t __ex = __excepts;
76 fesetexceptflag(&__ex, __excepts);
77 return 0;
78}
79
80int fetestexcept(int __excepts) {
81 fexcept_t __fpscr;
82 fegetenv(&__fpscr);
83 return (__fpscr & __excepts);
84}
85
86int fegetround(void) {
87 fenv_t _fpscr;
88 fegetenv(&_fpscr);
89 return ((_fpscr >> _FPSCR_RMODE_SHIFT) & 0x3);
90}
91
92int fesetround(int __round) {
93 fenv_t _fpscr;
94 fegetenv(&_fpscr);
95 _fpscr &= ~(0x3 << _FPSCR_RMODE_SHIFT);
96 _fpscr |= (__round << _FPSCR_RMODE_SHIFT);
97 fesetenv(&_fpscr);
98 return 0;
99}
100
101int feholdexcept(fenv_t* __envp) {
102 fenv_t __env;
103 fegetenv(&__env);
104 *__envp = __env;
105 __env &= ~(FE_ALL_EXCEPT | _FPSCR_ENABLE_MASK);
106 fesetenv(&__env);
107 return 0;
108}
109
110int feupdateenv(const fenv_t* __envp) {
111 fexcept_t __fpscr;
112 fegetenv(&__fpscr);
113 fesetenv(__envp);
114 feraiseexcept(__fpscr & FE_ALL_EXCEPT);
115 return 0;
116}
117
118int feenableexcept(int __mask) {
119 fenv_t __old_fpscr, __new_fpscr;
120 fegetenv(&__old_fpscr);
121 __new_fpscr = __old_fpscr | (__mask & FE_ALL_EXCEPT) << _FPSCR_ENABLE_SHIFT;
122 fesetenv(&__new_fpscr);
123 return ((__old_fpscr >> _FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
124}
125
126int fedisableexcept(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
134int fegetexcept(void) {
135 fenv_t __fpscr;
136 fegetenv(&__fpscr);
137 return ((__fpscr & _FPSCR_ENABLE_MASK) >> _FPSCR_ENABLE_SHIFT);
138}