Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mock/lib-time-mocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ efitimeus_t getTimeNowUs() {
}

efitimesec_t getTimeNowS() {
return getTimeNowUs() / 1000 / 1000;
return static_cast<efitimeus_t::value_type>(getTimeNowUs()) / 1000 / 1000;
}

efitick_t getTimeNowNt() {
return getTimeNowUs() * US_TO_NT_MULTIPLIER;
return (static_cast<efitimeus_t::value_type>(getTimeNowUs())) * US_TO_NT_MULTIPLIER;
}
17 changes: 15 additions & 2 deletions util/include/rusefi/rusefi_time_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
#include <cstdint>
#include <sys/types.h>

#include "tagged_numeric.h"

enum class time_unit {
MICROSECOND,
};

template <typename arithmetic_value_type, time_unit> struct timestamp_tag_s {
using value_type = arithmetic_value_type;
};

/**
* We use a signed type here so that subtraction result is a proper negative value.
* A typical use-case negative result is when we do 'timeNow() - timeOfEvent' where timeOfEvent
Expand All @@ -24,7 +34,7 @@ using efitick_t = int64_t;
/**
* 64 bit time in microseconds (1/1_000_000 of a second), since boot
*/
using efitimeus_t = int64_t;
using efitimeus_t = tagged_numeric_t<timestamp_tag_s<int64_t, time_unit::MICROSECOND>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can/should we just use std::chrono instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it makes sense. I will rewrite code using std::chrono::duration<int64_t, micros> and std::chrono::time_point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it makes sense. I will rewrite code using std::chrono::duration<int64_t, micros> and std::chrono::time_point.

What about our 1/168_000_000 second ticks and our 1/218_000_000 second tickets depending on target platform?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about our 1/168_000_000 second ticks and our 1/218_000_000 second tickets depending on target platform?

// 4mhz was chosen because it's the GCD of (84, 108, 200), the three speeds of STM32 TIM5 clock currently supported
// https://www.wolframalpha.com/input/?i=common+factors+of+168+180+216
#define US_TO_NT_MULTIPLIER (4)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// time in seconds
using efitimesec_t = time_t;
Expand Down Expand Up @@ -55,4 +65,7 @@ efitick_t getTimeNowNt();
#define US_PER_SECOND_LL 1000000LL

#define MS2US(MS_TIME) ((MS_TIME) * 1000)
#define US2MS(US_TIME) ((US_TIME) / 1000)

inline int US2MS(const efitimeus_t& timeus) {
return static_cast<efitimeus_t::value_type>(timeus) / 1000;
}
32 changes: 32 additions & 0 deletions util/include/rusefi/tagged_numeric.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by kifir on 4/25/24.
//

#pragma once

#include <type_traits>

template<typename tag_type> class tagged_numeric_t {
public:
using value_type = typename tag_type::value_type;

tagged_numeric_t();
tagged_numeric_t(const value_type& val); // implicit to allow init from numeric value (for backward compatibility)

explicit operator value_type() const;
explicit operator int() const;
explicit operator float() const;

bool operator ==(const tagged_numeric_t& val) const;
bool operator ==(const value_type& val) const;
bool operator !=(const value_type& val) const;
bool operator >(const tagged_numeric_t& val) const;

tagged_numeric_t operator -(const tagged_numeric_t& val) const;
tagged_numeric_t operator +(const tagged_numeric_t& val) const;
tagged_numeric_t operator +(const int& val) const;
private:
value_type value;
};

#include "tagged_numeric.inl"
63 changes: 63 additions & 0 deletions util/include/rusefi/tagged_numeric.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
template<typename tag_type>
tagged_numeric_t<tag_type>::tagged_numeric_t()
: tagged_numeric_t(0) {
}

template<typename tag_type>
tagged_numeric_t<tag_type>::tagged_numeric_t(const value_type& val)
: value(val) {
static_assert(std::is_arithmetic_v<value_type>);
static_assert(sizeof(tagged_numeric_t) == sizeof(value_type));
}

template<typename tag_type>
tagged_numeric_t<tag_type>::operator value_type() const {
return value;
}

template<typename tag_type>
tagged_numeric_t<tag_type>::operator int() const {
return static_cast<int>(value);
}

template<typename tag_type>
tagged_numeric_t<tag_type>::operator float() const {
return static_cast<float>(value);
}

template<typename tag_type>
bool tagged_numeric_t<tag_type>::operator ==(const tagged_numeric_t& val) const {
return value == val;
}

template<typename tag_type>
bool tagged_numeric_t<tag_type>::operator ==(const value_type& val) const {
return value == val;
}

template<typename tag_type>
bool tagged_numeric_t<tag_type>::operator !=(const value_type& val) const {
return value != val;
}

template<typename tag_type>
bool tagged_numeric_t<tag_type>::operator >(const tagged_numeric_t& val) const {
return value > val.value;
}

template<typename tag_type>
tagged_numeric_t<tag_type> tagged_numeric_t<tag_type>::operator -(
const tagged_numeric_t& val
) const {
return tagged_numeric_t(value - val.value);
}

template<typename tag_type>
tagged_numeric_t<tag_type> tagged_numeric_t<tag_type>::operator +(const tagged_numeric_t& val) const {
return tagged_numeric_t(value + val.value);
}

template<typename tag_type>
tagged_numeric_t<tag_type> tagged_numeric_t<tag_type>::operator +(const int& val) const {
return tagged_numeric_t(value + val);
}
5 changes: 5 additions & 0 deletions util/src/tagged_numeric.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by kifir on 4/25/24.
//

#include "tagged_numeric.h"