473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fenv.h standard header

Hello comp.lang.c enthusiasts,

Once while in a fit of pique, I thought I'd pull a Plauger. I have come
up with an implementation of the functions declared in the standard C99
header fenv.h. I thought I'd post them and get some constructive
criticism from this crowd of pedants.

First, the fenv.h header itself:

/* fenv standard header */
#ifndef _FENV
#define _FENV
/* typedefs */
typedef int fexcept_t;
typedef struct {
fexcept_t _FE_status_flags;
int _FE_rounding_direction;
} fenv_t;
/* floating-point exception constants */
#define FE_DIVBYZERO 0x01
#define FE_INEXACT 0x02
#define FE_INVALID 0x04
#define FE_OVERFLOW 0x08
#define FE_UNDERFLOW 0x10
#define FE_ALL_EXCEPT 0x1F
/* rounding constants */
#define FE_DOWNWARD 1
#define FE_TONEAREST 2
#define FE_TOWARDZERO 3
#define FE_UPWARD 4
/* default and current floating-point environments */
#define FE_DFL_ENV _FE_default_envp
extern const fenv_t *_FE_default_envp;
extern fenv_t _FE_current;
/* function prototypes */
#ifdef __cplusplus
extern "C"
{
#endif
int feclearexcept (int);
int fegetexceptflag (fexcept_t *, int);
int feraiseexcept (int);
int fesetexceptflag (const fexcept_t *, int);
int fetestexcept (int);
int fegetround (void);
int fesetround (int);
int fegetenv (fenv_t *);
int feholdexcept (fenv_t *);
int fesetenv (const fenv_t *);
int feupdateenv (const fenv_t *);
#ifdef __cplusplus
};
#endif
#endif /* _FENV */
/* END OF FILE */

Then, the implementation:

/* fe_constants.c - constants defined in fenv.h
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
fenv_t _FE_current = { 0, 3 };
fenv_t _FE_default_env = { 0, 3 };
const fenv_t *_FE_default_envp = &_FE_default_env;
/* END OF FILE */

/* feclearexcept.c - clear floating-point exception
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (feclearexcept) (int excepts)
{
if (excepts)
_FE_current._FP_status_flags &= ~excepts;
return 0;
}
/* END OF FILE */

/* fegetenv.c - get current floating-point environment
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fegetenv) (fenv_t * envp)
{
if (envp == 0)
return 1;
/* save current FPE */
envp->_FP_status_flags = _FE_current._FP_status_flags;
envp->_FP_rounding_direction = _FE_current._FP_rounding_direction;
return 0;
}
/* END OF FILE */

/* fegetexceptflag.c - get floating-point status flags
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fegetexceptflag) (fexcept_t * flagp, int excepts)
{
if (flagp == 0)
return 1;
*flagp = (*flagp & ~excepts) | (_FE_current._FP_status_flags &
excepts);
return 0;
}
/* END OF FILE */

/* fegetround.c - get current rounding direction
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fegetround) (void)
{
return _FE_current._FP_rounding_direction;
}
/* END OF FILE */

/* feholdexcept - save current floating-point environment
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
#include <signal.h>
int (feholdexcept) (fenv_t * envp)
{
if (envp == 0)
return 1;
/* save current FPE */
envp->_FP_status_flags = _FE_current._FP_status_flags;
envp->_FP_rounding_direction = _FE_current._FP_rounding_direction;
/* clear FP status flags */
_FE_current._FP_status_flags = 0;
/* install a continue on FP exceptions mode */
signal (SIGFPE, SIG_IGN);
return 0;
}
/* END OF FILE */

/* feraiseexcept.c - raise floating-point exception
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
#include <signal.h>
int (feraiseexcept) (int excepts)
{
_FE_current._FP_status_flags |= excepts;
raise (SIGFPE);
return 0;
}
/* END OF FILE */

/* fesetenv.c - set current floating-point environment
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fesetenv) (const fenv_t * envp)
{
if (envp == 0)
return 1;
/* establish current FPE */
fesetexceptflag (&(envp->_FP_status_flags), FE_ALL_EXCEPT);
return fesetround (envp->_FP_rounding_direction);
}
/* END OF FILE */

/* fesetexceptflag.c - set floating-point status flags
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fesetexceptflag) (const fexcept_t * flagp, int excepts)
{
if (flagp == 0)
return 1;
_FE_current._FP_status_flags =
(*flagp & excepts) | (_FE_current._FP_status_flags & ~excepts);
return 0;
}
/* END OF FILE */

/* fesetround.c - set current rounding direction
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fesetround) (int round)
{
if (round >= 1 && round <= 4)
{
_FE_current._FP_rounding_direction = round;
return 0;
}
else
return 1;
}
/* END OF FILE */

/* fetestexcept.c - test floating-point exception flags
AUTHOR: Gregory Pietsch
*/
#include <fenv.h>
int (fetestexcept) (int excepts)
{
return (_FE_current._FP_status_flags & excepts);
}
/* END OF FILE */

Thanks in advance, Gregory Pietsch

Nov 15 '05 #1
0 1553

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

71
by: Christopher Benson-Manica | last post by:
At what point was the .h dropped from the STL headers? I just had a discussion yesterday with my boss, who said he wanted .h on all the STL includes, despite me protesting that it was not...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
2
by: ambika | last post by:
Am just a beginner. Can someone please help me know what do non-standard header files mean??? I include the header file "conio.h" in my program.I was told that this is a non-standard header file...
5
by: sathyashrayan | last post by:
Friends, As I was going through the standard draft of C (C99) I come across the header file <time.h>. I wonder why the standard has included the header file since it depends on the hardware that...
5
by: jayceechong | last post by:
If I write my own C compiler, can I just "take" existing standard header files (e.g. stdio.h, stdlib.h, etc.) from an existing C compiler and plug them in my own? I understand I have to write my...
11
by: BilfFord X | last post by:
If I # include "text.h" , where text.h consists of the following: # define NOTHING_IMPORTANT # include <stdio.h> , am I writing c++? cordially, bfx
2
by: sharpblade | last post by:
The title of these Item is "Make header files self-sufficient" There're two examples in this Item: Example 1: Dependent names. Templates are compiled at the point where they are defined, except...
2
by: nikanth | last post by:
Isn't intptr_t part of the latest ISO C++? It is part of the C99 standard but C++? Has any new standard for C++ after C++ 2003 which is almost same as C++98 published? Which are the compilers that...
2
by: Devine123 | last post by:
Hi, I’m creating a perl script that takes incoming http/s requests, logs the standard input, output and error, before returning the output to the client. The input, output and error log is appended...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.