fenv.c revision 2d367905a2e1b950f79b408141eea07c222b590b
1/*-
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: libm/aarch64/fenv.c $
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;
36
37int fegetenv(fenv_t* __envp) {
38  fenv_t _fpcr, _fpsr;
39  __asm__ __volatile__("mrs %0,fpcr" : "=r" (_fpcr));
40  __asm__ __volatile__("mrs %0,fpsr" : "=r" (_fpsr));
41  *__envp = (_fpcr | _fpsr);
42  return 0;
43}
44
45int fesetenv(const fenv_t* __envp) {
46  fenv_t _fpcr = (*__envp & FPCR_MASK);
47  fenv_t _fpsr = (*__envp & FPSR_MASK);
48  __asm__ __volatile__("msr fpcr,%0" : :"ri" (_fpcr));
49  __asm__ __volatile__("msr fpsr,%0" : :"ri" (_fpsr));
50  return 0;
51}
52
53int feclearexcept(int __excepts) {
54  fexcept_t __fpscr;
55  fegetenv(&__fpscr);
56  __fpscr &= ~__excepts;
57  fesetenv(&__fpscr);
58  return 0;
59}
60
61int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
62  fexcept_t __fpscr;
63  fegetenv(&__fpscr);
64  *__flagp = __fpscr & __excepts;
65  return 0;
66}
67
68int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
69  fexcept_t __fpscr;
70  fegetenv(&__fpscr);
71  __fpscr &= ~__excepts;
72  __fpscr |= *__flagp & __excepts;
73  fesetenv(&__fpscr);
74  return 0;
75}
76
77int feraiseexcept(int __excepts) {
78  fexcept_t __ex = __excepts;
79  fesetexceptflag(&__ex, __excepts);
80  return 0;
81}
82
83int fetestexcept(int __excepts) {
84  fexcept_t __fpscr;
85  fegetenv(&__fpscr);
86  return (__fpscr & __excepts);
87}
88
89int fegetround(void) {
90  fenv_t _fpscr;
91  fegetenv(&_fpscr);
92  return ((_fpscr >> _FPSCR_RMODE_SHIFT) & 0x3);
93}
94
95int fesetround(int __round) {
96  fenv_t _fpscr;
97  fegetenv(&_fpscr);
98  _fpscr &= ~(0x3 << _FPSCR_RMODE_SHIFT);
99  _fpscr |= (__round << _FPSCR_RMODE_SHIFT);
100  fesetenv(&_fpscr);
101  return 0;
102}
103
104int feholdexcept(fenv_t* __envp) {
105  fenv_t __env;
106  fegetenv(&__env);
107  *__envp = __env;
108  __env &= ~(FE_ALL_EXCEPT | _FPSCR_ENABLE_MASK);
109  fesetenv(&__env);
110  return 0;
111}
112
113int feupdateenv(const fenv_t* __envp) {
114  fexcept_t __fpscr;
115  fegetenv(&__fpscr);
116  fesetenv(__envp);
117  feraiseexcept(__fpscr & FE_ALL_EXCEPT);
118  return 0;
119}
120
121int feenableexcept(int __mask) {
122  fenv_t __old_fpscr, __new_fpscr;
123  fegetenv(&__old_fpscr);
124  __new_fpscr = __old_fpscr | (__mask & FE_ALL_EXCEPT) << _FPSCR_ENABLE_SHIFT;
125  fesetenv(&__new_fpscr);
126  return ((__old_fpscr >> _FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
127}
128
129int fedisableexcept(int __mask) {
130  fenv_t __old_fpscr, __new_fpscr;
131  fegetenv(&__old_fpscr);
132  __new_fpscr = __old_fpscr & ~((__mask & FE_ALL_EXCEPT) << _FPSCR_ENABLE_SHIFT);
133  fesetenv(&__new_fpscr);
134  return ((__old_fpscr >> _FPSCR_ENABLE_SHIFT) & FE_ALL_EXCEPT);
135}
136
137int fegetexcept(void) {
138  fenv_t __fpscr;
139  fegetenv(&__fpscr);
140  return ((__fpscr & _FPSCR_ENABLE_MASK) >> _FPSCR_ENABLE_SHIFT);
141}
142