From 82954f823bc83c193be8c4d63c4bb2928b471a22 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 16 Aug 2012 15:18:17 +1000 Subject: [PATCH] SITL: make abs() 16 bit in SITL this will help us find abs() bugs in autotest --- libraries/Desktop/include/wiring.h | 3 ++- libraries/Desktop/support/Arduino.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/libraries/Desktop/include/wiring.h b/libraries/Desktop/include/wiring.h index 0b8710eef7..8cf2fd7023 100644 --- a/libraries/Desktop/include/wiring.h +++ b/libraries/Desktop/include/wiring.h @@ -74,7 +74,6 @@ extern "C"{ #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) -#define abs(x) ((x)>0?(x):-(x)) #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) //#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) #define radians(deg) ((deg)*DEG_TO_RAD) @@ -124,6 +123,8 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); void attachInterrupt(uint8_t, void (*)(void), int mode); void detachInterrupt(uint8_t); +int abs(int v); + #ifdef __cplusplus } // extern "C" #endif diff --git a/libraries/Desktop/support/Arduino.cpp b/libraries/Desktop/support/Arduino.cpp index f82cc37e7c..a0c6c8e017 100644 --- a/libraries/Desktop/support/Arduino.cpp +++ b/libraries/Desktop/support/Arduino.cpp @@ -208,3 +208,12 @@ void runInterrupt(uint8_t inum) interrupt_table[inum].call(); } } + +// this version of abs() is here to ensure it is 16 bit +// which allows us to find abs() bugs in SITL +int abs(int v) +{ + int16_t v16 = (int16_t)v; + if (v16 >= 0) return v16; + return -v16; +}