Optimize osl_getSystemTime on Windows

Make OffTime static const; don't cast from FILETIME to __int64 (see
https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
for explanation: "it can cause alignment faults on 64-bit Windows").
Instead, cast in opposite direction: from 8-byte-aligned 64-bit integer
to FILETIME.

Change-Id: Iba61cc0198f8f25ef471d87e661c8801724b913d
Reviewed-on: https://gerrit.libreoffice.org/75256
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2019-07-09 01:09:35 +02:00
parent e22adfad19
commit f65905dd0f

View File

@@ -29,9 +29,7 @@
sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal) sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
{ {
SYSTEMTIME SystemTime; unsigned __int64 CurTime;
FILETIME CurTime, OffTime;
__int64 Value;
typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME); typedef VOID (WINAPI *GetSystemTimePreciseAsFileTime_PROC)(LPFILETIME);
@@ -46,25 +44,31 @@ sal_Bool SAL_CALL osl_getSystemTime(TimeValue* pTimeVal)
// use ~1 microsecond resolution if available // use ~1 microsecond resolution if available
if (pGetSystemTimePreciseAsFileTime) if (pGetSystemTimePreciseAsFileTime)
pGetSystemTimePreciseAsFileTime(&CurTime); pGetSystemTimePreciseAsFileTime(reinterpret_cast<LPFILETIME>(&CurTime));
else else
{ {
SYSTEMTIME SystemTime;
GetSystemTime(&SystemTime); GetSystemTime(&SystemTime);
SystemTimeToFileTime(&SystemTime, &CurTime); SystemTimeToFileTime(&SystemTime, reinterpret_cast<LPFILETIME>(&CurTime));
} }
SystemTime.wYear = 1970; static const unsigned __int64 OffTime = [] {
SystemTime.wMonth = 1; SYSTEMTIME SystemTime;
SystemTime.wDayOfWeek = 0; SystemTime.wYear = 1970;
SystemTime.wDay = 1; SystemTime.wMonth = 1;
SystemTime.wHour = 0; SystemTime.wDayOfWeek = 0;
SystemTime.wMinute = 0; SystemTime.wDay = 1;
SystemTime.wSecond = 0; SystemTime.wHour = 0;
SystemTime.wMilliseconds = 0; SystemTime.wMinute = 0;
SystemTime.wSecond = 0;
SystemTime.wMilliseconds = 0;
SystemTimeToFileTime(&SystemTime, &OffTime); unsigned __int64 ft;
SystemTimeToFileTime(&SystemTime, reinterpret_cast<LPFILETIME>(&ft));
return ft;
}();
Value = *reinterpret_cast<__int64 *>(&CurTime) - *reinterpret_cast<__int64 *>(&OffTime); const unsigned __int64 Value = CurTime - OffTime;
pTimeVal->Seconds = static_cast<unsigned long>(Value / 10000000L); pTimeVal->Seconds = static_cast<unsigned long>(Value / 10000000L);
pTimeVal->Nanosec = static_cast<unsigned long>((Value % 10000000L) * 100); pTimeVal->Nanosec = static_cast<unsigned long>((Value % 10000000L) * 100);