1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010-2019 Red Hat, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_FENV_H
30 #define _SYS_FENV_H 1
31 
32 #include <sys/cdefs.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
39 /* Primary sources:
40 
41      The Open Group Base Specifications Issue 6:
42    http://www.opengroup.org/onlinepubs/000095399/basedefs/fenv.h.html
43 
44      C99 Language spec (draft n1256):
45    <url unknown>
46 
47      Intel(R) 64 and IA-32 Architectures Software Developer's Manuals:
48    http://www.intel.com/products/processor/manuals/
49 
50      GNU C library manual pages:
51    http://www.gnu.org/software/libc/manual/html_node/Control-Functions.html
52    http://www.gnu.org/software/libc/manual/html_node/Rounding.html
53    http://www.gnu.org/software/libc/manual/html_node/FP-Exceptions.html
54    http://www.gnu.org/software/libc/manual/html_node/Status-bit-operations.html
55 
56      Linux online man page(s):
57    http://linux.die.net/man/3/fegetexcept
58 
59     The documentation quotes these sources for reference.  All definitions and
60    code have been developed solely based on the information from these specs.
61 
62 */
63 
64 /*  Represents the entire floating-point environment. The floating-point
65    environment refers collectively to any floating-point status flags and
66    control modes supported by the implementation.
67     In this implementation, the struct contains the state information from
68    the fstenv/fnstenv instructions and a copy of the SSE MXCSR, since GCC
69    uses SSE for a lot of floating-point operations.  (Cygwin assumes i686
70    or above these days, as does the compiler.)  */
71 
72 typedef struct _fenv_t
73 {
74   struct _fpu_env_info {
75     unsigned int _fpu_cw;	/* low 16 bits only. */
76     unsigned int _fpu_sw;	/* low 16 bits only. */
77     unsigned int _fpu_tagw;	/* low 16 bits only. */
78     unsigned int _fpu_ipoff;
79     unsigned int _fpu_ipsel;
80     unsigned int _fpu_opoff;
81     unsigned int _fpu_opsel;	/* low 16 bits only. */
82   } _fpu;
83   unsigned int _sse_mxcsr;
84 } fenv_t;
85 
86 /*  Represents the floating-point status flags collectively, including
87    any status the implementation associates with the flags. A floating-point
88    status flag is a system variable whose value is set (but never cleared)
89    when a floating-point exception is raised, which occurs as a side effect
90    of exceptional floating-point arithmetic to provide auxiliary information.
91     A floating-point control mode is a system variable whose value may be
92    set by the user to affect the subsequent behavior of floating-point
93    arithmetic. */
94 
95 typedef __uint32_t fexcept_t;
96 
97 /*  The <fenv.h> header shall define the following constants if and only
98    if the implementation supports the floating-point exception by means
99    of the floating-point functions feclearexcept(), fegetexceptflag(),
100    feraiseexcept(), fesetexceptflag(), and fetestexcept(). Each expands to
101    an integer constant expression with values such that bitwise-inclusive
102    ORs of all combinations of the constants result in distinct values.  */
103 
104 #define FE_DIVBYZERO	(1 << 2)
105 #define FE_INEXACT	(1 << 5)
106 #define FE_INVALID	(1 << 0)
107 #define FE_OVERFLOW	(1 << 3)
108 #define FE_UNDERFLOW	(1 << 4)
109 
110 /*  The <fenv.h> header shall define the following constant, which is
111    simply the bitwise-inclusive OR of all floating-point exception
112    constants defined above:  */
113 
114 /* in agreement w/ Linux the subnormal exception will always be masked */
115 #define FE_ALL_EXCEPT \
116   (FE_INEXACT | FE_UNDERFLOW | FE_OVERFLOW | FE_DIVBYZERO | FE_INVALID)
117 
118 /*  The <fenv.h> header shall define the following constants if and only
119    if the implementation supports getting and setting the represented
120    rounding direction by means of the fegetround() and fesetround()
121    functions. Each expands to an integer constant expression whose values
122    are distinct non-negative vales.  */
123 
124 #define FE_DOWNWARD	(1)
125 #define FE_TONEAREST	(0)
126 #define FE_TOWARDZERO	(3)
127 #define FE_UPWARD	(2)
128 
129 /* Only Solaris and QNX implement fegetprec/fesetprec.  As Solaris, use the
130    values defined by http://www.open-std.org/jtc1/sc22//WG14/www/docs/n752.htm
131    QNX defines different values. */
132 #if __MISC_VISIBLE
133 #define FE_FLTPREC	(0)
134 #define FE_DBLPREC	(2)
135 #define FE_LDBLPREC	(3)
136 #endif
137 /*  Additional implementation-defined environments, with macro
138    definitions beginning with FE_ and an uppercase letter,and having
139    type "pointer to const-qualified fenv_t",may also be specified by
140    the implementation.  */
141 
142 #if __GNU_VISIBLE
143 /*  If possible, the GNU C Library defines a macro FE_NOMASK_ENV which
144    represents an environment where every exception raised causes a trap
145    to occur. You can test for this macro using #ifdef. It is only defined
146    if _GNU_SOURCE is defined.  */
147 extern const fenv_t *_fe_nomask_env;
148 #define FE_NOMASK_ENV (_fe_nomask_env)
149 #endif
150 
151 #ifdef __CYGWIN__
152 
153 #if __MISC_VISIBLE
154 int fegetprec (void);
155 int fesetprec (int __prec);
156 #endif
157 
158 #endif /* __CYGWIN__ */
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif /* _FENV_H */
165