After qptimizing the epicsTime::addNanoSec function we now have a larger code which uses only unsigned integer arithmetic. This is for the benefit of embedded cpu's lacking a hardware floating point coprocessor at the expense of some additional code to maintain. I measered the cpu load improving from 40% free to 80% free after making this change, on a nios2 soft core processor.

This commit is contained in:
Jeff Hill johill@lanl.gov
2012-11-14 12:51:30 -07:00
parent b8eec29e43
commit b86c4107c1
+42 -2
View File
@@ -113,8 +113,48 @@ epicsTimeLoadTimeInit::epicsTimeLoadTimeInit ()
//
inline void epicsTime::addNanoSec (long nSecAdj)
{
double secAdj = static_cast <double> (nSecAdj) / nSecPerSec;
*this += secAdj;
// After qptimizing this function we now have a larger
// code which uses only unsigned integer arithmetic.
// This is for the benefit of embedded cpu's lacking
// a hardware floating point coprocessor at the
// expense of some additional code to maintain.
// joh 14-11-2012
if ( nSecAdj >= 0 ) {
unsigned long nSecOffsetLong =
static_cast < unsigned long > ( nSecAdj );
while ( nSecOffsetLong >= nSecPerSec ) {
this->secPastEpoch++; // overflow expected
nSecOffsetLong -= nSecPerSec;
}
const epicsUInt32 nSecOffset =
static_cast < epicsUInt32 > ( nSecOffsetLong );
epicsUInt32 nSecPerSecRemaining = nSecPerSec - nSecOffset;
if ( this->nSec >= nSecPerSecRemaining ) {
this->secPastEpoch++; // overflow expected
this->nSec -= nSecPerSecRemaining;
}
else {
this->nSec += nSecOffset;
}
}
else {
unsigned long nSecOffsetLong =
static_cast <unsigned long> ( -nSecAdj );
while ( nSecOffsetLong >= nSecPerSec ) {
this->secPastEpoch--; // underflow expected
nSecOffsetLong -= nSecPerSec;
}
const epicsUInt32 nSecOffset =
static_cast < epicsUInt32 > ( nSecOffsetLong );
if ( this->nSec >= nSecOffset ) {
this->nSec -= nSecOffset;
}
else {
// borrow
this->secPastEpoch--; // underflow expected
this->nSec += nSecPerSec - nSecOffset;
}
}
}
//