# 2 HPF Data Types

Every HPF element and expression has a data type. The data type of an element may be implicit in its definition or explicitly attached to the element in a declaration statement. This chapter describes the HPF data types and constants that pghpf supports.

Fortran provides two types of data types, intrinsic data types and derived data types. Types provided by the language are intrinsic types. Types specified by the programmer and built from the intrinsic data types are called derived types.

## 2.1 Intrinsic Data Types

Fortran provides six different intrinsic data types as shown in Table 2-1. Tables 2-2 and 2-3 show variations and different "kinds" of the intrinsic data types that pghpf supports.

### 2.1.1 Kind Parameter

The pghpf compiler supports the Fortran KIND parameter to specify a precision for intrinsic data types. The KIND parameter follows a data type specifier and specifies size or type of the supported data type. A KIND specification overrides the length attribute that the statement implies and assigns a specific length to the item, regardless of the compiler's command-line options. A KIND is defined for a data type by a PARAMETER statement, using sizes supported on the particular system.

The following are several examples using a KIND specification:

```INTEGER (SHORT) :: L
REAL (HIGH)  B
REAL (KIND=HIGH)  XVAR,  YVAR
```
These examples require that the programmer use a PARAMETER statement to define the kinds:
```INTEGER, PARAMETER:: SHORT =1
INTEGER HIGH
PARAMETER (HIGH=8)
```
Table 2-2 shows several examples of KINDs that a system could support.

Table 2-1 Fortran Intrinsic Data Types
 Data Type Value INTEGER An integer number. REAL A real number. DOUBLE PRECISION A double precision floating point number, real number, taking up two numeric storage units and whose precision is greater than REAL. LOGICAL A value which can be either TRUE or FALSE. COMPLEX A pair of real numbers used in complex arithmetic. Fortran provides two precisions for COMPLEX numbers. CHARACTER A string consisting of one or more printable characters.

Table 2-2 Data Types Kind Parameters (examples)
 Type Kind Size INTEGER SHORT 1 byte INTEGER LONG 2 bytes REAL HIGH 8 bytes

### 2.1.2 Number of Bytes Specification @

The pghpf compiler supports a length specifier for some data types. The data type can be followed by a data type length specifier of the form *s, where s is one of the supported lengths for the data type. Such a specification overrides the length attribute that the statement implies and assigns a specific length to the specified item, regardless of the compiler options. For example, REAL*8 is equivalent to DOUBLE PRECISION. Table 2-3 shows the lengths of data types, their meanings, and their sizes.

Table 2-3 Data Type Extensions
 Type Meaning Size LOGICAL*1 Small LOGICAL 1 byte LOGICAL*2 Short LOGICAL 2 bytes LOGICAL*4 LOGICAL 4 bytes BYTE Small INTEGER 1 byte INTEGER*1 Same as BYTE 1 byte INTEGER*2 Short INTEGER 2 bytes INTEGER*4 INTEGER 4 bytes REAL*4 REAL 4 bytes REAL*8 DOUBLE PRECISION 8 bytes COMPLEX*8 COMPLEX 8 bytes COMPLEX*16 DOUBLE COMPLEX 16 bytes

The BYTE type is treated as a signed one-byte integer and is equivalent to LOGICAL*1.

Assignment of a value too big for the data type to which it is assigned is an undefined operation.

A specifier is allowed after a CHARACTER function name even if the CHARACTER type word has a specifier. For example:

```CHARACTER*4 FUNCTION C*8 (VAR1)
```
The function size specification C*8 overrides the CHARACTER*4 specification. Logical data items can be used with any operation where a similar sized integer data item is permissible and vice versa. The logical data item is treated as an integer or the integer data item is treated as a logical of the same size and no type conversion is performed.

Floating point data items of type REAL or DOUBLE PRECISION may be used as array subscripts, in computed GOTOs, in array bounds and in alternate returns, pghpf converts the floating point number to an integer.

The data type of the result of an arithmetic expression corresponds to the type of its data. The type of an expression is determined by the rank of its elements. Table 2-4 shows the ranks of the various data types, from lowest to highest.

Table 2-4 Data Type Ranks
 Data Type Rank LOGICAL 1 (lowest) INTEGER*2 2 INTEGER*4 3 REAL*4 4 REAL*8 (Double precision) 5 COMPLEX*8 (Complex) 6 COMPLEX*16 (Double complex) 7 (highest)

The data type of a value produced by an operation on two arithmetic elements of different data types is the data type of the highest-ranked element in the operation. The exception to this rule is that an operation involving a COMPLEX*8 element and a REAL*8 element produces a COMPLEX*16 result. In this operation, the COMPLEX*8 element is converted to a COMPLEX*16 element, which consists of two REAL*8 elements, before the operation is performed.

The type of a logical expression is always a LOGICAL*4 result.

