📄 other-builtins.html
字号:
<html lang="en">
<head>
<title>Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included in the section entitled "GNU Free Documentation License".
<p>(a) The FSF's Front-Cover Text is:
<p>A GNU Manual
<p>(b) The FSF's Back-Cover Text is:
<p>You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.-->
</head>
<body>
<div class="node">
<p>
Node:<a name="Other%20Builtins">Other Builtins</a>,
Next:<a rel="next" accesskey="n" href="Target-Builtins.html#Target%20Builtins">Target Builtins</a>,
Previous:<a rel="previous" accesskey="p" href="Vector-Extensions.html#Vector%20Extensions">Vector Extensions</a>,
Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>
<hr><br>
</div>
<h3 class="section">Other built-in functions provided by GCC</h3>
<p>GCC provides a large number of built-in functions other than the ones
mentioned above. Some of these are for internal use in the processing
of exceptions or variable-length argument lists and will not be
documented here because they may change from time to time; we do not
recommend general use of these functions.
<p>The remaining functions are provided for optimization purposes.
<p>GCC includes built-in versions of many of the functions in the standard
C library. The versions prefixed with <code>__builtin_</code> will always be
treated as having the same meaning as the C library function even if you
specify the <code>-fno-builtin</code> option. (see <a href="C-Dialect-Options.html#C%20Dialect%20Options">C Dialect Options</a>)
Many of these functions are only optimized in certain cases; if they are
not optimized in a particular case, a call to the library function will
be emitted.
<p>The functions <code>abort</code>, <code>exit</code>, <code>_Exit</code> and <code>_exit</code>
are recognized and presumed not to return, but otherwise are not built
in. <code>_exit</code> is not recognized in strict ISO C mode (<code>-ansi</code>,
<code>-std=c89</code> or <code>-std=c99</code>). <code>_Exit</code> is not recognized in
strict C89 mode (<code>-ansi</code> or <code>-std=c89</code>). All these functions
have corresponding versions prefixed with <code>__builtin_</code>, which may be
used even in strict C89 mode.
<p>Outside strict ISO C mode, the functions <code>alloca</code>, <code>bcmp</code>,
<code>bzero</code>, <code>index</code>, <code>rindex</code>, <code>ffs</code>, <code>fputs_unlocked</code>,
<code>printf_unlocked</code> and <code>fprintf_unlocked</code> may be handled as
built-in functions. All these functions have corresponding versions
prefixed with <code>__builtin_</code>, which may be used even in strict C89
mode.
<p>The ISO C99 functions <code>conj</code>, <code>conjf</code>, <code>conjl</code>,
<code>creal</code>, <code>crealf</code>, <code>creall</code>, <code>cimag</code>, <code>cimagf</code>,
<code>cimagl</code>, <code>imaxabs</code>, <code>llabs</code>, <code>snprintf</code>,
<code>vscanf</code>, <code>vsnprintf</code> and <code>vsscanf</code> are handled as built-in
functions except in strict ISO C90 mode. There are also built-in
versions of the ISO C99 functions <code>cosf</code>, <code>cosl</code>,
<code>expf</code>, <code>expl</code>, <code>fabsf</code>, <code>fabsl</code>,
<code>logf</code>, <code>logl</code>, <code>sinf</code>, <code>sinl</code>, <code>sqrtf</code>, and
<code>sqrtl</code>, that are recognized in any mode since ISO C90 reserves
these names for the purpose to which ISO C99 puts them. All these
functions have corresponding versions prefixed with <code>__builtin_</code>.
<p>The ISO C90 functions <code>abs</code>, <code>cos</code>, <code>exp</code>, <code>fabs</code>,
<code>fprintf</code>, <code>fputs</code>, <code>labs</code>, <code>log</code>,
<code>memcmp</code>, <code>memcpy</code>,
<code>memset</code>, <code>printf</code>, <code>putchar</code>, <code>puts</code>, <code>scanf</code>,
<code>sin</code>, <code>snprintf</code>, <code>sprintf</code>, <code>sqrt</code>, <code>sscanf</code>,
<code>strcat</code>,
<code>strchr</code>, <code>strcmp</code>, <code>strcpy</code>, <code>strcspn</code>,
<code>strlen</code>, <code>strncat</code>, <code>strncmp</code>, <code>strncpy</code>,
<code>strpbrk</code>, <code>strrchr</code>, <code>strspn</code>, <code>strstr</code>,
<code>vprintf</code> and <code>vsprintf</code> are all
recognized as built-in functions unless <code>-fno-builtin</code> is
specified (or <code>-fno-builtin-</code><var>function</var><code></code> is specified for an
individual function). All of these functions have corresponding
versions prefixed with <code>__builtin_</code>.
<p>GCC provides built-in versions of the ISO C99 floating point comparison
macros that avoid raising exceptions for unordered operands. They have
the same names as the standard macros ( <code>isgreater</code>,
<code>isgreaterequal</code>, <code>isless</code>, <code>islessequal</code>,
<code>islessgreater</code>, and <code>isunordered</code>) , with <code>__builtin_</code>
prefixed. We intend for a library implementor to be able to simply
<code>#define</code> each standard macro to its built-in equivalent.
<p>
<table width="100%">
<tr>
<td align="left">int <b>__builtin_types_compatible_p</b><i> </i>(<i></i><var>type1</var><i>, </i><var>type2</var><i></i>)<i>
</i></td>
<td align="right">Built-in Function</td>
</tr>
</table>
<table width="95%" align="center">
<tr><td>
<p>You can use the built-in function <code>__builtin_types_compatible_p</code> to
determine whether two types are the same.
<p>This built-in function returns 1 if the unqualified versions of the
types <var>type1</var> and <var>type2</var> (which are types, not expressions) are
compatible, 0 otherwise. The result of this built-in function can be
used in integer constant expressions.
<p>This built-in function ignores top level qualifiers (e.g., <code>const</code>,
<code>volatile</code>). For example, <code>int</code> is equivalent to <code>const
int</code>.
<p>The type <code>int[]</code> and <code>int[5]</code> are compatible. On the other
hand, <code>int</code> and <code>char *</code> are not compatible, even if the size
of their types, on the particular architecture are the same. Also, the
amount of pointer indirection is taken into account when determining
similarity. Consequently, <code>short *</code> is not similar to
<code>short **</code>. Furthermore, two types that are typedefed are
considered compatible if their underlying types are compatible.
<p>An <code>enum</code> type is considered to be compatible with another
<code>enum</code> type. For example, <code>enum {foo, bar}</code> is similar to
<code>enum {hot, dog}</code>.
<p>You would typically use this function in code whose execution varies
depending on the arguments' types. For example:
<pre class="smallexample"> #define foo(x) \
({ \
typeof (x) tmp; \
if (__builtin_types_compatible_p (typeof (x), long double)) \
tmp = foo_long_double (tmp); \
else if (__builtin_types_compatible_p (typeof (x), double)) \
tmp = foo_double (tmp); \
else if (__builtin_types_compatible_p (typeof (x), float)) \
tmp = foo_float (tmp); \
else \
abort (); \
tmp; \
})
</pre>
<p><em>Note:</em> This construct is only available for C.
</td></tr>
</table>
<p>
<table width="100%">
<tr>
<td align="left"><var>type</var> <b>__builtin_choose_expr</b><i> </i>(<i></i><var>const_exp</var><i>, </i><var>exp1</var><i>, </i><var>exp2</var><i></i>)<i>
</i></td>
<td align="right">Built-in Function</td>
</tr>
</table>
<table width="95%" align="center">
<tr><td>
<p>You can use the built-in function <code>__builtin_choose_expr</code> to
evaluate code depending on the value of a constant expression. This
built-in function returns <var>exp1</var> if <var>const_exp</var>, which is a
constant expression that must be able to be determined at compile time,
is nonzero. Otherwise it returns 0.
<p>This built-in function is analogous to the <code>? :</code> operator in C,
except that the expression returned has its type unaltered by promotion
rules. Also, the built-in function does not evaluate the expression
that was not chosen. For example, if <var>const_exp</var> evaluates to true,
<var>exp2</var> is not evaluated even if it has side-effects.
<p>This built-in function can return an lvalue if the chosen argument is an
lvalue.
<p>If <var>exp1</var> is returned, the return type is the same as <var>exp1</var>'s
type. Similarly, if <var>exp2</var> is returned, its return type is the same
as <var>exp2</var>.
<p>Example:
<pre class="smallexample"> #define foo(x) \
__builtin_choose_expr ( \
__builtin_types_compatible_p (typeof (x), double), \
foo_double (x), \
__builtin_choose_expr ( \
__builtin_types_compatible_p (typeof (x), float), \
foo_float (x), \
/* The void expression results in a compile-time error \
when assigning the result to something. */ \
(void)0))
</pre>
<p><em>Note:</em> This construct is only available for C. Furthermore, the
unused expression (<var>exp1</var> or <var>exp2</var> depending on the value of
<var>const_exp</var>) may still generate syntax errors. This may change in
future revisions.
</td></tr>
</table>
<p>
<table width="100%">
<tr>
<td align="left">int <b>__builtin_constant_p</b><i> </i>(<i></i><var>exp</var><i></i>)<i>
</i></td>
<td align="right">Built-in Function</td>
</tr>
</table>
<table width="95%" align="center">
<tr><td>
You can use the built-in function <code>__builtin_constant_p</code> to
determine if a value is known to be constant at compile-time and hence
that GCC can perform constant-folding on expressions involving that
value. The argument of the function is the value to test. The function
returns the integer 1 if the argument is known to be a compile-time
constant and 0 if it is not known to be a compile-time constant. A
return of 0 does not indicate that the value is <em>not</em> a constant,
but merely that GCC cannot prove it is a constant with the specified
value of the <code>-O</code> option.
<p>You would typically use this function in an embedded application where
memory was a critical resource. If you have some complex calculation,
you may want it to be folded if it involves constants, but need to call
a function if it does not. For example:
<pre class="smallexample"> #define Scale_Value(X) \
(__builtin_constant_p (X) \
? ((X) * SCALE + OFFSET) : Scale (X))
</pre>
<p>You may use this built-in function in either a macro or an inline
function. However, if you use it in an inlined function and pass an
argument of the function as the argument to the built-in, GCC will
never return 1 when you call the inline function with a string constant
or compound literal (see <a href="Compound-Literals.html#Compound%20Literals">Compound Literals</a>) and will not return 1
when you pass a constant numeric value to the inline function unless you
specify the <code>-O</code> option.
<p>You may also use <code>__builtin_constant_p</code> in initializers for static
data. For instance, you can write
<pre class="smallexample"> static const int table[] = {
__builtin_constant_p (EXPRESSION) ? (EXPRESSION) : -1,
/* <small class="dots">...</small> */
};
</pre>
<p>This is an acceptable initializer even if <var>EXPRESSION</var> is not a
constant expression. GCC must be more conservative about evaluating the
built-in in this case, because it has no opportunity to perform
optimization.
<p>Previous versions of GCC did not accept this built-in in data
initializers. The earliest version where it is completely safe is
3.0.1.
</td></tr>
</table>
<p>
<table width="100%">
<tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -