This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Crosspost from compiler forum
Hi!
Embedded abs function have unexpected behavior. When I call abs(some_big_int64-other_big_int64) I expect
1) Calculate difference
2) Strip int64 value to int
3) return abs value
But result is very strange. Abs function return NEGATIVE value! I thest this code on TMS570LS0914 using arm_15.12.3.LTS
static int abs_ (int x) { if (x < 0) { return -x; } return x; } int main() { int64_t v1, v2, v3, v4, v5, v6, v7; v1 = 0x7FFFFFFFUL; v2 = v1 + 1; v3 = abs (v2 - v1); //!!!!! v3=-1 v4 = v2 - v1; v5 = abs (v4); v6 = abs_ (v2 - v1); v7 = abs_ (v4); LOG_WARNING (SYS, "v1=%" PRId64 " 0x%" PRIX64, v1, v1); //v1=2147483647 0x7FFFFFFF LOG_WARNING (SYS, "v2=%" PRId64 " 0x%" PRIX64, v2, v2); //v2=2147483648 0x80000000 LOG_WARNING (SYS, "v3=%" PRId64 " 0x%" PRIX64, v3, v3); //v3=-1 0xFFFFFFFFFFFFFFFF LOG_WARNING (SYS, "v4=%" PRId64 " 0x%" PRIX64, v4, v4); //v4=1 0x1 LOG_WARNING (SYS, "v5=%" PRId64 " 0x%" PRIX64, v5, v5); //v5=1 0x1 LOG_WARNING (SYS, "v6=%" PRId64 " 0x%" PRIX64, v6, v6); //v6=1 0x1 LOG_WARNING (SYS, "v7=%" PRId64 " 0x%" PRIX64, v7, v7); //v7=1 0x1 }
The variables are of type int64_t, which is long long for the ARM compiler.Vladimir Romanov said:But result is very strange. Abs function return NEGATIVEvalue!
The abs function operates on type int (32 bits for the ARM compiler).
Given that the variables are long long try using llabs instead of abs, where llabs operates on type long long.