## 2.2 Constants

A constant is an unchanging value that can be determined at compile time. It takes a form corresponding to one of the data types.

The pghpf Fortran compiler supports decimal (INTEGER and REAL), unsigned binary, octal, hexadecimal and Hollerith constants. The use of character constants in a numeric context, for example, in the right-hand side of an arithmetic assignment statement, is supported. These constants assume a data type that conforms to the context in which they appear.

### 2.2.1 Integer Constants

The form of a decimal integer constant is:

```[s]d1d2...dn  [ _ kind-parameter ]
```
where s is an optional sign and di is a digit in the range 0 to 9. The optional _kind-parameter specifies a supported kind. The value of an integer constant must be within the range for the specified kind.

Below are several examples of integer constants.

```+2
-36
437
-36_SHORT
```

#### Binary, Octal and Hexadecimal Constants

The pghpf compiler, and Fortran 90 support various types of constants besides decimal constants. Fortran allows unsigned binary, octal, or hexadecimal constants in DATA statements. pghpf supports these constants in DATA statements, and additionally, supports some of these constants outside of DATA statements. For more information on support of these constants, refer to Section 2.6, "Fortran Binary, Octal and Hexadecimal Constants" later on in this chapter.

### 2.2.2 Real Constants

Real constants have two forms, scaled and unscaled. An unscaled real constant consists of a signed or unsigned decimal number (a number with a decimal point). A scaled real constant takes the same form as an unscaled constant, but is followed by an exponent scaling factor of the form:

```E+digits  [_ kind-parameter ]
Edigit [_ kind-parameter ]
E-digits [_ kind-parameter ]
```
where digits is the scaling factor, the power of ten, to be applied to the unscaled constant. The first two forms above are equivalent, that is, a scaling factor without a sign is assumed to be positive. Table 2-5 shows several real constants.

Table 2-5 Example of Real Constants
 Constant Value 1.0 unscaled single precision constant 1. unscaled single precision constant -.003 signed unscaled single precision constant -.003_LOW signed unscaled constant with kind LOW. -1.0 signed unscaled single precision constant 6.1E2_LOW is equivalent to 610.0 with kind LOW +2.3E3_HIGH is equivalent to 2300.0 with kind HIGH 6.1E2 is equivalent to 610.0 +2.3E3 is equivalent to 2300.0 -3.5E-1 is equivalent to -0.35

#### Double Precision Constants

A double precision constant has the same form as a scaled REAL constant except that the E is replaced by D and the kind parameter is not permitted. Table 2-6 shows several double precision constants.

```D+digits
Ddigit
D-digits
```

Table 2-6 Double Precision Constants
 Constant Value 6.1D2 is equivalent to 610.0 +2.3D3 is equivalent to 2300.0 -3.5D-1 is equivalent to -0.35 +4D4 is equivalent to 40000.

### 2.2.3 Complex Constants

A complex constant is held as two real or integer constants separated by a comma and surrounded by parentheses. The first real number is the real part and the second real number is the imaginary part. Together these values represent a complex number. Integer values supplied as parameter for a COMPLEX constant are converted to REAL numbers. Below are several examples:

```(18,-4)(3.5,-3.5)(6.1E2,+2.3E3)
```

### 2.2.4 Logical Constants

A logical constant is one of:

```.TRUE. [ _ kind-parameter ]
.FALSE.[ _ kind-parameter ]
```
The logical constants .TRUE. and .FALSE. are by default defined to be four-byte values -1 and 0 respectively. A logical expression is defined to be .TRUE. if its least significant bit is 1 and .FALSE. otherwise[*] . Below are several examples:
```.TRUE..FALSE..TRUE._BIT
```
The abbreviations T and F can be used in place of .TRUE. and .FALSE. in data initialization statements and in NAMELIST input.

### 2.2.5 Character Constants

