blob: 893bc300900ecd7f32c9f598656989f286524ee0 [file] [log] [blame]
Chris Dearman726800e2012-05-01 17:45:53 -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/mips/fenv.c,v 1.1 2008/04/26 12:20:29 imp 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 _fcsr = 0;
39#ifdef __mips_hard_float
40 __asm__ __volatile__("cfc1 %0,$31" : "=r" (_fcsr));
41#endif
42 *__envp = _fcsr;
43 return 0;
44}
45
46int fesetenv(const fenv_t* __envp) {
47 fenv_t _fcsr = *__envp;
48#ifdef __mips_hard_float
49 __asm__ __volatile__("ctc1 %0,$31" : : "r" (_fcsr));
50#endif
51 return 0;
52}
53
54int feclearexcept(int __excepts) {
55 fexcept_t __fcsr;
56 fegetenv(&__fcsr);
57 __excepts &= FE_ALL_EXCEPT;
58 __fcsr &= ~(__excepts | (__excepts << _FCSR_CAUSE_SHIFT));
59 fesetenv(&__fcsr);
60 return 0;
61}
62
63int fegetexceptflag(fexcept_t* __flagp, int __excepts) {
64 fexcept_t __fcsr;
65 fegetenv(&__fcsr);
66 *__flagp = __fcsr & __excepts & FE_ALL_EXCEPT;
67 return 0;
68}
69
70int fesetexceptflag(const fexcept_t* __flagp, int __excepts) {
71 fexcept_t __fcsr;
72 fegetenv(&__fcsr);
73 /* Ensure that flags are all legal */
74 __excepts &= FE_ALL_EXCEPT;
75 __fcsr &= ~__excepts;
76 __fcsr |= *__flagp & __excepts;
77 fesetenv(&__fcsr);
78 return 0;
79}
80
81int feraiseexcept(int __excepts) {
82 fexcept_t __fcsr;
83 fegetenv(&__fcsr);
84 /* Ensure that flags are all legal */
85 __excepts &= FE_ALL_EXCEPT;
86 /* Cause bit needs to be set as well for generating the exception*/
87 __fcsr |= __excepts | (__excepts << _FCSR_CAUSE_SHIFT);
88 fesetenv(&__fcsr);
89 return 0;
90}
91
92int fetestexcept(int __excepts) {
93 fexcept_t __FCSR;
94 fegetenv(&__FCSR);
95 return (__FCSR & __excepts & FE_ALL_EXCEPT);
96}
97
98int fegetround(void) {
99 fenv_t _fcsr;
100 fegetenv(&_fcsr);
101 return (_fcsr & _FCSR_RMASK);
102}
103
104int fesetround(int __round) {
105 fenv_t _fcsr;
106 fegetenv(&_fcsr);
107 _fcsr &= ~_FCSR_RMASK;
108 _fcsr |= (__round & _FCSR_RMASK ) ;
109 fesetenv(&_fcsr);
110 return 0;
111}
112
113int feholdexcept(fenv_t* __envp) {
114 fenv_t __env;
115 fegetenv(&__env);
116 *__envp = __env;
117 __env &= ~(FE_ALL_EXCEPT | _FCSR_ENABLE_MASK);
118 fesetenv(&__env);
119 return 0;
120}
121
122int feupdateenv(const fenv_t* __envp) {
123 fexcept_t __fcsr;
124 fegetenv(&__fcsr);
125 fesetenv(__envp);
126 feraiseexcept(__fcsr & FE_ALL_EXCEPT);
127 return 0;
128}
129
130int feenableexcept(int __mask) {
131 fenv_t __old_fcsr, __new_fcsr;
132 fegetenv(&__old_fcsr);
133 __new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
134 fesetenv(&__new_fcsr);
135 return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
136}
137
138int fedisableexcept(int __mask) {
139 fenv_t __old_fcsr, __new_fcsr;
140 fegetenv(&__old_fcsr);
141 __new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
142 fesetenv(&__new_fcsr);
143 return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
144}
145
146int fegetexcept(void) {
147 fenv_t __fcsr;
148 fegetenv(&__fcsr);
149 return ((__fcsr & _FCSR_ENABLE_MASK) >> _ENABLE_SHIFT);
150}