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:
Ryan Friedman
2023-06-05 09:09:13 +10:00
committed by Andrew Tridgell
parent 0faf1a2152
commit cf2b65877e
2 changed files with 52 additions and 1 deletions
+37
View File
@@ -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;
}