fenv.h revision 1.2 1 1.2 christos /* $NetBSD: fenv.h,v 1.2 2016/08/24 06:22:20 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos *
28 1.1 christos * $FreeBSD: src/lib/msun/alpha/fenv.h,v 1.3 2005/03/16 19:03:44 das Exp $
29 1.1 christos */
30 1.1 christos
31 1.1 christos #ifndef _ALPHA_FENV_H_
32 1.1 christos #define _ALPHA_FENV_H_
33 1.1 christos
34 1.1 christos #include <sys/stdint.h>
35 1.1 christos
36 1.1 christos typedef __uint64_t fenv_t;
37 1.1 christos typedef __uint16_t fexcept_t;
38 1.1 christos
39 1.1 christos /* Exception flags */
40 1.2 christos #define FE_INVALID 0x01
41 1.2 christos #define FE_DIVBYZERO 0x02
42 1.2 christos #define FE_OVERFLOW 0x04
43 1.2 christos #define FE_UNDERFLOW 0x08
44 1.2 christos #define FE_INEXACT 0x10
45 1.2 christos #define FE_INTOVF 0x20 /* not maskable */
46 1.1 christos #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INTOVF | \
47 1.1 christos FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
48 1.1 christos
49 1.1 christos /* Rounding modes */
50 1.1 christos #define FE_TOWARDZERO 0x00
51 1.1 christos #define FE_DOWNWARD 0x01
52 1.1 christos #define FE_TONEAREST 0x02
53 1.1 christos #define FE_UPWARD 0x03
54 1.1 christos #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
55 1.1 christos FE_UPWARD | FE_TOWARDZERO)
56 1.1 christos #define _ROUND_SHIFT 58
57 1.1 christos
58 1.2 christos #define _FPUSW_SHIFT 52
59 1.1 christos
60 1.1 christos #define __excb() __asm __volatile("excb")
61 1.1 christos #define __mf_fpcr(__cw) __asm __volatile("mf_fpcr %0" : "=f" (*(__cw)))
62 1.1 christos #define __mt_fpcr(__cw) __asm __volatile("mt_fpcr %0" : : "f" (__cw))
63 1.1 christos
64 1.1 christos union __fpcr {
65 1.1 christos double __d;
66 1.1 christos fenv_t __bits;
67 1.1 christos };
68 1.1 christos
69 1.1 christos __BEGIN_DECLS
70 1.1 christos
71 1.1 christos /* Default floating-point environment */
72 1.1 christos extern const fenv_t __fe_dfl_env;
73 1.1 christos #define FE_DFL_ENV (&__fe_dfl_env)
74 1.1 christos
75 1.1 christos static __inline int
76 1.1 christos feclearexcept(int __excepts)
77 1.1 christos {
78 1.1 christos union __fpcr __r;
79 1.1 christos
80 1.1 christos __excb();
81 1.1 christos __mf_fpcr(&__r.__d);
82 1.1 christos __r.__bits &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
83 1.1 christos __mt_fpcr(__r.__d);
84 1.1 christos __excb();
85 1.1 christos return 0;
86 1.1 christos }
87 1.1 christos
88 1.1 christos static __inline int
89 1.1 christos fegetexceptflag(fexcept_t *__flagp, int __excepts)
90 1.1 christos {
91 1.1 christos union __fpcr __r;
92 1.1 christos
93 1.1 christos __excb();
94 1.1 christos __mf_fpcr(&__r.__d);
95 1.1 christos __excb();
96 1.1 christos *__flagp = (__r.__bits >> _FPUSW_SHIFT) & __excepts;
97 1.1 christos return 0;
98 1.1 christos }
99 1.1 christos
100 1.1 christos static __inline int
101 1.1 christos fesetexceptflag(const fexcept_t *__flagp, int __excepts)
102 1.1 christos {
103 1.1 christos union __fpcr __r;
104 1.1 christos fenv_t __xflag, __xexcepts;
105 1.1 christos
106 1.1 christos __xflag = (fenv_t)*__flagp << _FPUSW_SHIFT;
107 1.1 christos __xexcepts = (fenv_t)__excepts << _FPUSW_SHIFT;
108 1.1 christos __excb();
109 1.1 christos __mf_fpcr(&__r.__d);
110 1.1 christos __r.__bits &= ~__xexcepts;
111 1.1 christos __r.__bits |= __xflag & __xexcepts;
112 1.1 christos __mt_fpcr(__r.__d);
113 1.1 christos __excb();
114 1.1 christos return 0;
115 1.1 christos }
116 1.1 christos
117 1.1 christos static __inline int
118 1.1 christos feraiseexcept(int __excepts)
119 1.1 christos {
120 1.1 christos
121 1.1 christos /*
122 1.1 christos * XXX Generating exceptions this way does not actually invoke
123 1.1 christos * a userland trap handler when enabled, but neither do
124 1.1 christos * arithmetic operations as far as I can tell. Perhaps there
125 1.1 christos * are more bugs in the kernel trap handler.
126 1.1 christos */
127 1.1 christos fexcept_t __ex = __excepts;
128 1.1 christos fesetexceptflag(&__ex, __excepts);
129 1.1 christos return 0;
130 1.1 christos }
131 1.1 christos
132 1.1 christos static __inline int
133 1.1 christos fetestexcept(int __excepts)
134 1.1 christos {
135 1.1 christos union __fpcr __r;
136 1.1 christos
137 1.1 christos __excb();
138 1.1 christos __mf_fpcr(&__r.__d);
139 1.1 christos __excb();
140 1.1 christos return (__r.__bits >> _FPUSW_SHIFT) & __excepts;
141 1.1 christos }
142 1.1 christos
143 1.1 christos static __inline int
144 1.1 christos fegetround(void)
145 1.1 christos {
146 1.1 christos union __fpcr __r;
147 1.1 christos
148 1.1 christos /*
149 1.1 christos * No exception barriers should be required here if we assume
150 1.1 christos * that only fesetround() can change the rounding mode.
151 1.1 christos */
152 1.1 christos __mf_fpcr(&__r.__d);
153 1.1 christos return (int)(__r.__bits >> _ROUND_SHIFT) & _ROUND_MASK;
154 1.1 christos }
155 1.1 christos
156 1.1 christos static __inline int
157 1.1 christos fesetround(int __round)
158 1.1 christos {
159 1.1 christos union __fpcr __r;
160 1.1 christos
161 1.1 christos if (__round & ~_ROUND_MASK)
162 1.1 christos return (-1);
163 1.1 christos __excb();
164 1.1 christos __mf_fpcr(&__r.__d);
165 1.1 christos __r.__bits &= ~((fenv_t)_ROUND_MASK << _ROUND_SHIFT);
166 1.1 christos __r.__bits |= (fenv_t)__round << _ROUND_SHIFT;
167 1.1 christos __mt_fpcr(__r.__d);
168 1.1 christos __excb();
169 1.1 christos return 0;
170 1.1 christos }
171 1.1 christos
172 1.1 christos int fegetenv(fenv_t *);
173 1.1 christos int feholdexcept(fenv_t *);
174 1.1 christos int fesetenv(const fenv_t *);
175 1.1 christos int feupdateenv(const fenv_t *);
176 1.1 christos
177 1.1 christos #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
178 1.1 christos int feenableexcept(int);
179 1.1 christos int fedisableexcept(int);
180 1.1 christos int fegetexcept(void);
181 1.1 christos #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
182 1.1 christos
183 1.1 christos
184 1.1 christos __END_DECLS
185 1.1 christos
186 1.1 christos #endif /* !_ALPHA_FENV_H_ */
187