Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active November 17, 2016 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-nissie/a939d8c856cea1bd936eb5d61d9979f1 to your computer and use it in GitHub Desktop.
Save t-nissie/a939d8c856cea1bd936eb5d61d9979f1 to your computer and use it in GitHub Desktop.
Qiitaの卜部昌平著『Cで飽和++』のコードのコピペ
/* saturated_inc(x) http://qiita.com/shyouhei/items/e960e641882da80c8932 */
#if __GNUC__ >= 7
# define saturated_inc(x) __builtin_add_overflow_p(x, 1, x) ? x : ++x
#else
# define saturated_inc(x) ({__typeof__(x) y; __builtin_add_overflow(x, 1, &y) ? x : ++x; })
#endif
signed long long
func(signed long long x)
{
return saturated_inc(x);
}
//Local variables:
//compile-command: "gcc -O3 -S -o- saturated.c"
//End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment