[ Pobierz całość w formacie PDF ]

pointer can yield incorrect results.
The second choice also has a suboption: localize pointers to one routine and do
not optimize it, but do optimize the routines that do the calculations. If you put
the calling the routines on different files, you can optimize one and not
optimize the other.
Example: A relatively  safe kind of coding with -O3 or -O4:
REAL A, B, V(100,100) ! Within this programming unit,
POINTER ( P, V ) ! do nothing else with P
P = MALLOC(10000) ! other than getting the address and passing it.
&
CALL CALC ( P, A )
...
END
SUBROUTINE CALC ( ARRAY, X )
...
RETURN
END
If you want to optimize only CALC at level -O4, then use no pointers in CALC.
Some Problematic Code Practices
Any of the following coding practices, and many others, could cause problems
with an optimization level of -O3 or -O4:
" A program unit does arithmetic with the pointer.
" A subprogram saves the address of any of its arguments between calls.
" A function returns the address of any of its arguments, although it can
return the value of a pointer argument.
62 FORTRAN 77 Reference Manual
2
" A variable is referenced through a pointer, but the address of the variable is
not explicitly taken with the LOC() or MALLOC() functions.
Example: One kind of code that could cause trouble with -O3 or -O4:
COMMON A, B, C
POINTER ( P, V )
P = LOC(A) + 4 ! !Possible problems if optimized
&
The compiler assumes that a reference through P may change A, but not B;
this assumption could produce incorrect code.
Data Types and Data Items 63
2
64 FORTRAN 77 Reference Manual
Expressions
3
An expression is a combination of one or more operands, zero or more
operators, and zero or more pairs of parentheses.
This chapter is organized into the following sections:
Expressions, Operators, and Operands page 65
Arithmetic Expressions page 66
Character Expressions page 74
Logical Expressions page 78
Relational Operator page 80
Constant Expressions page 81
Record Assignment page 82
Evaluation of Expressions page 83
3.1 Expressions, Operators, and Operands
There are three kinds of expressions:
" An arithmetic expression evaluates to a single arithmetic value.
" A character expression evaluates to a single value of type character.
" A logical or relational expression evaluates to a single logical value.
The operators indicate what action or operation to perform.
65
3
The operands indicate what items to apply the action to. An operand can be any
of the following kinds of data items:
" Constant
" Variable
" Array element
" Function
" Substring
" Structured record field (if it evaluates to a scalar data item)
3.2 Arithmetic Expressions
An arithmetic expression evaluates to a single arithmetic value, and its operands
have the following types. indicates a nonstandard feature.
" BYTE
" COMPLEX
" COMPLEX*32 (SPARC only)
" DOUBLE COMPLEX
" DOUBLE PRECISION
" INTEGER
" LOGICAL
" REAL
" REAL*16 (SPARC only)
The operators for an arithmetic expression are any of the following:
Table 3-1 Arithmetic Operators
Operator Meaning
** Exponentiation
* Multiplication
/ Division
- Subtraction or Unary Minus
+ Addition or Unary Plus
If BYTE or LOGICAL operands are combined with arithmetic operators, they
are interpreted as integer data.
66 FORTRAN 77 Reference Manual
3
Each of these operators is a binary operator in an expression of the form:
a " b
where a and b are operands, and " is any one of the**,*,/,-, or+ operators.
Examples: Binary operators:
A-Z
X*B
The operators + and - are unary operators in an expression of the form:
" b
where b is an operand, and " is either of the - or + operators.
Examples: Unary operators:
-Z
+B
Basic Arithmetic Expressions
Each arithmetic operator is shown in its basic expression in the following table:
Table 3-2 Arithmetic Expressions
Expression Meaning
a ** z Raise a to the power z
a / z Divide a by z
a * z Multiply a by z
a - z Subtract z from a
-z Negate z
a + z Add z to a
+z Same as z
Expressions 67
3
In the absence of parentheses, if there is more than one operator in an
expression, then the operators are applied in the order of precedence. With one
exception, if the operators are of equal precedence, they are applied left to
right.
Table 3-3 Arithmetic Operator Precedence
Operator Precedence
** First
* / Second
+ - Last
For the left-to-right rule, the one exception is shown by the following example:
F ** S ** Z
The above is evaluated as:
F ** (S ** Z)
f77 allows two successive operators.
Example: Two successive operators:
X ** -A * Z
The above expression is evaluated as follows:
X ** (-(A * Z))
In the above example, the compiler starts to evaluate the **, but it needs to
know what power to raise X to; so it looks at the rest of the expression and
must choose between - and *. It first does the *, then the -, then the **.
Some early releases of this FORTRAN 77 incorrectly interpreted X**-A*Z as
(X**(-A))*Z. Current releases correctly interpret X**-A*Z as
 X**(-(A*Z)), which is compatible with VMS FORTRAN.
68 FORTRAN 77 Reference Manual
3
Example: Two successive operators:
demo% cat twoops.f
REAL X / 2.0 /, A / 1.0 /, Z / -3.0 /
PRINT *, "X**-A*Z = ", X ** -A*Z
PRINT *, "X**(-(A*Z)) = ", X ** (-(A*Z))
PRINT *, "(X**(-A))*Z = ", (X ** (-A))*Z
PRINT *, "X**-2 = ", X ** -2 !{same in both}
END
demo% f77old twoops.f (Use old)
twoops.f:
MAIN:
demo% a.out
X**-A*Z = -1.50000
X**(-(A*Z)) = 8.00000
(X**(-A))*Z = -1.50000
X**-2 = 0.250000
demo% f77new -silent twoops.f {Use new}
demo% a.out
X**-A*Z = 8.00000
X**(-(A*Z)) = 8.00000
(X**(-A))*Z = -1.50000
X**-2 = 0.250000
demo%
Expressions 69
3
Mixed Mode
If both operands have the same type, then the resulting value has that type. If
operands have different types, then the weaker of two types is promoted to the
stronger type, where the weaker type is the one with less precision or fewer
storage units. The ranking is summarized in the following table:
Data Type Rank
BYTE or LOGICAL*1 1 (Weakest)
LOGICAL*2 2
LOGICAL*4 3
INTEGER*2 4
INTEGER*4 5
INTEGER*8 6 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • annablack.xlx.pl