Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
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 | ||||
22 | 22226 | inline int time2print(int64_t time) { | ||
23 | 22226 | return static_cast<int>(time); | ||
24 | } | |||
25 | ||||
26 | // For the sole purpose of satisfying weird Mac OS + some Win compilers' stdlibs impl... | |||
27 | 2 | constexpr 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 | 2 | return std::isfinite(f); | ||
37 | #endif | |||
38 | } | |||
39 | ||||
40 | 2 | constexpr bool _assertFloatFitsInto32BitsAndCast(float value) { | ||
41 | 2 | constexpr auto FirstUnrepresentableBigFloat = static_cast<float>(INT32_MAX); | ||
42 | 2 | 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 |
3/6✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
2 | return constexpr_isfinite(value) && value >= kInt32MinF && value < FirstUnrepresentableBigFloat; | |
52 | } | |||
53 | ||||
54 | static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX) == false); | |||
55 | static_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) | |||
59 | static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 1) == false); | |||
60 | static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 63) == false); | |||
61 | static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MAX - 64) == true); | |||
62 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 1)) == false); | |||
63 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 63)) == false); | |||
64 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MAX - 64)) == true); | |||
65 | ||||
66 | // For INT32_MIN cases | |||
67 | static_assert(_assertFloatFitsInto32BitsAndCast(INT32_MIN) == true); | |||
68 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MIN)) == true); | |||
69 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(INT32_MIN) - 0.1f) == true); | |||
70 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN))) == true); | |||
71 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 1)) == true); | |||
72 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 128)) == true); | |||
73 | static_assert(_assertFloatFitsInto32BitsAndCast(static_cast<float>(static_cast<int64_t>(INT32_MIN) - 129)) == false); | |||
74 | ||||
75 | // Extreme negative float (definitely invalid) | |||
76 | static_assert(_assertFloatFitsInto32BitsAndCast(-1e38f) == false); | |||
77 | // NaN | |||
78 | static_assert(!_assertFloatFitsInto32BitsAndCast(NAN)); | |||
79 | // -Infinity | |||
80 | static_assert(!_assertFloatFitsInto32BitsAndCast(-INFINITY)); | |||
81 | ||||
82 | 2 | constexpr int32_t assertFloatFitsInto32BitsAndCast(const char *msg, float value) { | ||
83 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2 times.
|
2 | if (!_assertFloatFitsInto32BitsAndCast(value)) { |
84 | ✗ | criticalError("%s value is not representable in 32bit int: %f", msg, value); | ||
85 | } | |||
86 | ||||
87 | 2 | return static_cast<int32_t>(value); | ||
88 | } | |||
89 | ||||
90 | 22659 | inline /*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 | 22659 | 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 | */ | |||
117 | uint32_t getTimeNowLowerNt(); | |||
118 | ||||
119 | /** | |||
120 | * @brief Returns the 32 bit number of milliseconds since the board initialization. | |||
121 | */ | |||
122 | efitimems_t getTimeNowMs(); | |||
123 | ||||
124 | /** | |||
125 | * @brief Current system time in seconds (32 bits) | |||
126 | */ | |||
127 | efitimesec_t getTimeNowS(); | |||
128 | ||||
129 | #if EFI_UNIT_TEST | |||
130 | void setTimeNowUs(int us); | |||
131 | void advanceTimeUs(int us); | |||
132 | #endif | |||
133 |