Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

.\" $NetBSD: __builtin_constant_p.3,v 1.2 2010/12/19 10:08:44 jruoho Exp $
.\"
.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd December 19, 2010
.Dt __BUILTIN_CONSTANT_P 3
.Os
.Sh NAME
.Nm __builtin_constant_p
.Nd GNU extension to determine compile time constants
.Sh SYNOPSIS
.Ft int
.Fn __builtin_constant_p "value"
.Sh DESCRIPTION
The
.Fn __builtin_constant_p
is a
.Tn GNU
extension for determining whether a value
is known to be constant at compile time.
The function is closely related to the concept of
.Dq constant folding
used by modern optimizing compilers.
.Pp
If the
.Fa value
is known to be a compile-time constant, a value 1 is returned.
If
.Fn __builtin_constant_p
returns 0, the
.Fa value
is not a compile-time constant in the sense that
.Xr gcc 1
was unable to determine whether the value is constant or not.
.Sh EXAMPLES
A typical example of the use of
.Fn __builtin_constant_p
involves a situation where it may be desirable to
fold the computation if it involves a constant,
but a function call is needed otherwise.
For instance,
.Xr bswap16 3
is defined in
.Nx
as:
.Bd -literal -offset indent
#define bswap16(x) \\
        (__builtin_constant_p((x)) ? \\
         __byte_swap_u16_constant(x) : __BYTE_SWAP_U16_VARIABLE(x))
.Ed
.Sh SEE ALSO
.Xr gcc 1 ,
.Xr __builtin_object_size 3 ,
.Xr __builtin_return_address 3
.Sh CAVEATS
This is a non-standard, compiler-specific extension.