SYSTEM_CLOCK Fixup

by Craig T. Dedo

February 19, 1996

1.	Rationale

Currently, the Fortran standard requires that all three arguments to the SYSTEM_CLOCK intrinsic subroutine be of type default INTEGER. While the INTEGER data type is acceptable for COUNT and COUNT_MAX, it causes serious accuracy problems for the COUNT_RATE argument. This is most likely an error in the existing standard which should be corrected. This error unnecessarily restricts the available precision.

The processor clocks of many machines, most notably the clocks of Intel 80x86 PCs, have significant fractional components in their clock rates. On Intel (MS-DOS PC) machines, the clock ticks approximately 18.20648193 (1,193,180 / 65,536) times per second (Norton and Wilton 1988, 143-145) (Norton, Aitken, and Wilton 1993, 174-178). Requiring COUNT_RATE to be INTEGER causes considerable loss of accuracy for such machines.

Interpretation 132 attempted to resolve this problem but it is at best a barely tolerable quick fix. As currently defined, interpretation 132 poses severe portability problems, even from one compiler to another on the same machine. In the Intel case, the interpretation allowed all three of these practices:

1.	Increment COUNT by 1 every 0.05495 seconds but return a COUNT_RATE of 18.

2.	Increment COUNT by 10 every 0.05495 seconds and return a COUNT_RATE of 182.

3.	Increment COUNT by a variable amount every 0.05495 seconds and return a COUNT_RATE of 100.

All three of these practices are, at best, counter-intuitive and not the behavior an ordinary programmer would expect. At worst, they are downright deceptive. Most programmers expect COUNT_RATE to be a very close approximation to the actual system clock count rate.

One of the most attractive features of Fortran is that it is generally intuitive and straightforward. There are relatively few traps and "gotchas" which cause problems for programmers who have less than expert level of ability. This is in sharp contrast to almost all other popular languages. In order to encourage the continued popularity of Fortran, X3J3 should eliminate such problems whenever possible.

2.	Technical Description

This proposal extends the functionality of the SYSTEM_CLOCK subroutine so that it can accept non-default kinds of integers in all three arguments and can accept a generic real argument for COUNT_RATE.

This proposal provides an optional scaling feature for processors with very high clock rates. The author chose a cutoff rate of 20,000 ticks per second because this is the highest round value that is possible for a clock rate without causing COUNT_MAX to exceed the maximum value for a 4 byte signed integer in a 24 hour period. (Note: 2,147,483,648 / 86,400 = 24,855.13)

3.	Edits to the Standard

These edits are with respect to the Fortran 95 Committee Draft, X3J3 / 96-007r0 .

[266:19-40] Replace section 13.14.106 with the following:

13.14.106	SYSTEM_CLOCK ([COUNT][, COUNT_RATE][, COUNT_MAX])

Description.	Returns numeric data from a real-time clock.

Class.	Subroutine

Arguments.

COUNT (optional)	shall be scalar and of type signed integer of any kind that is capable of representing the value of COUNT_MAX. It is an INTENT (OUT) argument. It is set to a processor-dependent value based on the current value of the processor clock or to -HUGE(0) if there is no clock. The processor-dependent value is incremented by one for each clock count until the value COUNT_MAX is reached and is reset to zero at the next count. It lies in the range 0 to COUNT_MAX if there is a clock.

COUNT_RATE (optional)

shall be scalar and of type integer or real of any kind that is defined on the processor. It is an INTENT (OUT) argument. It is set to a processor- dependent approximation to the number of processor clock counts per second, or to zero if there is no clock.

COUNT_MAX (optional)

shall be scalar and of type signed integer of any kind that is capable of representing the value of COUNT_MAX. It is an INTENT (OUT) argument. It is set to the number of clock ticks in a 24 hour (86400 second) period or to zero if there is no clock. This argument is the maximum value that COUNT can have.

Constraint:	A processor with an actual clock rate greater than 20,000 ticks per second may optionally report a result for COUNT_RATE that is scaled by an integer scale factor such that COUNT_RATE repo