HP 3000 Manuals

Using Consistent Data Storage [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Using Consistent Data Storage 

Because machine architectures differ between systems, the methods of
storing data also differ.  Default sizes for data types might also differ
between HP and non-HP systems, resulting in the data storage accidentally
overlapping.  This creates a program that compiles and loads on both HP
and non-HP systems without errors, but produces different results with
the same input data.  However, if data storage is used carefully and
consistently, programs producing varied results on different systems with
the same data will be less frequent.

Guidelines for consistency and integrity of a program's data storage are
described below.  Using these guidelines might not produce a program that
makes optimal use of space, or executes as quickly as possible; however,
using these guidelines will produce code that is easily portable and is
fairly efficient.

Use the LONG and SHORT Compiler Directives 

If you are porting between HP systems, data type consistency is easily
maintained because the implicit defaults for HP FORTRAN 77 data types on
all HP implementations are the same.  That is, on all systems, the type
INTEGER defaults to INTEGER*4 and the type REAL defaults to REAL*4.
However, if your program was developed on a non-HP compiler or on an HP
compiler that is not an implementation of the HP FORTRAN 77 standard
(such as FORTRAN/3000, FORTRAN 4X, or FORTRAN/1000), the default data
type sizes might not be the same.  For example, the type INTEGER defaults
to INTEGER*2 under FORTRAN/3000.

You can use the HP FORTRAN 77 LONG and SHORT compiler directives to
change the default data type sizes.  The LONG directive on HP systems
only documents the HP FORTRAN 77 default because the default is already
four bytes for INTEGER and LOGICAL values.  However, the SHORT directive
sets the default for INTEGER and LOGICAL to two bytes.  For example, if
you insert a SHORT directive in every unit to be ported from
FORTRAN/3000, the implicit data type sizes will not change and therefore
should not create any problems.  Refer to the HP FORTRAN 77/iX Reference 
for more information on the LONG and SHORT directive.

Consistent data storage on HP systems is obtained by using the implicit
HP FORTRAN 77 defaults or by using the LONG and SHORT directives.
However, because the data type sizes are not clearly documented in the
program itself, these methods are not recommended for programs that might
be ported more than once.  Instead, declare all variables, as described
later in this chapter.

Use Length Specifications in All Type Statements 

Not only should all the variables be explicitly declared, but their
individual sizes should be defined and documented.  By declaring sizes,
you avoid the possibility of different default sizes causing invalid
run-time results and you ensure that the common and equivalence lengths
are the same.  Also, the data storage is well defined, with the exact
amount of variable storage specified in the declarations.  This guideline
is not recommended if performance is a priority.

Declare All Variables 

Implicit declaration of variables according to the standard HP FORTRAN 77
conventions (variables beginning with the letters I through N are
INTEGER; the rest are REAL) causes a default data item's size to be
assigned.  In addition, implicit declarations are not documented, making
modifications difficult.  Although implicit declarations might save
programming time during the initial writing phase, it might take more
time for debugging and modifying the program.  Therefore, declaring all
variables in a program should save programming time, as well as ensuring
the integrity of the data structures.

Because HP FORTRAN 77 has an implicit declaration facility defined as
part of the ANSI language, you might forget to declare all variables.
For example, it would be easy to forget to declare an index of a DO loop
if the loop was added after the initial design was complete.  You can use
the IMPLICIT NONE statement to turn off the implicit declaration
facility.  The IMPLICIT NONE statement (a MIL-STD-1753 extension)
generates error messages for undeclared variables.  Placing this
statement in each program unit immediately following the PROGRAM,
SUBROUTINE, FUNCTION, or BLOCK DATA statement guarantees that no variable
declaration is overlooked.

Avoid Using the EQUIVALENCE Statement 

Data alignment requirements differ between systems.  For example,
noncharacter data on some machines must be word-aligned, while character
data on other machines must be aligned on a byte boundary.  Considering
the different machine word sizes between systems, you can see how data
alignment and equivalence overlapping could change without being noticed
when a program is ported between systems.  Therefore, when possible,
avoid equivalences between character and noncharacter data.

Because systems store data differently, an EQUIVALENCE statement can
cause storage allocation problems.  Complicated equivalence expressions
have a higher chance of problems in the storage allocation algorithm;
therefore, do not try to conserve storage by using equivalences.  Also,
long and complicated equivalence expressions can be confusing when
changes have to be made to the program.

Declare Common Blocks the Same in Every Program Unit 

Because common blocks are implemented differently on different HP
systems, you should declare each common block exactly the same in every
program unit in which it is used.  By doing this, your program is easier
to read and is consistent in data storage.  If common blocks are not
declared the same, some of the same problems associated with the
EQUIVALENCE statement could occur.  However, because the declarations are
in different program units (and possibly in different files), the
problems are more difficult to find and correct.

To ensure that the declarations remain identical, use the INCLUDE
statement or INCLUDE compiler directive.  Place each common block
declaration and associated variable declarations in a separate file;
reference the file by using the INCLUDE statement or directive in every
program unit that uses the common block.  There is no chance of errors
due to unmatched declarations if all program units that share a
declaration file are recompiled whenever the declaration file changes.
However, if all program units are not recompiled when their declaration
file changes, differences in the old and new common block definitions
might cause incorrect run-time results.

Initialize Data Before the Algorithm Begins 

The initialization of memory locations varies between systems.  Some
systems check and initialize all allocated memory to a set value.  Most
systems leave the allocated memory in the same state as the previous
program that used the memory.  Therefore, to ensure storage portability,
initialize all data before the actual algorithm begins.

Using HP FORTRAN 77, you can initialize data in two ways:

   1.  Use assignment statements to improve documentation and readability
       if your program is small, has very few variables, and does not
       have common blocks.

   2.  Use DATA statements with BLOCK DATA subprograms for initializing
       common blocks if you need more control over the initial values.

Refer to the HP FORTRAN 77/iX Reference for the syntax and semantics of
these statements.

Avoid Accessing the Representation of Logical Values 

The representation for the logical values .TRUE. and .FALSE. differs
between implementations of HP FORTRAN 77.  Therefore, avoid accessing
these values (such as by using the EQUIVALENCE statement) and testing
their internal values.  You should also not use logical variables to pass
nonlogical data (such as character data) because the clarity and
readability of the program is reduced.

Maintain Parameter Type and Length Consistency 

The final guideline for data storage portability is to maintain type and
length consistency for subroutine and function parameters and function
values being returned.  For example, serious problems in data overlapping
can result if a program calls a subroutine with two INTEGER*2 parameters
when the subroutine expects two INTEGER*4 parameters.  Because FORTRAN 77
passes all parameters by reference, the program's data area will become
corrupt because the subroutine manipulates four extra bytes of data.
Problems like this often appear when trying to port to another system.

Care should especially be taken for character data.  The CHARACTER*(*)
declaration should only be used when a routine must be called with
different character parameter sizes.  In these situations, the routines
should be written to guarantee that the current length of a character
parameter is not exceeded.  The length can be passed as a separate
parameter or determined in the subprogram by using the LENGTH intrinsic
function.  Programs should be written to avoid data corruption from
inconsistent type and length of parameters.  On your system, there may be
a system-specific directive that helps ensure consistent parameter type
and length; for details, see the HP FORTRAN 77/iX Reference Manual.

By following the data storage guidelines described in this section, you
will avoid most data overlap and data size problems.  In summary, all the
variables and parameters should be declared so that any system's storage
allocation algorithm produces data areas that function identically.
Alignment problems are avoided by a minimal use of the EQUIVALENCE
statement.  All common blocks of the same name should have the same
internal structure.  Data should be explicitly initialized.  The internal
representation of logical values should not be relied upon.  Type and
length consistency should be maintained for parameters and return values.



MPE/iX 5.0 Documentation