Character string constants may be delimited using either an apostrophe (') or a double quote ("). The apostrophe or double quote acts as a delimiter and is not part of the character constant. Use two apostrophes together to include an apostrophe as part of the expression. If a string begins with one variety of quote mark, the other may be embedded within it without using the repeated quote or backslash escape. Within character constants, blanks are significant. For further information on the use of the backslash character, refer to -Mbackslash in the pghpf User's Guide.

A character constant is one of:

```[ kind-parameter_ ] "[characters]"
[ kind-parameter_ ] '[characters]'
```
Below are several examples of character constants.
```'abc'
'abc '
'ab''c'"Test Word"
GREEK_"uupi"
```
A zero length character constant is written as '' or "".

If a character constant is used in a numeric context, for example as the expression on the right side of an arithmetic assignment statement, it is treated as a Hollerith constant. The rules for typing and sizing character constants used in a numeric context are outlined later in the description of Hollerith constants.

### 2.2.6 PARAMETER Constants

The PARAMETER statement permits named constants to be defined. Refer to the description of the PARAMETER statement found in Chapter 3 for more details on defining constants.

## 2.3 Derived Types

A derived type is a type made up of components whose type is either intrinsic or another derived type. The TYPE and END TYPE keywords define a derived type. For example the following derived type declaration defines the type PERSON and the array CUSTOMER of type PERSON:

```! Declare a structure to define a person derived typeTYPE PERSON		INTEGER ID	LOGICAL LIVING	CHARACTER(LEN=20) FIRST, LAST, MIDDLE	INTEGER AGEEND TYPE PERSON
TYPE (PERSON) CUSTOMER(10)
```
A derived type statement definition is called a derived-type statement (the statements between TYPE PERSON and END TYPE PERSON in the previous example. The definition of a variable of the new type is called a TYPE statement (CUSTOMER in the previous example); note the use of parenthesis in the TYPE statement.

The % character accesses the components of a derived type. For example:

```CUSTOMER(1) % ID = 11308
```

## 2.4 Arrays

Arrays in Fortran are not data types, but are intrinsic or derived types with special characteristics. A dimension statement provides a data type with one or more dimensions. The differences between Fortran 90 and old Fortran 77 arrays are several (note that Fortran 90 supports all of Fortran 77 array semantics). Fortran 90 arrays are "objects" and operations and expressions involving arrays may apply to every element of the array in an unspecified order. For example, in the following code, where A and B are arrays of the same size, the following expression adds six to every element of B and assigns the results to corresponding elements of A:

```	A = B + 6
```
Fortran arrays may be passed with unspecified shapes to subroutines and functions, and sections of arrays may be used and passed as well. Arrays of derived type data types are also valid. In addition, allocatable arrays may be created with deferred shapes (actual dimensions are determined when the array is allocated while the program is running).

## 2.5 Fortran Pointers and Targets

Fortran pointers are similar to allocatable arrays. Pointers are declared with a type and a rank, however they do not actually represent a value, but represent a value's address. Fortran has a new assignment statement for dealing with pointers. The pointer assignment operator => allows assignment to a pointer.

## 2.6 Fortran Binary, Octal and Hexadecimal Constants

The PGI HPF language supports two representations for Binary, Octal, and Hexadecimal numbers, The standard Fortran 90 representation and the PGI extension representation. Refer to Appendix C, "PGI Extensions" for details on the alternate representation.

Fortran supports binary, octal and hexadecimal constants in DATA statements. The form of a binary constant is:

```B'b1b2...bn'
B"b1b2...bn"
```
where b i is either 0 or 1.

The form of an octal constant is:

```O'c1c2...cn'
O"c1c2...cn"
```
where c i is in the range 0 through 7.

The form of a hexadecimal constant is:

```Z'a1a2...an'
Z"a1a2...an"
```
where ai is in the range 0 through 9 or a letter in the range A through F or a through f (case mixing is allowed).

## 2.7 Hollerith Constants

The form of a Hollerith constant is:

```nHc1c2...cn
```
where n specifies the positive number of characters in the constant and cannot exceed 2000 characters. A Hollerith constant is stored as a byte string with four characters per 32-bit word. Hollerith constants are untyped arrays of INTEGER*4. The last word of the array is padded on the right with blanks if necessary. Hollerith constants cannot assume a character data type and cannot be used where a character value is expected. The data type of a Hollerith constant used in a numeric expression is determined by the following rules:
• Sign-extension is never performed.
• The byte size of the Hollerith constant is determined by its context and is not strictly limited to 32 or 64 bits like hexadecimal and octal constants.
• When the constant is used with a binary operator (including the assignment operator), the data type of the constant assumes the data type of the other operand.
• When a specific data type is required, that type is assumed for the constant. When an integer or logical is required, INTEGER*4 and LOGICAL*4 are assumed. When a float is required, REAL*4 is assumed (array subscripting is an example of the use of a required data type).
• When a constant is used as an argument to certain generic functions (AND, OR, EQV, NEQV, SHIFT, and COMPL), a 32-bit operation is performed if no argument is larger than 32 bits; otherwise, a 64-bit operation is performed. The size of the result corresponds to the chosen operation.
• When a constant is used as an actual argument, no data type is assumed and the argument is passed as an INTEGER*4 array. Character constants are passed by descriptor only.
• When a constant is used in any other context, a 32-bit INTEGER*4 array type is assumed.
When the length of the Hollerith constant is less than the length implied by the data type, spaces are appended to the constant on the right. When the length of the constant is greater than the length implied by the data type, the constant is truncated on the right.

[*] The option -Munixlogical defines a logical expression to be TRUE if its value is non-zero, and FALSE otherwise; also, the internal value of .TRUE. is set to one. This option is not available on all pghpf target systems.