Browse Source

AP_Math: added unsigned versions of constrain functions

sometimes it really does matter that we use constrain_uint32() instead
of constrain_int32(). For example, if we have a value like 0xFFFFFFFF
then the result will be very different

we should use unsigned constrain when dealing with unsigned values
Copter-4.2
Andrew Tridgell 3 years ago committed by Randy Mackay
parent
commit
1f30cf93cf
  1. 4
      libraries/AP_Math/AP_Math.cpp
  2. 15
      libraries/AP_Math/AP_Math.h
  3. 4
      libraries/AP_Math/tests/test_math.cpp

4
libraries/AP_Math/AP_Math.cpp

@ -313,9 +313,13 @@ T constrain_value(const T amt, const T low, const T high) @@ -313,9 +313,13 @@ T constrain_value(const T amt, const T low, const T high)
}
template int constrain_value<int>(const int amt, const int low, const int high);
template unsigned int constrain_value<unsigned int>(const unsigned int amt, const unsigned int low, const unsigned int high);
template long constrain_value<long>(const long amt, const long low, const long high);
template unsigned long constrain_value<unsigned long>(const unsigned long amt, const unsigned long low, const unsigned long high);
template long long constrain_value<long long>(const long long amt, const long long low, const long long high);
template unsigned long long constrain_value<unsigned long long>(const unsigned long long amt, const unsigned long long low, const unsigned long long high);
template short constrain_value<short>(const short amt, const short low, const short high);
template unsigned short constrain_value<unsigned short>(const unsigned short amt, const unsigned short low, const unsigned short high);
template float constrain_value<float>(const float amt, const float low, const float high);
template double constrain_value<double>(const double amt, const double low, const double high);

15
libraries/AP_Math/AP_Math.h

@ -179,16 +179,31 @@ inline int16_t constrain_int16(const int16_t amt, const int16_t low, const int16 @@ -179,16 +179,31 @@ inline int16_t constrain_int16(const int16_t amt, const int16_t low, const int16
return constrain_value(amt, low, high);
}
inline uint16_t constrain_uint16(const uint16_t amt, const uint16_t low, const uint16_t high)
{
return constrain_value(amt, low, high);
}
inline int32_t constrain_int32(const int32_t amt, const int32_t low, const int32_t high)
{
return constrain_value(amt, low, high);
}
inline uint32_t constrain_uint32(const uint32_t amt, const uint32_t low, const uint32_t high)
{
return constrain_value(amt, low, high);
}
inline int64_t constrain_int64(const int64_t amt, const int64_t low, const int64_t high)
{
return constrain_value(amt, low, high);
}
inline uint64_t constrain_uint64(const uint64_t amt, const uint64_t low, const uint64_t high)
{
return constrain_value(amt, low, high);
}
// degrees -> radians
static inline constexpr ftype radians(ftype deg)
{

4
libraries/AP_Math/tests/test_math.cpp

@ -377,6 +377,10 @@ TEST(MathTest, Constrain) @@ -377,6 +377,10 @@ TEST(MathTest, Constrain)
EXPECT_EQ(19.9, constrain_value(19.8, 19.9, 20.1));
EXPECT_EQ(19.9f, constrain_value(19.8f, 19.9f, 20.1f));
// test that constrain on 32 bit integer works correctly. Note the asymmetry
EXPECT_EQ(10, constrain_int32( 0xFFFFFFFFU, 10U, 1200U));
EXPECT_EQ(1200U, constrain_uint32(0xFFFFFFFFU, 10U, 1200U));
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
EXPECT_EQ(1.0f, constrain_float(nanf("0x4152"), 1.0f, 1.0f));
EXPECT_EQ(1.0f, constrain_value(nanf("0x4152"), 1.0f, 1.0f));

Loading…
Cancel
Save