]> git.sesse.net Git - ffmpeg/commitdiff
avutil/common: Implement av_sat_add64_c() with fewer branches
authorMichael Niedermayer <michael@niedermayer.cc>
Mon, 19 Oct 2020 08:20:26 +0000 (10:20 +0200)
committerMichael Niedermayer <michael@niedermayer.cc>
Sat, 24 Oct 2020 17:11:12 +0000 (19:11 +0200)
No benchmark because this is not used in any speed relevant pathes nor is it
used where __builtin_add_overflow is available.
So I do not know how to realistically benchmark it.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
libavutil/common.h

index 92b721a59c48d41685a486737994ae95b2a799ef..b9fbcc4d6097fb1cde1e3c5983da948bde704c73 100644 (file)
@@ -303,11 +303,10 @@ static av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
     int64_t tmp;
     return !__builtin_add_overflow(a, b, &tmp) ? tmp : (tmp < 0 ? INT64_MAX : INT64_MIN);
 #else
-    if (b >= 0 && a >= INT64_MAX - b)
-        return INT64_MAX;
-    if (b <= 0 && a <= INT64_MIN - b)
-        return INT64_MIN;
-    return a + b;
+    int64_t s = a+(uint64_t)b;
+    if ((int64_t)(a^b | ~s^b) >= 0)
+        return INT64_MAX ^ (b >> 63);
+    return s;
 #endif
 }