Integer-Overflow-Builtins.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Integer Overflow Builtins</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Integer Overflow Builtins">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Integer Overflow Builtins">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="x86-specific-memory-model-extensions-for-transactional-memory.html#x86-specific-memory-model-extensions-for-transactional-memory" rel="next" title="x86 specific memory model extensions for transactional memory">
  31. <link href="_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins" rel="prev" title="__atomic Builtins">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Integer-Overflow-Builtins"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="x86-specific-memory-model-extensions-for-transactional-memory.html#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.html#g_t_005f_005fatomic-Builtins" accesskey="p" rel="prev">__atomic Builtins</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Built_002din-Functions-to-Perform-Arithmetic-with-Overflow-Checking"></a>
  69. <h3 class="section">6.54 Built-in Functions to Perform Arithmetic with Overflow Checking</h3>
  70. <p>The following built-in functions allow performing simple arithmetic operations
  71. together with checking whether the operations overflowed.
  72. </p>
  73. <dl>
  74. <dt><a name="index-_005f_005fbuiltin_005fadd_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_add_overflow</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> *res)</em></dt>
  75. <dt><a name="index-_005f_005fbuiltin_005fsadd_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_sadd_overflow</strong> <em>(int a, int b, int *res)</em></dt>
  76. <dt><a name="index-_005f_005fbuiltin_005fsaddl_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_saddl_overflow</strong> <em>(long int a, long int b, long int *res)</em></dt>
  77. <dt><a name="index-_005f_005fbuiltin_005fsaddll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_saddll_overflow</strong> <em>(long long int a, long long int b, long long int *res)</em></dt>
  78. <dt><a name="index-_005f_005fbuiltin_005fuadd_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_uadd_overflow</strong> <em>(unsigned int a, unsigned int b, unsigned int *res)</em></dt>
  79. <dt><a name="index-_005f_005fbuiltin_005fuaddl_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_uaddl_overflow</strong> <em>(unsigned long int a, unsigned long int b, unsigned long int *res)</em></dt>
  80. <dt><a name="index-_005f_005fbuiltin_005fuaddll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_uaddll_overflow</strong> <em>(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</em></dt>
  81. <dd>
  82. <p>These built-in functions promote the first two operands into infinite precision signed
  83. type and perform addition on those promoted operands. The result is then
  84. cast to the type the third pointer argument points to and stored there.
  85. If the stored result is equal to the infinite precision result, the built-in
  86. functions return false, otherwise they return true. As the addition is
  87. performed in infinite signed precision, these built-in functions have fully defined
  88. behavior for all argument values.
  89. </p>
  90. <p>The first built-in function allows arbitrary integral types for operands and
  91. the result type must be pointer to some integral type other than enumerated or
  92. boolean type, the rest of the built-in functions have explicit integer types.
  93. </p>
  94. <p>The compiler will attempt to use hardware instructions to implement
  95. these built-in functions where possible, like conditional jump on overflow
  96. after addition, conditional jump on carry etc.
  97. </p>
  98. </dd></dl>
  99. <dl>
  100. <dt><a name="index-_005f_005fbuiltin_005fsub_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_sub_overflow</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> *res)</em></dt>
  101. <dt><a name="index-_005f_005fbuiltin_005fssub_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_ssub_overflow</strong> <em>(int a, int b, int *res)</em></dt>
  102. <dt><a name="index-_005f_005fbuiltin_005fssubl_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_ssubl_overflow</strong> <em>(long int a, long int b, long int *res)</em></dt>
  103. <dt><a name="index-_005f_005fbuiltin_005fssubll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_ssubll_overflow</strong> <em>(long long int a, long long int b, long long int *res)</em></dt>
  104. <dt><a name="index-_005f_005fbuiltin_005fusub_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_usub_overflow</strong> <em>(unsigned int a, unsigned int b, unsigned int *res)</em></dt>
  105. <dt><a name="index-_005f_005fbuiltin_005fusubl_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_usubl_overflow</strong> <em>(unsigned long int a, unsigned long int b, unsigned long int *res)</em></dt>
  106. <dt><a name="index-_005f_005fbuiltin_005fusubll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_usubll_overflow</strong> <em>(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</em></dt>
  107. <dd>
  108. <p>These built-in functions are similar to the add overflow checking built-in
  109. functions above, except they perform subtraction, subtract the second argument
  110. from the first one, instead of addition.
  111. </p>
  112. </dd></dl>
  113. <dl>
  114. <dt><a name="index-_005f_005fbuiltin_005fmul_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_mul_overflow</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> *res)</em></dt>
  115. <dt><a name="index-_005f_005fbuiltin_005fsmul_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_smul_overflow</strong> <em>(int a, int b, int *res)</em></dt>
  116. <dt><a name="index-_005f_005fbuiltin_005fsmull_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_smull_overflow</strong> <em>(long int a, long int b, long int *res)</em></dt>
  117. <dt><a name="index-_005f_005fbuiltin_005fsmulll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_smulll_overflow</strong> <em>(long long int a, long long int b, long long int *res)</em></dt>
  118. <dt><a name="index-_005f_005fbuiltin_005fumul_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_umul_overflow</strong> <em>(unsigned int a, unsigned int b, unsigned int *res)</em></dt>
  119. <dt><a name="index-_005f_005fbuiltin_005fumull_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_umull_overflow</strong> <em>(unsigned long int a, unsigned long int b, unsigned long int *res)</em></dt>
  120. <dt><a name="index-_005f_005fbuiltin_005fumulll_005foverflow"></a>Built-in Function: <em>bool</em> <strong>__builtin_umulll_overflow</strong> <em>(unsigned long long int a, unsigned long long int b, unsigned long long int *res)</em></dt>
  121. <dd>
  122. <p>These built-in functions are similar to the add overflow checking built-in
  123. functions above, except they perform multiplication, instead of addition.
  124. </p>
  125. </dd></dl>
  126. <p>The following built-in functions allow checking if simple arithmetic operation
  127. would overflow.
  128. </p>
  129. <dl>
  130. <dt><a name="index-_005f_005fbuiltin_005fadd_005foverflow_005fp"></a>Built-in Function: <em>bool</em> <strong>__builtin_add_overflow_p</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> c)</em></dt>
  131. <dt><a name="index-_005f_005fbuiltin_005fsub_005foverflow_005fp"></a>Built-in Function: <em>bool</em> <strong>__builtin_sub_overflow_p</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> c)</em></dt>
  132. <dt><a name="index-_005f_005fbuiltin_005fmul_005foverflow_005fp"></a>Built-in Function: <em>bool</em> <strong>__builtin_mul_overflow_p</strong> <em>(<var>type1</var> a, <var>type2</var> b, <var>type3</var> c)</em></dt>
  133. <dd>
  134. <p>These built-in functions are similar to <code>__builtin_add_overflow</code>,
  135. <code>__builtin_sub_overflow</code>, or <code>__builtin_mul_overflow</code>, except that
  136. they don&rsquo;t store the result of the arithmetic operation anywhere and the
  137. last argument is not a pointer, but some expression with integral type other
  138. than enumerated or boolean type.
  139. </p>
  140. <p>The built-in functions promote the first two operands into infinite precision signed type
  141. and perform addition on those promoted operands. The result is then
  142. cast to the type of the third argument. If the cast result is equal to the infinite
  143. precision result, the built-in functions return false, otherwise they return true.
  144. The value of the third argument is ignored, just the side-effects in the third argument
  145. are evaluated, and no integral argument promotions are performed on the last argument.
  146. If the third argument is a bit-field, the type used for the result cast has the
  147. precision and signedness of the given bit-field, rather than precision and signedness
  148. of the underlying type.
  149. </p>
  150. <p>For example, the following macro can be used to portably check, at
  151. compile-time, whether or not adding two constant integers will overflow,
  152. and perform the addition only when it is known to be safe and not to trigger
  153. a <samp>-Woverflow</samp> warning.
  154. </p>
  155. <div class="smallexample">
  156. <pre class="smallexample">#define INT_ADD_OVERFLOW_P(a, b) \
  157. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  158. enum {
  159. A = INT_MAX, B = 3,
  160. C = INT_ADD_OVERFLOW_P (A, B) ? 0 : A + B,
  161. D = __builtin_add_overflow_p (1, SCHAR_MAX, (signed char) 0)
  162. };
  163. </pre></div>
  164. <p>The compiler will attempt to use hardware instructions to implement
  165. these built-in functions where possible, like conditional jump on overflow
  166. after addition, conditional jump on carry etc.
  167. </p>
  168. </dd></dl>
  169. <hr>
  170. <div class="header">
  171. <p>
  172. Next: <a href="x86-specific-memory-model-extensions-for-transactional-memory.html#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.html#g_t_005f_005fatomic-Builtins" accesskey="p" rel="prev">__atomic Builtins</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  173. </div>
  174. </body>
  175. </html>