HP 3000 Manuals

Guidelines for Portability [ HP C Programmer's Guide ] MPE/iX 5.0 Documentation


HP C Programmer's Guide

Guidelines for Portability 

This section lists some things you can do to make your HP C programs more
portable.

   *   Use the ANSI C compiler option whenever possible when writing new
       programs.  HP C conforms to the standard when it is invoked with
       the -Aa option.  The -w and +e options should not be used with the
       -Aa option, as these options will suppress warning messages and
       allow non-conforming extensions.

   *   When you recompile existing programs, try compiling in ANSI mode.
       ANSI C mandates more thorough error checking, so portability
       problems are more likely to be flagged by the compiler in this
       mode.  (Bugs are also more likely to be caught.)  Many existing
       programs will compile and execute correctly in ANSI mode with few
       or no changes.

   *   Pay attention to all warnings produced by the compiler.  Most
       warnings represent potentially problematic program constructs.
       You should consider warnings to be portability risks.

   *   For an additional level of warnings, compile with the +w1 option.
       Pay particular attention to the warnings that mention "ANSI
       migration" issues.  These identify most program constructs that
       are legal but are likely to work differently between pre-ANSI and
       ANSI compilers.

   *   Consult the detailed listing of diagnostic messages in the HP C
       Reference Manual for more information on how to correct each
       problem.  For most messages, a reference to the relevant section
       of the ANSI standard is also given.

   *   On HP-UX, use lint, the C program syntax checker, to detect
       potential portability problems in your program.  The lint utility
       also produces warnings about poor style, lack of efficiency, and
       inconsistency within the program.

   *   Use the #define, #if, and #ifdef preprocessing directives and
       typedef declarations to isolate any necessary machine or operating
       system dependencies.

   *   Declare all variables with the correct types.  For example,
       functions and parameters default to int.  On many implementations,
       pointers and integers are the same size, and the defaults work
       correctly.  However, for maximum portability, the correct types
       should be used.

   *   Use only the standard C library functions.

   *   Code bit manipulation algorithms carefully to gain independence
       from machine-specific representations of numeric values.  For
       example, use x & ~3 instead of x & 0xFFFFFFFC to mask the
       low-order 2 bits to zero.

   *   Avoid absolute addressing.

Examples 

The following example illustrates some ways to program for portability.
In this example, the include files IEEE.h and floatX.h isolate
machine-dependent portions of the code.  These include files use the
#define and typedef mechanisms to define macro constants and type
definitions in the main body of the program.

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;
                  }
              :



MPE/iX 5.0 Documentation