diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/integer-overflow-builtins.html | |
new repository
Diffstat (limited to 'devdocs/gcc~13/integer-overflow-builtins.html')
| -rw-r--r-- | devdocs/gcc~13/integer-overflow-builtins.html | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/devdocs/gcc~13/integer-overflow-builtins.html b/devdocs/gcc~13/integer-overflow-builtins.html new file mode 100644 index 00000000..b266f733 --- /dev/null +++ b/devdocs/gcc~13/integer-overflow-builtins.html @@ -0,0 +1,66 @@ +<div class="section-level-extent" id="Integer-Overflow-Builtins"> <div class="nav-panel"> <p> Next: <a href="x86-specific-memory-model-extensions-for-transactional-memory" accesskey="n" rel="next">x86-Specific Memory Model Extensions for Transactional Memory</a>, Previous: <a href="_005f_005fatomic-builtins" accesskey="p" rel="prev">Built-in Functions for Memory Model Aware Atomic Operations</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Built-in-Functions-to-Perform-Arithmetic-with-Overflow-Checking"><span>6.56 Built-in Functions to Perform Arithmetic with Overflow Checking<a class="copiable-link" href="#Built-in-Functions-to-Perform-Arithmetic-with-Overflow-Checking"> ¶</a></span></h1> <p>The following built-in functions allow performing simple arithmetic operations together with checking whether the operations overflowed. </p> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fadd_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_add_overflow</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fadd_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsadd_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_sadd_overflow</strong> <code class="def-code-arguments">(int a, int b, int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsadd_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsaddl_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_saddl_overflow</strong> <code class="def-code-arguments">(long int a, long int b, long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsaddl_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsaddll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_saddll_overflow</strong> <code class="def-code-arguments">(long long int a, long long int b, long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsaddll_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fuadd_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_uadd_overflow</strong> <code class="def-code-arguments">(unsigned int a, unsigned int b, unsigned int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fuadd_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fuaddl_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_uaddl_overflow</strong> <code class="def-code-arguments">(unsigned long int a, unsigned long int b, unsigned long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fuaddl_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fuaddll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_uaddll_overflow</strong> <code class="def-code-arguments">(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fuaddll_005foverflow"> ¶</a></span> +</dt> <dd> <p>These built-in functions promote the first two operands into infinite precision signed type and perform addition on those promoted operands. The result is then cast to the type the third pointer argument points to and stored there. If the stored result is equal to the infinite precision result, the built-in functions return <code class="code">false</code>, otherwise they return <code class="code">true</code>. As the addition is performed in infinite signed precision, these built-in functions have fully defined behavior for all argument values. </p> <p>The first built-in function allows arbitrary integral types for operands and the result type must be pointer to some integral type other than enumerated or boolean type, the rest of the built-in functions have explicit integer types. </p> <p>The compiler will attempt to use hardware instructions to implement these built-in functions where possible, like conditional jump on overflow after addition, conditional jump on carry etc. </p> </dd> +</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fsub_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_sub_overflow</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsub_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fssub_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_ssub_overflow</strong> <code class="def-code-arguments">(int a, int b, int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fssub_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fssubl_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_ssubl_overflow</strong> <code class="def-code-arguments">(long int a, long int b, long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fssubl_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fssubll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_ssubll_overflow</strong> <code class="def-code-arguments">(long long int a, long long int b, long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fssubll_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fusub_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_usub_overflow</strong> <code class="def-code-arguments">(unsigned int a, unsigned int b, unsigned int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fusub_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fusubl_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_usubl_overflow</strong> <code class="def-code-arguments">(unsigned long int a, unsigned long int b, unsigned long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fusubl_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fusubll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_usubll_overflow</strong> <code class="def-code-arguments">(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fusubll_005foverflow"> ¶</a></span> +</dt> <dd> <p>These built-in functions are similar to the add overflow checking built-in functions above, except they perform subtraction, subtract the second argument from the first one, instead of addition. </p> </dd> +</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fmul_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_mul_overflow</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fmul_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsmul_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_smul_overflow</strong> <code class="def-code-arguments">(int a, int b, int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsmul_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsmull_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_smull_overflow</strong> <code class="def-code-arguments">(long int a, long int b, long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsmull_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsmulll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_smulll_overflow</strong> <code class="def-code-arguments">(long long int a, long long int b, long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsmulll_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fumul_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_umul_overflow</strong> <code class="def-code-arguments">(unsigned int a, unsigned int b, unsigned int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fumul_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fumull_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_umull_overflow</strong> <code class="def-code-arguments">(unsigned long int a, unsigned long int b, unsigned long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fumull_005foverflow"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fumulll_005foverflow"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_umulll_overflow</strong> <code class="def-code-arguments">(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fumulll_005foverflow"> ¶</a></span> +</dt> <dd> <p>These built-in functions are similar to the add overflow checking built-in functions above, except they perform multiplication, instead of addition. </p> </dd> +</dl> <p>The following built-in functions allow checking if simple arithmetic operation would overflow. </p> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fadd_005foverflow_005fp"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_add_overflow_p</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> c)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fadd_005foverflow_005fp"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fsub_005foverflow_005fp"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_sub_overflow_p</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> c)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsub_005foverflow_005fp"> ¶</a></span> +</dt> <dt class="deftypefnx def-cmd-deftypefn" id="index-_005f_005fbuiltin_005fmul_005foverflow_005fp"> +<span class="category-def">Built-in Function: </span><span><code class="def-type">bool</code> <strong class="def-name">__builtin_mul_overflow_p</strong> <code class="def-code-arguments">(<var class="var">type1</var> a, <var class="var">type2</var> b, <var class="var">type3</var> c)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fmul_005foverflow_005fp"> ¶</a></span> +</dt> <dd> <p>These built-in functions are similar to <code class="code">__builtin_add_overflow</code>, <code class="code">__builtin_sub_overflow</code>, or <code class="code">__builtin_mul_overflow</code>, except that they don’t store the result of the arithmetic operation anywhere and the last argument is not a pointer, but some expression with integral type other than enumerated or boolean type. </p> <p>The built-in functions promote the first two operands into infinite precision signed type and perform addition on those promoted operands. The result is then cast to the type of the third argument. If the cast result is equal to the infinite precision result, the built-in functions return <code class="code">false</code>, otherwise they return <code class="code">true</code>. The value of the third argument is ignored, just the side effects in the third argument are evaluated, and no integral argument promotions are performed on the last argument. If the third argument is a bit-field, the type used for the result cast has the precision and signedness of the given bit-field, rather than precision and signedness of the underlying type. </p> <p>For example, the following macro can be used to portably check, at compile-time, whether or not adding two constant integers will overflow, and perform the addition only when it is known to be safe and not to trigger a <samp class="option">-Woverflow</samp> warning. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define INT_ADD_OVERFLOW_P(a, b) \ + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) + +enum { + A = INT_MAX, B = 3, + C = INT_ADD_OVERFLOW_P (A, B) ? 0 : A + B, + D = __builtin_add_overflow_p (1, SCHAR_MAX, (signed char) 0) +};</pre> +</div> <p>The compiler will attempt to use hardware instructions to implement these built-in functions where possible, like conditional jump on overflow after addition, conditional jump on carry etc. </p> </dd> +</dl> </div> <div class="nav-panel"> <p> Next: <a href="x86-specific-memory-model-extensions-for-transactional-memory">x86-Specific Memory Model Extensions for Transactional Memory</a>, Previous: <a href="_005f_005fatomic-builtins">Built-in Functions for Memory Model Aware Atomic Operations</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution"> + <p class="_attribution-p"> + © Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br> + <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Integer-Overflow-Builtins.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Integer-Overflow-Builtins.html</a> + </p> +</div> |
