HP C Programmer's Guide (92434-90009)

Chapter 5 123
Programming for Portability
Guidelines for Portability
The main program fmult.c uses the #ifdef preprocessor command to include floatX.h
by default. If the option -D IEEE_FLOAT is passed to the compiler, and subsequently the
preprocessor, the program will use the IEEE representation for the structure float_rep
rather than a machine-dependent representation.
Partial contents of the file IEEE.h:
#define FLT_MAX 3.4028235E38
#define PLUS_INFINITY 0X7F800000
#define MINUS_INFINITY 0XFF800000
typedef struct {
unsigned sign : 1;
unsigned exp : 8;
unsigned mant : 23;
} FLOAT_REP;
#define EXP_BIAS 127
Partial contents of the file floatX.h:
#define FLT_MAX 1.70141E38
#define PLUS_INFINITY 0X7FFFFFFE
#define MINUS_INFINITY 0XFFFFFFFE
typedef struct {
unsigned sign : 1;
unsigned mant : 23;
unsigned exp : 7;
unsigned exp_sign : 1;
} FLOAT_REP;
#define EXP_BIAS 0
Partial contents of the file fmult.c:
#ifdef IEEE_FLOAT
#include "IEEE.h"
#else
#include "floatX.h"
#endif
union {
float f;
FLOAT_REP f_rep;
FLOAT_INT f_int;
} float_num;
float f_mult(float val1, float val2)
{
if (val1 > 1.0F val2 >1.0F) {
if (val1 > FLT_MAX/val2 ||
val2 > FLT_MAX/val1) {
float_num.f_int = PLUS_INFINITY;
return float_num.f;
}