1.\" $NetBSD: __builtin_constant_p.3,v 1.2 2010/12/19 10:08:44 jruoho Exp $
2.\"
3.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\" POSSIBILITY OF SUCH DAMAGE.
26.\"
27.Dd December 19, 2010
28.Dt __BUILTIN_CONSTANT_P 3
29.Os
30.Sh NAME
31.Nm __builtin_constant_p
32.Nd GNU extension to determine compile time constants
33.Sh SYNOPSIS
34.Ft int
35.Fn __builtin_constant_p "value"
36.Sh DESCRIPTION
37The
38.Fn __builtin_constant_p
39is a
40.Tn GNU
41extension for determining whether a value
42is known to be constant at compile time.
43The function is closely related to the concept of
44.Dq constant folding
45used by modern optimizing compilers.
46.Pp
47If the
48.Fa value
49is known to be a compile-time constant, a value 1 is returned.
50If
51.Fn __builtin_constant_p
52returns 0, the
53.Fa value
54is not a compile-time constant in the sense that
55.Xr gcc 1
56was unable to determine whether the value is constant or not.
57.Sh EXAMPLES
58A typical example of the use of
59.Fn __builtin_constant_p
60involves a situation where it may be desirable to
61fold the computation if it involves a constant,
62but a function call is needed otherwise.
63For instance,
64.Xr bswap16 3
65is defined in
66.Nx
67as:
68.Bd -literal -offset indent
69#define bswap16(x) \\
70        (__builtin_constant_p((x)) ? \\
71         __byte_swap_u16_constant(x) : __BYTE_SWAP_U16_VARIABLE(x))
72.Ed
73.Sh SEE ALSO
74.Xr gcc 1 ,
75.Xr __builtin_object_size 3 ,
76.Xr __builtin_return_address 3
77.Sh CAVEATS
78This is a non-standard, compiler-specific extension.
79