rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
efitime.h
Go to the documentation of this file.
1/**
2 * @file efitime.h
3 *
4 * By the way, there are 86400000 milliseconds in a day
5 *
6 * @date Apr 14, 2014
7 * @author Andrey Belomutskiy, (c) 2012-2020
8 */
9
10#pragma once
11
12#include "rusefi_types.h"
13#include "error_handling.h"
14#include <rusefi/rusefi_time_math.h>
15
16
17#if EFI_PROD_CODE
18// for US_TO_NT_MULTIPLIER which is port-specific
19#include "port_mpu_util.h"
20#endif
21
22inline int time2print(int64_t time) {
23 return static_cast<int>(time);
24}
25
26// For the sole purpose of satisfying weird Mac OS + some Win compilers' stdlibs impl...
27constexpr bool constexpr_isfinite(float f) {
28#if __cplusplus >= 202302L
29 // In C++23, std::isfinite is officially constexpr
30 return std::isfinite(f);
31#elif defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
32 // On Windows and macOS, std::isfinite might not be constexpr pre-C++23
33 return true;
34#else
35 // This is meant to correspond to normal/target platforms
36 return std::isfinite(f);
37#endif
38}
39
40constexpr bool _assertFloatFitsInto32BitsAndCast(float value) {
41 constexpr auto FirstUnrepresentableBigFloat = static_cast<float>(INT32_MAX);
42 constexpr auto kInt32MinF = static_cast<float>(INT32_MIN);
43
44 // 32bit float has 24bit precision that lead to fiasco like:
45 // 2147483648.0 > 2147483647 == false
46 // Also at 2147483648.0 we need 2^7=128 step to go to next value of 2147483904.0f
47 // And 2147483648.0f is already non-representable in 32 bit int
48 // So if someone using corner value of max int for smth like invalid float
49 // we might accidentally give green light if we are not strictly under 2147483648.0
50 // i.e. do not change this check to implicit convertion int->float with non strict condition!
51 return constexpr_isfinite(value) && value >= kInt32MinF && value < FirstUnrepresentableBigFloat;
52}
53
54static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX) == false);
55static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX)) == false);
56
57// 64 for int because during conversion it is rounding to nearest
58// i.e. -64 to +63 = 128 or 2^7 because 2^31 (int) / 2^24 (float)
59static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 1) == false);
60static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 63) == false);
61static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 64) == true);
62static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 1)) == false);
63static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 63)) == false);
64static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 64)) == true);
65
66// For INT32_MIN cases
67static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MIN) == true);
68static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MIN)) == true);
69static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MIN) - 0.1f) == true);
70static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN))) == true);
71static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 1)) == true);
72static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 128)) == true);
73static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 129)) == false);
74
75// Extreme negative float (definitely invalid)
76static_assert(_assertFloatFitsInto32BitsAndCast(-1e38f) == false);
77// NaN
78static_assert(!_assertFloatFitsInto32BitsAndCast(NAN));
79// -Infinity
80static_assert(!_assertFloatFitsInto32BitsAndCast(-INFINITY));
81
82constexpr int32_t assertFloatFitsInto32BitsAndCast(const char *msg, float value) {
84 criticalError("%s value is not representable in 32bit int: %f", msg, value);
85 }
86
87 return static_cast<int32_t>(value);
88}
89
90inline /*64bit*/ efitick_t sumTickAndFloat(/*64bit*/efitick_t ticks, /*32bit*/float extra) {
91 // we have a peculiar case of type compiler uses to evaluate expression vs precision here
92 // we need 64 bit precision not (lower) float precision
93 // 32 bits is 11 or 23 seconds if US_TO_NT_MULTIPLIER = 168 like on kinetis/cypress
94 // 32 bits is 500 or 1000 seconds if US_TO_NT_MULTIPLIER = 4 like on stm32
95 // 'extra' is below 10 seconds here so we use 32 bit type for performance reasons
96 return ticks + (int32_t) extra;
97}
98
99// microseconds to ticks
100// since only about 20 seconds of ticks fit in 32 bits this macro is casting parameter into 64 bits 'efitick_t' type
101// please note that int64 <-> float is a heavy operation thus we have 'USF2NT' below
102#define US2NT(us) (((efitick_t)(us)) * US_TO_NT_MULTIPLIER)
103
104// milliseconds to ticks
105#define MS2NT(msTime) US2NT(MS2US(msTime))
106// See USF2NT above for when to use MSF2NT. ***WARNING*** please be aware of sumTickAndFloat
107#define MSF2NT(msTimeFloat) USF2NT(MS2US(msTimeFloat))
108
109/**
110 * Get a monotonically increasing (but wrapping) 32-bit timer value
111 * Implemented at port level, based on timer or CPU tick counter
112 * Main source of EFI clock, SW-extended to 64bits
113 *
114 * 2147483648 / ~168MHz = ~12 seconds to overflow
115 *
116 */
117uint32_t getTimeNowLowerNt();
118
119/**
120 * @brief Returns the 32 bit number of milliseconds since the board initialization.
121 */
123
124/**
125 * @brief Current system time in seconds (32 bits)
126 */
127efitimesec_t getTimeNowS();
128
129#if EFI_UNIT_TEST
130void setTimeNowUs(int us);
131void advanceTimeUs(int us);
132#endif
uint32_t getTimeNowLowerNt()
efitimesec_t getTimeNowS()
Current system time in seconds (32 bits)
Definition efitime.cpp:42
void setTimeNowUs(int us)
int time2print(int64_t time)
Definition efitime.h:22
efitick_t sumTickAndFloat(efitick_t ticks, float extra)
Definition efitime.h:90
constexpr bool _assertFloatFitsInto32BitsAndCast(float value)
Definition efitime.h:40
constexpr int32_t assertFloatFitsInto32BitsAndCast(const char *msg, float value)
Definition efitime.h:82
constexpr bool constexpr_isfinite(float f)
Definition efitime.h:27
void advanceTimeUs(int us)
efitimems_t getTimeNowMs()
Returns the 32 bit number of milliseconds since the board initialization.
Definition efitime.cpp:34
uint32_t efitimems_t