Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TimeVal more optimizer-friendly #751

Merged
merged 1 commit into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion trikControl/src/event.cpp
Expand Up @@ -27,7 +27,7 @@ EventCodeInterface *Event::code(int codeNum)
{
if (!mEventCodes.contains(codeNum)) {
const QSharedPointer<EventCode> eventCode(new EventCode(codeNum));
connect(this, SIGNAL(on(int, int, int)), eventCode.data(), SLOT(onEvent(int, int, int)));
connect(this, &Event::on, eventCode.data(), &EventCode::onEvent);
mEventCodes.insert(codeNum, eventCode);
}

Expand Down
42 changes: 30 additions & 12 deletions trikKernel/include/trikKernel/timeVal.h
Expand Up @@ -19,7 +19,7 @@
namespace trikKernel {

/// Structure of a time value in a convenient format.
class TRIKKERNEL_EXPORT TimeVal
class TimeVal
{
public:

Expand All @@ -29,7 +29,9 @@ class TRIKKERNEL_EXPORT TimeVal
/// in most cases.
/// Formula for translation : mTime = (sec * 10^6 + mcsec) << 8 = sec * 10^6 << mShift + mcsec << mShift =
/// = sec * mSecConst << (mShift - 6) + mcsec << mShift
TimeVal(int sec, int mcsec);
TimeVal(int sec, int mcsec) {
mTime = ((sec * mSecConst) >> (mShift - 6)) + (mcsec >> mShift);
}

/// Returns packed data that shifted to the left on mShift bits.
uint32_t packedUInt32() const;
Expand All @@ -40,18 +42,13 @@ class TRIKKERNEL_EXPORT TimeVal

/// Counts time interval between two packed data of time
static int timeInterval(int packedTimeLeft, int packedTimeRight);
#if QT_VERSION < 0x050000
/// This method is used in qRegisterMetaType() method and needs default constructor.
/// It is a friend method for hiding default constructor.
friend void *qMetaTypeConstructHelper<TimeVal>(const TimeVal *t);
#else

friend struct QtMetaTypePrivate::QMetaTypeFunctionHelper<TimeVal>;
#endif

/// "Minus" operator is for computing time interval between two timestamps, returns value in microsends.
/// @param left - a value before sign, usually "time after event".
/// @param right - a value after sign, usually "time before event".
friend int operator-(const TimeVal &left, const TimeVal &right);
friend int operator-(TimeVal left, TimeVal right);

private:
TimeVal() = default;
Expand All @@ -60,15 +57,36 @@ class TRIKKERNEL_EXPORT TimeVal

uint32_t mTime = 0;

static const uint32_t mSecConst = 15625;
static const uint32_t mShift = 8;
static constexpr uint32_t mSecConst = 15625;
static constexpr uint32_t mShift = 8;
};

inline int operator-(const TimeVal &left, const TimeVal &right)
inline int operator-(TimeVal left, TimeVal right)
{
return (left.mTime - right.mTime) << TimeVal::mShift;
}

inline uint32_t TimeVal::packedUInt32() const
{
static_assert(sizeof(mTime) == sizeof(TimeVal), "TimeVal must contain only single uint32_t");
return mTime;
}

inline TimeVal TimeVal::fromPackedUInt32(uint32_t packedTime)
{
return TimeVal(packedTime);
}

inline int TimeVal::timeInterval(int packedTimeLeft, int packedTimeRight)
{
return (packedTimeLeft - packedTimeRight) << mShift;
}

inline TimeVal::TimeVal(int packedTime)
: mTime(packedTime)
{}


}

Q_DECLARE_METATYPE(trikKernel::TimeVal)
41 changes: 0 additions & 41 deletions trikKernel/src/timeVal.cpp

This file was deleted.

1 change: 0 additions & 1 deletion trikKernel/trikKernel.pro
Expand Up @@ -50,7 +50,6 @@ SOURCES += \
$$PWD/src/fileUtils.cpp \
$$PWD/src/loggingHelper.cpp \
$$PWD/src/rcReader.cpp \
$$PWD/src/timeVal.cpp \
$$PWD/src/translationsHelper.cpp \
$$PWD/src/$$PLATFORM/coreDumping.cpp \

Expand Down