rusefi/firmware/util/efilib.h

128 lines
3.5 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file efilib.h
*
* @date Feb 21, 2014
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
#ifndef EFILIB_H_
#define EFILIB_H_
#include <stdint.h>
2016-08-09 20:03:58 -07:00
#define _MAX_FILLER 11
2015-07-10 06:01:56 -07:00
// http://en.wikipedia.org/wiki/Endianness
#define SWAP_UINT16(x) (((x) << 8) | ((x) >> 8))
2015-07-10 06:01:56 -07:00
#define SWAP_UINT32(x) ((((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000))
2015-07-10 06:01:56 -07:00
// human-readable IDs start from 1 while computer-readbale indexes start from 0
#define ID2INDEX(id) ((id) - 1)
// number of milliseconds in one period of given frequency (per second)
#define frequency2periodMs(freq) ((1000.0f) / (freq))
// number of microseconds in one period of given frequency (per second)
#define frequency2periodUs(freq) ((1000000.0f) / (freq))
#define ERROR_CODE 311223344
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
2017-03-29 18:07:26 -07:00
const char * boolToString(bool value);
2015-07-10 06:01:56 -07:00
char * efiTrim(char *param);
2017-04-04 19:53:58 -07:00
int mytolower(const char c);
2015-07-10 06:01:56 -07:00
uint32_t efiStrlen(const char *param);
int efiPow10(int param);
bool startsWith(const char *line, const char *prefix);
int indexOf(const char *string, char ch);
float atoff(const char *string);
int atoi(const char *string);
#define UNUSED(x) (void)(x)
int absI(int32_t value);
float absF(float value);
2017-12-10 07:19:05 -08:00
/**
* Rounds value to specified precision.
* @param precision some pow of 10 value - for example, 100 for two digit precision
*/
2015-07-10 06:01:56 -07:00
float efiRound(float value, float precision);
int maxI(int i1, int i2);
int minI(int i1, int i2);
float maxF(float i1, float i2);
float minF(float i1, float i2);
char* itoa10(char *p, int num);
bool isSameF(float v1, float v2);
bool strEqualCaseInsensitive(const char *str1, const char *str2);
bool strEqual(const char *str1, const char *str2);
// Currently used by air-interp. tCharge mode (see EngineState::updateTChargeK()).
float limitRateOfChange(float newValue, float oldValue, float incrLimitPerSec, float decrLimitPerSec, float secsPassed);
// @brief Compute e^x using a 4th order taylor expansion centered at x=-1. Provides
// bogus results outside the range -2 < x < 0.
float expf_taylor(float x);
2015-07-10 06:01:56 -07:00
#ifdef __cplusplus
}
2019-11-13 05:42:16 -08:00
#include <cstddef>
2019-11-13 05:42:16 -08:00
// C++ helpers go here
namespace efi
{
template <typename T, size_t N>
constexpr size_t size(const T(&)[N]) {
2019-11-13 05:42:16 -08:00
return N;
}
} // namespace efi
/**
* Copies an array from src to dest. The lengths of the arrays must match.
*/
template <typename TElement, size_t N>
constexpr void copyArray(TElement (&dest)[N], const TElement (&src)[N]) {
for (size_t i = 0; i < N; i++) {
dest[i] = src[i];
}
}
/**
* Copies an array from src to the beginning of dst. If dst is larger
* than src, then only the elements copied from src will be touched.
* Any remaining elements at the end will be untouched.
*/
template <typename TElement, size_t NSrc, size_t NDest>
constexpr void copyArrayPartial(TElement (&dest)[NDest], const TElement (&src)[NSrc]) {
static_assert(NDest >= NSrc, "Source array must be larger than destination.");
for (size_t i = 0; i < NSrc; i++) {
dest[i] = src[i];
}
}
2015-07-10 06:01:56 -07:00
#endif /* __cplusplus */
#if defined(__cplusplus) && defined(__OPTIMIZE__)
#include <type_traits>
// "g++ -O2" version, adds more strict type check and yet no "strict-aliasing" warnings!
#define cisnan(f) ({ \
static_assert(sizeof(f) == sizeof(int32_t)); \
union cisnanu_t { std::remove_reference_t<decltype(f)> __f; int32_t __i; } __cisnan_u = { f }; \
__cisnan_u.__i == 0x7FC00000; \
})
#else
// "g++ -O0" or other C++/C compilers
#define cisnan(f) (*(((int*) (&f))) == 0x7FC00000)
#endif /* __cplusplus && __OPTIMIZE__ */
2015-07-10 06:01:56 -07:00
#endif /* EFILIB_H_ */