1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5 * Copyright (c) 2013 Andrew Turner <andrew@FreeBSD.ORG>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32 #define _SH_FPU_MASK_SHIFT 5
33 #define _SH_FPU_ROUND_MASK 3
34 #define _sh_get_fpscr(fpscr) __asm__ volatile ("sts fpscr,%0" : "=r" (fpscr))
35 #define _sh_set_fpscr(fpscr) __asm__ volatile ("lds %0,fpscr" : : "r" (fpscr))
36
feclearexcept(int excepts)37 __declare_fenv_inline(int) feclearexcept(int excepts)
38 {
39 fexcept_t fpscr;
40
41 _sh_get_fpscr(fpscr);
42 fpscr &= ~excepts;
43 _sh_set_fpscr(fpscr);
44 return (0);
45 }
46
fedisableexcept(int __mask)47 __declare_fenv_inline(int) fedisableexcept(int __mask)
48 {
49 fenv_t __old_fpsr, __new_fpsr;
50
51 _sh_get_fpscr(__old_fpsr);
52 __new_fpsr = __old_fpsr &
53 ~((__mask & FE_ALL_EXCEPT) << _SH_FPU_MASK_SHIFT);
54 _sh_set_fpscr(__new_fpsr);
55 return ((__old_fpsr >> _SH_FPU_MASK_SHIFT) & FE_ALL_EXCEPT);
56 }
57
feenableexcept(int __mask)58 __declare_fenv_inline(int) feenableexcept(int __mask)
59 {
60 fenv_t __old_fpsr, __new_fpsr, __check_fpsr;
61
62 _sh_get_fpscr(__old_fpsr);
63 __new_fpsr = __old_fpsr |
64 ((__mask & FE_ALL_EXCEPT) << _SH_FPU_MASK_SHIFT);
65 _sh_set_fpscr(__new_fpsr);
66 _sh_get_fpscr(__check_fpsr);
67 if (__check_fpsr != __new_fpsr)
68 return -1;
69 return ((__old_fpsr >> _SH_FPU_MASK_SHIFT) & FE_ALL_EXCEPT);
70 }
71
fegetenv(fenv_t * envp)72 __declare_fenv_inline(int) fegetenv(fenv_t *envp)
73 {
74 fenv_t __env;
75 _sh_get_fpscr(__env);
76 *envp = __env;
77 return (0);
78 }
79
fegetexcept(void)80 __declare_fenv_inline(int) fegetexcept(void)
81 {
82 fenv_t fpscr;
83
84 _sh_get_fpscr(fpscr);
85 return (fpscr & FE_ALL_EXCEPT);
86 }
87
fegetexceptflag(fexcept_t * flagp,int excepts)88 __declare_fenv_inline(int) fegetexceptflag(fexcept_t *flagp, int excepts)
89 {
90 fexcept_t fpscr;
91
92 _sh_get_fpscr(fpscr);
93 *flagp = fpscr & excepts;
94 return (0);
95 }
96
fegetround(void)97 __declare_fenv_inline(int) fegetround(void)
98 {
99 fenv_t fpscr;
100
101 _sh_get_fpscr(fpscr);
102 return (fpscr & _SH_FPU_ROUND_MASK);
103 }
104
feholdexcept(fenv_t * envp)105 __declare_fenv_inline(int) feholdexcept(fenv_t *envp)
106 {
107 fenv_t __env;
108
109 _sh_get_fpscr(__env);
110 *envp = __env;
111 __env &= ~(FE_ALL_EXCEPT);
112 _sh_set_fpscr(__env);
113 return (0);
114 }
115
fesetenv(const fenv_t * envp)116 __declare_fenv_inline(int) fesetenv(const fenv_t *envp)
117 {
118
119 _sh_set_fpscr(*envp);
120 return (0);
121 }
122
fesetexceptflag(const fexcept_t * flagp,int excepts)123 __declare_fenv_inline(int) fesetexceptflag(const fexcept_t *flagp, int excepts)
124 {
125 fexcept_t fpscr;
126
127 _sh_get_fpscr(fpscr);
128 fpscr &= ~excepts;
129 fpscr |= *flagp & excepts;
130 _sh_set_fpscr(fpscr);
131 return (0);
132 }
133
feraiseexcept(int excepts)134 __declare_fenv_inline(int) feraiseexcept(int excepts)
135 {
136 fexcept_t __ex = excepts;
137
138 fesetexceptflag(&__ex, excepts);
139 return (0);
140 }
141
fesetexcept(int excepts)142 __declare_fenv_inline(int) fesetexcept(int excepts)
143 {
144 return feraiseexcept(excepts);
145 }
146
fesetround(int round)147 __declare_fenv_inline(int) fesetround(int round)
148 {
149 fenv_t fpscr;
150
151 _sh_get_fpscr(fpscr);
152 fpscr &= ~(_SH_FPU_ROUND_MASK);
153 fpscr |= round;
154 _sh_set_fpscr(fpscr);
155 return (0);
156 }
157
fetestexcept(int excepts)158 __declare_fenv_inline(int) fetestexcept(int excepts)
159 {
160 fexcept_t fpscr;
161
162 _sh_get_fpscr(fpscr);
163 return (fpscr & excepts);
164 }
165
feupdateenv(const fenv_t * envp)166 __declare_fenv_inline(int) feupdateenv(const fenv_t *envp)
167 {
168 fexcept_t fpscr;
169
170 _sh_get_fpscr(fpscr);
171 _sh_set_fpscr(*envp);
172 feraiseexcept(fpscr & FE_ALL_EXCEPT);
173 return (0);
174 }
175
176 #undef _SH_FPU_ROUND_MASK
177 #undef _SH_FPU_MASK_SHIFT
178 #undef _sh_get_fpscr
179 #undef _sh_set_fpscr
180