Skip to content

Commit

Permalink
overflow-arith: begin to add support for overflow builtin functions
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
strssndktn authored and davem330 committed Oct 23, 2015
1 parent c80dbe0 commit 7990714
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/linux/compiler-gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@
#define KASAN_ABI_VERSION 3
#endif

#if GCC_VERSION >= 50000
#define CC_HAVE_BUILTIN_OVERFLOW
#endif

#endif /* gcc version >= 40000 specific checks */

#if !defined(__noclone)
Expand Down
18 changes: 18 additions & 0 deletions include/linux/overflow-arith.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <linux/kernel.h>

#ifdef CC_HAVE_BUILTIN_OVERFLOW

#define overflow_usub __builtin_usub_overflow

#else

static inline bool overflow_usub(unsigned int a, unsigned int b,
unsigned int *res)
{
*res = a - b;
return *res > a ? true : false;
}

#endif

0 comments on commit 7990714

Please sign in to comment.