this repo has no description
1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23/*******************************************************************************
24* *
25* File: fenv.h *
26* *
27* Contains: typedefs and prototypes for C99 floating point environment. *
28* *
29*******************************************************************************/
30
31#ifndef __FENV__
32#define __FENV__
33
34#if defined( __ppc__ ) || defined( __ppc64__ )
35 #error Wrong arch. This is Intel only.
36#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/*
43 A collection of functions designed to provide access to the floating
44 point environment for numerical programming. It is compliant with
45 the floating-point requirements in C99.
46
47 Earlier versions of fenv.h were merely "modeled after" C9X. Many of the functions
48 that formerly returned ints now return void to be standard compliant.
49
50 Note: There are actually two physical floating point environments on x86. There
51 is the one described by the x87 floating point control and status words, which applies
52 primarily to calculations done with long double on MacOS X for Intel. There is the
53 MXCSR which applies primarily to calculations done with scalar float, scalar double
54 and SSE/SSE2/SSE3. The high level interface, which uses FE_ macros as int arguments
55 to configure the fexcep_t, returns and works with values that represents the logical
56 OR of these two sets of flags or masks. That is, if a flag or mask is set in either
57 environment, it will be set in fexcept_t when the state is read. Likewise, setting
58 the mask using a fexcep_t will set that mask on both environments. For this reason,
59 changing the value of the MXCSR state or floating point control/status word state on
60 your own will make the results of the functions declared in this header undefined.
61 See below for details about how and when. Exception: you may change the FZ, DAZ, DE
62 and DM bits in the MXCSR independent of this interface and retain defined behavior,
63 so long as you do not change the other bits. It is suggested that developers who wish
64 this level of control access the bits in the fenv_t directly. They are direct copies
65 of the hardware special purpose registers of similar name. Please consult appropriate
66 Intel documentation for the processor about the meaning of various bits in each register.
67
68 The file <fenv.h> declares many functions in support of numerical
69 programming. Programs that test flags or run under
70 non-default modes must do so under the effect of an enabling
71 "fenv_access" pragma.
72*/
73
74/********************************************************************************
75* *
76* fenv_t is a type for representing the entire floating-point *
77* environment in a single object. *
78* *
79* fexcept_t is a type for representing the floating-point *
80* exception flag state collectively. *
81* *
82********************************************************************************/
83typedef struct {
84 unsigned short __control; /* A direct copy of the floaing point control word */
85 unsigned short __status; /* A direct copy of the floaing point status word */
86 unsigned int __mxcsr; /* A direct copy of the MXCSR */
87 char __reserved[8]; /* Reserved for future expansion. */
88} fenv_t;
89
90typedef unsigned short fexcept_t;
91
92/* Definitions of floating-point exception macros */
93#define FE_INEXACT 0x0020
94#define FE_UNDERFLOW 0x0010
95#define FE_OVERFLOW 0x0008
96#define FE_DIVBYZERO 0x0004
97#define FE_INVALID 0x0001
98#define FE_ALL_EXCEPT 0x003D
99
100/* Definitions of rounding direction macros */
101#define FE_TONEAREST 0x0000
102#define FE_DOWNWARD 0x0400
103#define FE_UPWARD 0x0800
104#define FE_TOWARDZERO 0x0C00
105
106/* default environment object */
107extern const fenv_t _FE_DFL_ENV;
108#define FE_DFL_ENV &_FE_DFL_ENV /* pointer to default environment */
109
110/*******************************************************************************
111* A environment object that sets to defualt settings and in addition sets the *
112* FZ and DAZ bits in the MXCSR, which causes flush-to-zero behavior of *
113* denormals. When using this environment, denormals encountered by XMM based *
114* calculation (which normally should be all single and double precision scalar *
115* floating point calculations, and all SSE/SSE2/SSE3 computation) will be *
116* treated as zero. Calculation results that are denormals will also be *
117* truncated to zero. This calculation mode is not IEEE-754 compliant, but may *
118* prevent lengthy stalls that occur in code that encounters denormals. It is *
119* suggested that you do not use this mode unless you have established that *
120* denormals are causing trouble for your code. Please use wisely. *
121* *
122* CAUTION: The math library currently is not architected to do the right *
123* thing in the face of DAZ + FZ mode. For example, ceil( +denormal) returns *
124* +denormal rather than 1.0 in some versions of MacOS X. In some circumstances *
125* this may lead to unexpected application behavior. Use at your own risk. *
126* *
127* It is not possible to disable denorm stalls on calculation using the x87 FPU.*
128*******************************************************************************/
129extern const fenv_t _FE_DFL_DISABLE_SSE_DENORMS_ENV;
130#define FE_DFL_DISABLE_SSE_DENORMS_ENV &_FE_DFL_DISABLE_SSE_DENORMS_ENV
131
132/*******************************************************************************
133* The following functions provide high level access to the exception flags.*
134* The "int" input argument can be constructed by bitwise ORs of the *
135* exception macros: for example: FE_OVERFLOW | FE_INEXACT. *
136*******************************************************************************/
137
138/*******************************************************************************
139* The function "feclearexcept" clears the supported floating point *
140* exceptions represented by its argument. *
141*******************************************************************************/
142
143extern int feclearexcept(int /*excepts*/);
144
145
146/*******************************************************************************
147* The function "fegetexceptflag" stores a implementation-defined *
148* representation of the states of the floating-point status flags indicated *
149* by its integer argument excepts in the object pointed to by the argument, *
150* flagp. *
151*******************************************************************************/
152
153extern int fegetexceptflag(fexcept_t * /*flagp*/, int /*excepts*/);
154
155
156/*******************************************************************************
157* The function "feraiseexcept" raises the supported floating-point *
158* exceptions represented by its argument. The order in which these *
159* floating-point exceptions are raised is unspecified. *
160*******************************************************************************/
161
162extern int feraiseexcept(int /*excepts*/);
163
164
165/*******************************************************************************
166* The function "fesetexceptflag" sets or clears the floating point status *
167* flags indicated by the argument excepts to the states stored in the *
168* object pointed to by flagp. The value of the *flagp shall have been set *
169* by a previous call to fegetexceptflag whose second argument represented *
170* at least those floating-point exceptions represented by the argument *
171* excepts. This function does not raise floating-point exceptions; it just *
172* sets the state of the flags. *
173*******************************************************************************/
174
175extern int fesetexceptflag(const fexcept_t * /*flagp*/, int /*excepts*/);
176
177
178/*******************************************************************************
179* The function "fetestexcept" determines which of the specified subset of *
180* the floating-point exception flags are currently set. The excepts *
181* argument specifies the floating-point status flags to be queried. This *
182* function returns the value of the bitwise OR of the floating-point *
183* exception macros corresponding to the currently set floating-point *
184* exceptions included in excepts. *
185* *
186* On MacOS X for Intel, the result is the value of union of the *
187* corresponding result from the x87 and SSE floating point states. *
188*******************************************************************************/
189
190extern int fetestexcept(int /*excepts*/);
191
192
193/*******************************************************************************
194* The following functions provide control of rounding direction modes. *
195*******************************************************************************/
196
197/*******************************************************************************
198* The function "fegetround" returns the value of the rounding direction *
199* macro which represents the current rounding direction, or a negative *
200* if there is no such rounding direction macro or the current rounding *
201* direction is not determinable. *
202*******************************************************************************/
203
204extern int fegetround(void);
205
206
207/*******************************************************************************
208* The function "fesetround" establishes the rounding direction represented *
209* by its argument "round". If the argument is not equal to the value of a *
210* rounding direction macro, the rounding direction is not changed. It *
211* returns zero if and only if the argument is equal to a rounding *
212* direction macro. *
213*******************************************************************************/
214
215extern int fesetround(int /*round*/);
216
217
218/*******************************************************************************
219* The following functions manage the floating-point environment, exception *
220* flags and dynamic modes, as one entity. *
221*******************************************************************************/
222
223/*******************************************************************************
224* The fegetenv function stores the current floating-point enviornment in *
225* the object pointed to by envp. *
226*******************************************************************************/
227extern int fegetenv(fenv_t * /*envp*/);
228
229/*******************************************************************************
230* The feholdexcept function saves the current floating-point environment in *
231* the object pointed to by envp, clears the floating-point status flags, *
232* and then installs a non-stop (continue on floating-point exceptions) *
233* mode, if available, for all floating-point exceptions. The feholdexcept *
234* function returns zero if and only if non-stop floating-point exceptions *
235* handling was successfully installed. *
236*******************************************************************************/
237extern int feholdexcept(fenv_t * /*envp*/);
238
239/*******************************************************************************
240* The fesetnv function establishes the floating-point environment *
241* represented by the object pointed to by envp. The argument envp shall *
242* point to an object set by a call to fegetenv or feholdexcept, or equal to *
243* a floating-point environment macro -- we define only *FE_DFL_ENV and *
244* FE_DISABLE_SSE_DENORMS_ENV -- to be C99 standard compliant and portable *
245* to other architectures. Note that fesetnv merely installs the state of *
246* the floating-point status flags represented through its argument, and *
247* does not raise these floating-point exceptions. *
248* *
249* On MacOS X for Intel you may test and set the bits in *envp yourself, *
250* provided that you conditionalize the code appropriately to preserve *
251* portability and you follow the various strictures and suggestions *
252* provided by Intel in appropriate processor documentation. Please be aware *
253* that because there are two hardware locations for setting and reading *
254* floating point environment, this function (and others like it) are not *
255* atomic -- that is, for a brief period of time during the function call *
256* your new environment will have been applied to one but not both of the *
257* floating point engines (x87 and SSE). In addition, the behavior of some *
258* higher level interfaces (fegetround) is undefined if the x87 and SSE *
259* floating point units rounding modes are configured differently. Please *
260* use common sense. *
261*******************************************************************************/
262extern int fesetenv(const fenv_t * /*envp*/);
263
264/*******************************************************************************
265* The feupdateenv function saves the currently raised floating-point *
266* exceptions in its automatic storage, installs the floating-point *
267* environment represented by the object pointed to by envp, and then raises *
268* the saved floating-point exceptions. The argument envp shall point to an *
269* object set by a call to feholdexcept or fegetenv or equal a *
270* floating-point environment macro. *
271* *
272* Please see the description of feholdexcept for additional ways to create *
273* a fenv_t object, which are valid only for MacOS X for Intel. *
274*******************************************************************************/
275extern int feupdateenv(const fenv_t * /*envp*/);
276
277
278#ifdef __cplusplus
279}
280#endif
281
282#endif /* __FENV__ */
283