The POWER and POWER2 processor architectures include two special-purpose registers (an upper register and a lower register) that contain a high-resolution timer. The upper register contains time in seconds, and the lower register contains a count of fractional seconds in nanoseconds. The actual precision of the time in the lower register depends on its update frequency, which is model-specific.
The following assembler-language module (timer.s) provides routines (rtc_upper and rtc_lower) to access the upper and lower registers of the timer:
.globl .rtc_upper .rtc_upper: mfspr 3,4 # copy RTCU to return register br .globl .rtc_lower .rtc_lower: mfspr 3,5 # copy RTCL to return register br
The following module (second.c) contains a C routine that calls the timer.s routines to access the upper and lower register contents. It returns a double-precision real value of time in seconds.
double second() { int ts, tl, tu; ts = rtc_upper(); /* seconds */ tl = rtc_lower(); /* nanoseconds */ tu = rtc_upper(); /* Check for a carry from */ if (ts != tu) /* the lower reg to the upper. */ tl = rtc_lower(); /* Recover from the race condition. */ return ( tu + (double)tl/1000000000 ); }
The subroutine second() can be called from either a C routine or a FORTRAN routine.