Skip to content

Commit 7990714

Browse files
strssndktndavem330
authored andcommitted
overflow-arith: begin to add support for overflow builtin functions
The idea of the overflow-arith.h header is to collect overflow checking functions in one central place. If gcc compiler supports the __builtin_overflow_* builtins we use them because they might give better performance, otherwise the code falls back to normal overflow checking functions. The builtin_overflow functions are supported by gcc-5 and clang. The matter of supporting clang is to just provide a corresponding CC_HAVE_BUILTIN_OVERFLOW, because the specific overflow checking builtins don't differ between gcc and clang. I just provide overflow_usub function here as I intend this to get merged into net, more functions will definitely follow as they are needed. Signed-off-by: Hannes Frederic Sowa <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c80dbe0 commit 7990714

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

include/linux/compiler-gcc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@
237237
#define KASAN_ABI_VERSION 3
238238
#endif
239239

240+
#if GCC_VERSION >= 50000
241+
#define CC_HAVE_BUILTIN_OVERFLOW
242+
#endif
243+
240244
#endif /* gcc version >= 40000 specific checks */
241245

242246
#if !defined(__noclone)

include/linux/overflow-arith.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <linux/kernel.h>
4+
5+
#ifdef CC_HAVE_BUILTIN_OVERFLOW
6+
7+
#define overflow_usub __builtin_usub_overflow
8+
9+
#else
10+
11+
static inline bool overflow_usub(unsigned int a, unsigned int b,
12+
unsigned int *res)
13+
{
14+
*res = a - b;
15+
return *res > a ? true : false;
16+
}
17+
18+
#endif

0 commit comments

Comments
 (0)