mirror of
https://github.com/ArduPilot/ardupilot.git
synced 2026-10-06 19:00:27 +08:00
AP_Math: Move conversion utilites next to AP_Math
* This is next to the constraining functions Signed-off-by: Ryan Friedman <ryanfriedman5410+github@gmail.com>
This commit is contained in:
committed by
Andrew Tridgell
parent
0faf1a2152
commit
cf2b65877e
@@ -526,3 +526,40 @@ int32_t double_to_int32(const double v)
|
||||
{
|
||||
return int32_t(constrain_double(v, INT32_MIN, UINT32_MAX));
|
||||
}
|
||||
|
||||
|
||||
int32_t float_to_int32_le(const float& value)
|
||||
{
|
||||
int32_t out;
|
||||
static_assert(sizeof(value) == sizeof(out));
|
||||
|
||||
// Use memcpy because it's the most portable.
|
||||
// It might not be the fastest way on all hardware.
|
||||
// At least it's defined behavior in both c and c++.
|
||||
memcpy(&out, &value, sizeof(out));
|
||||
return out;
|
||||
}
|
||||
|
||||
float int32_to_float_le(const uint32_t& value)
|
||||
{
|
||||
float out;
|
||||
static_assert(sizeof(value) == sizeof(out));
|
||||
|
||||
// Use memcpy because it's the most portable.
|
||||
// It might not be the fastest way on all hardware.
|
||||
// At least it's defined behavior in both c and c++.
|
||||
memcpy(&out, &value, sizeof(out));
|
||||
return out;
|
||||
}
|
||||
|
||||
double uint64_to_double_le(const uint64_t& value)
|
||||
{
|
||||
double out;
|
||||
static_assert(sizeof(value) == sizeof(out));
|
||||
|
||||
// Use memcpy because it's the most portable.
|
||||
// It might not be the fastest way on all hardware.
|
||||
// At least it's defined behavior in both c and c++.
|
||||
memcpy(&out, &value, sizeof(out));
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user