xref: /illumos-gate/usr/src/man/man3c/stdc_bit_floor.3c (revision bb9475a199514dcace79d04d02c1eff05d65b94f)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright 2024 Oxide Computer Company
13.\"
14.Dd October 27, 2024
15.Dt STDC_BIT_FLOOR 3C
16.Os
17.Sh NAME
18.Nm stdc_bit_floor ,
19.Nm stdc_bit_floor_uc ,
20.Nm stdc_bit_floor_us ,
21.Nm stdc_bit_floor_ui ,
22.Nm stdc_bit_floor_ul ,
23.Nm stdc_bit_floor_ull
24.Nd find largest power of 2 smaller than value
25.Sh LIBRARY
26.Lb libc
27.Sh SYNOPSIS
28.In stdbit.h
29.Ft generic_value_type
30.Fo stdc_bit_floor
31.Fa "generic_value_type value"
32.Fc
33.Ft "unsigned char"
34.Fo stdc_bit_floor_uc
35.Fa "unsigned char value"
36.Fc
37.Ft "unsigned short"
38.Fo stdc_bit_floor_us
39.Fa "unsigned short value"
40.Fc
41.Ft "unsigned int"
42.Fo stdc_bit_floor_ui
43.Fa "unsigned int value"
44.Fc
45.Ft "unsigned long"
46.Fo stdc_bit_floor_ul
47.Fa "unsigned long value"
48.Fc
49.Ft "unsigned long long"
50.Fo stdc_bit_floor_ull
51.Fa "unsigned long long value"
52.Fc
53.Sh DESCRIPTION
54The
55.Fn stdc_bit_floor
56family of functions determine the largest power of 2 that is not greater
57than
58.Fa value .
59If
60.Fa value
61is 0, then the function returns 0.
62.Pp
63The
64.Fn stdc_bit_floor
65function is generic and will operate on all 8, 16, 32, and 64-bit
66unsigned integers; however, it is only available in C23.
67The other functions all operate on a specific integer type, but
68otherwise behave the same and are available regardless of the C language
69version.
70.Sh RETURN VALUES
71The functions in the
72.Fn stdc_bit_floor
73family returns the largest power of 2 that is not greater than
74.Fa value .
75These functions cannot fail.
76.Sh EXAMPLES
77.Sy Example 1
78Printing the bit floor of an integer.
79.Bd -literal
80#include <stdbit.h>
81#include <stdio.h>
82#include <limits.h>
83
84int
85main(void)
86{
87	printf("0x%x 0x%x 0x%x 0x%llx\en",
88	    stdc_bit_floor_uc(0x2b),
89	    stdc_bit_floor_us(0x1000),
90	    stdc_bit_floor_ui(UINT32_MAX),
91	    stdc_bit_floor_ull(0));
92	return (0);
93}
94.Ed
95.Pp
96When compiled and run, this produces:
97.Bd -literal -offset indent
98$ ./a.out
990x20 0x1000 0x80000000 0x0
100.Ed
101.Sh INTERFACE STABILITY
102.Sy Committed
103.Sh MT-LEVEL
104.Sy Async-Signal-Safe
105.Sh SEE ALSO
106.Xr stdc_bit_ceil 3C ,
107.Xr stdc_bit_width 3C ,
108.Xr stdc_count_ones 3C ,
109.Xr stdc_count_zero 3C ,
110.Xr stdc_first_leading_one 3C ,
111.Xr stdc_first_leading_zero 3C ,
112.Xr stdc_first_trailing_one 3C ,
113.Xr stdc_first_trailing_zero 3C ,
114.Xr stdc_has_single_bit 3C ,
115.Xr stdc_leading_ones 3C ,
116.Xr stdc_leading_zeros 3C ,
117.Xr stdc_trailing_ones 3C ,
118.Xr stdc_trailing_zeros 3C ,
119.Xr stdbit.h 3HEAD
120