llcommon merge. Added LLUnits.

This commit is contained in:
Shyotl
2016-04-06 01:31:20 -05:00
parent 0fa7848b19
commit 0841479ccc
66 changed files with 1895 additions and 621 deletions

View File

@@ -31,11 +31,9 @@
#include "u64.h"
#if LL_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# include <windows.h>
# include "llwin32headerslean.h"
#elif LL_LINUX || LL_SOLARIS || LL_DARWIN
# include <errno.h>
# include <errno.h>
# include <sys/time.h>
#else
# error "architecture not supported"
@@ -81,10 +79,10 @@ void ms_sleep(U32 ms)
U32 micro_sleep(U64 us, U32 max_yields)
{
// max_yields is unused; just fiddle with it to avoid warnings.
max_yields = 0;
ms_sleep(us / 1000);
return 0;
// max_yields is unused; just fiddle with it to avoid warnings.
max_yields = 0;
ms_sleep((U32)(us / 1000));
return 0;
}
#elif LL_LINUX || LL_SOLARIS || LL_DARWIN
static void _sleep_loop(struct timespec& thiswait)
@@ -216,56 +214,68 @@ U64 get_clock_count()
#endif
void update_clock_frequencies()
TimerInfo::TimerInfo()
: mClockFrequency(0.0),
mTotalTimeClockCount(0),
mLastTotalTimeClockCount(0)
{}
void TimerInfo::update()
{
gClockFrequency = calc_clock_frequency();
gClockFrequencyInv = 1.0/gClockFrequency;
gClocksToMicroseconds = gClockFrequencyInv * SEC_TO_MICROSEC;
mClockFrequency = calc_clock_frequency();
mClockFrequencyInv = 1.0/mClockFrequency;
mClocksToMicroseconds = mClockFrequencyInv;
}
TimerInfo& get_timer_info()
{
static TimerInfo sTimerInfo;
return sTimerInfo;
}
///////////////////////////////////////////////////////////////////////////////
// returns a U64 number that represents the number of
// microseconds since the Unix epoch - Jan 1, 1970
U64 totalTime()
U64MicrosecondsImplicit totalTime()
{
U64 current_clock_count = get_clock_count();
if (!gTotalTimeClockCount)
if (!get_timer_info().mTotalTimeClockCount || get_timer_info().mClocksToMicroseconds.value() == 0)
{
update_clock_frequencies();
gTotalTimeClockCount = current_clock_count;
get_timer_info().update();
get_timer_info().mTotalTimeClockCount = current_clock_count;
#if LL_WINDOWS
// Synch us up with local time (even though we PROBABLY don't need to, this is how it was implemented)
// Sync us up with local time (even though we PROBABLY don't need to, this is how it was implemented)
// Unix platforms use gettimeofday so they are synced, although this probably isn't a good assumption to
// make in the future.
gTotalTimeClockCount = (U64)(time(NULL) * gClockFrequency);
get_timer_info().mTotalTimeClockCount = (U64)(time(NULL) * get_timer_info().mClockFrequency);
#endif
// Update the last clock count
gLastTotalTimeClockCount = current_clock_count;
get_timer_info().mLastTotalTimeClockCount = current_clock_count;
}
else
{
if (LL_LIKELY(current_clock_count >= gLastTotalTimeClockCount))
if (current_clock_count >= get_timer_info().mLastTotalTimeClockCount)
{
// No wrapping, we're all okay.
gTotalTimeClockCount += current_clock_count - gLastTotalTimeClockCount;
get_timer_info().mTotalTimeClockCount += current_clock_count - get_timer_info().mLastTotalTimeClockCount;
}
else
{
// We've wrapped. Compensate correctly
gTotalTimeClockCount += (0xFFFFFFFFFFFFFFFFULL - gLastTotalTimeClockCount) + current_clock_count;
get_timer_info().mTotalTimeClockCount += (0xFFFFFFFFFFFFFFFFULL - get_timer_info().mLastTotalTimeClockCount) + current_clock_count;
}
// Update the last clock count
gLastTotalTimeClockCount = current_clock_count;
get_timer_info().mLastTotalTimeClockCount = current_clock_count;
}
// Return the total clock tick count in microseconds.
return (U64)(gTotalTimeClockCount*gClocksToMicroseconds);
U64Microseconds time(get_timer_info().mTotalTimeClockCount*get_timer_info().mClocksToMicroseconds);
return time;
}
@@ -273,9 +283,9 @@ U64 totalTime()
LLTimer::LLTimer()
{
if (!gClockFrequency)
if (!get_timer_info().mClockFrequency)
{
update_clock_frequencies();
get_timer_info().update();
}
mStarted = TRUE;
@@ -283,20 +293,32 @@ LLTimer::LLTimer()
}
LLTimer::~LLTimer()
{}
// static
void LLTimer::initClass()
{
if (!sTimer) sTimer = new LLTimer;
}
// static
U64 LLTimer::getTotalTime()
void LLTimer::cleanupClass()
{
delete sTimer; sTimer = NULL;
}
// static
U64MicrosecondsImplicit LLTimer::getTotalTime()
{
// simply call into the implementation function.
return totalTime();
U64MicrosecondsImplicit total_time = totalTime();
return total_time;
}
// static
F64 LLTimer::getTotalSeconds()
F64SecondsImplicit LLTimer::getTotalSeconds()
{
return U64_to_F64(getTotalTime()) * USEC_TO_SEC_F64;
return F64Microseconds(U64_to_F64(getTotalTime()));
}
void LLTimer::reset()
@@ -343,43 +365,43 @@ U64 getElapsedTimeAndUpdate(U64& lastClockCount)
}
F64 LLTimer::getElapsedTimeF64() const
F64SecondsImplicit LLTimer::getElapsedTimeF64() const
{
U64 last = mLastClockCount;
return (F64)getElapsedTimeAndUpdate(last) * gClockFrequencyInv;
return (F64)getElapsedTimeAndUpdate(last) * get_timer_info().mClockFrequencyInv;
}
F32 LLTimer::getElapsedTimeF32() const
F32SecondsImplicit LLTimer::getElapsedTimeF32() const
{
return (F32)getElapsedTimeF64();
}
F64 LLTimer::getElapsedTimeAndResetF64()
F64SecondsImplicit LLTimer::getElapsedTimeAndResetF64()
{
return (F64)getElapsedTimeAndUpdate(mLastClockCount) * gClockFrequencyInv;
return (F64)getElapsedTimeAndUpdate(mLastClockCount) * get_timer_info().mClockFrequencyInv;
}
F32 LLTimer::getElapsedTimeAndResetF32()
F32SecondsImplicit LLTimer::getElapsedTimeAndResetF32()
{
return (F32)getElapsedTimeAndResetF64();
}
///////////////////////////////////////////////////////////////////////////////
void LLTimer::setTimerExpirySec(F32 expiration)
void LLTimer::setTimerExpirySec(F32SecondsImplicit expiration)
{
mExpirationTicks = get_clock_count()
+ (U64)((F32)(expiration * gClockFrequency));
+ (U64)((F32)(expiration * get_timer_info().mClockFrequency.value()));
}
F32 LLTimer::getRemainingTimeF32() const
F32SecondsImplicit LLTimer::getRemainingTimeF32() const
{
U64 cur_ticks = get_clock_count();
if (cur_ticks > mExpirationTicks)
{
return 0.0f;
}
return F32((mExpirationTicks - cur_ticks) * gClockFrequencyInv);
return F32((mExpirationTicks - cur_ticks) * get_timer_info().mClockFrequencyInv);
}
@@ -392,7 +414,7 @@ BOOL LLTimer::checkExpirationAndReset(F32 expiration)
}
mExpirationTicks = cur_ticks
+ (U64)((F32)(expiration * gClockFrequency));
+ (U64)((F32)(expiration * get_timer_info().mClockFrequency));
return TRUE;
}
@@ -491,20 +513,20 @@ BOOL is_daylight_savings()
struct tm* utc_to_pacific_time(time_t utc_time, BOOL pacific_daylight_time)
{
S32 pacific_offset_hours;
S32Hours pacific_offset_hours;
if (pacific_daylight_time)
{
pacific_offset_hours = 7;
pacific_offset_hours = S32Hours(7);
}
else
{
pacific_offset_hours = 8;
pacific_offset_hours = S32Hours(8);
}
// We subtract off the PST/PDT offset _before_ getting
// "UTC" time, because this will handle wrapping around
// for 5 AM UTC -> 10 PM PDT of the previous day.
utc_time -= pacific_offset_hours * MIN_PER_HOUR * SEC_PER_MIN;
utc_time -= S32SecondsImplicit(pacific_offset_hours);
// Internal buffer to PST/PDT (see above)
struct tm* internal_time = gmtime(&utc_time);
@@ -521,7 +543,7 @@ struct tm* utc_to_pacific_time(time_t utc_time, BOOL pacific_daylight_time)
}
void microsecondsToTimecodeString(U64 current_time, std::string& tcstring)
void microsecondsToTimecodeString(U64MicrosecondsImplicit current_time, std::string& tcstring)
{
U64 hours;
U64 minutes;
@@ -543,9 +565,9 @@ void microsecondsToTimecodeString(U64 current_time, std::string& tcstring)
}
void secondsToTimecodeString(F32 current_time, std::string& tcstring)
void secondsToTimecodeString(F32SecondsImplicit current_time, std::string& tcstring)
{
microsecondsToTimecodeString((U64)((F64)(SEC_TO_MICROSEC*current_time)), tcstring);
microsecondsToTimecodeString(current_time, tcstring);
}