xref: /illumos-gate/usr/src/man/man3c/stdc_leading_ones.3c (revision 80b758da23abee3ef0e550f533aada9ce3b1b01f)
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_LEADING_ONES 3C
16.Os
17.Sh NAME
18.Nm stdc_leading_ones ,
19.Nm stdc_leading_ones_uc ,
20.Nm stdc_leading_ones_us ,
21.Nm stdc_leading_ones_ui ,
22.Nm stdc_leading_ones_ul ,
23.Nm stdc_leading_ones_ull
24.Nd count consecutive leading one bits
25.Sh LIBRARY
26.Lb libc
27.Sh SYNOPSIS
28.In stdbit.h
29.Ft "unsigned int"
30.Fo stdc_leading_ones
31.Fa "generic_value_type value"
32.Fc
33.Ft "unsigned int"
34.Fo stdc_leading_ones_uc
35.Fa "unsigned char value"
36.Fc
37.Ft "unsigned int"
38.Fo stdc_leading_ones_us
39.Fa "unsigned short value"
40.Fc
41.Ft "unsigned int"
42.Fo stdc_leading_ones_ui
43.Fa "unsigned int value"
44.Fc
45.Ft "unsigned int"
46.Fo stdc_leading_ones_ul
47.Fa "unsigned long value"
48.Fc
49.Ft "unsigned int"
50.Fo stdc_leading_ones_ull
51.Fa "unsigned long long value"
52.Fc
53.Sh DESCRIPTION
54The
55.Fn stdc_leading_ones
56family of functions counts the number of consecutive one bits present in
57.Fa value
58starting at the most significant bit.
59.Pp
60The
61.Fn stdc_leading_ones
62function is generic and will operate on all 8, 16, 32, and 64-bit
63unsigned integers; however, it is only available in C23.
64The other functions all operate on a specific integer type, but
65otherwise behave the same and are available regardless of the C language
66version.
67.Sh RETURN VALUES
68The functions in the
69.Fn stdc_leading_ones
70family always return the number of leading ones found in
71.Fa value .
72These functions cannot fail.
73.Sh EXAMPLES
74.Sy Example 1
75Printing the number of leading zeros.
76.Bd -literal
77#include <stdbit.h>
78#include <stdio.h>
79#include <limits.h>
80
81int
82main(void)
83{
84	printf("0x%x 0x%x 0x%x 0x%x\en", stdc_leading_ones_uc(0xf6),
85	    stdc_leading_ones_us(0x9009), stdc_leading_ones_ui(UINT32_MAX),
86	    stdc_leading_ones_ull(0));
87	return (0);
88}
89.Ed
90.Pp
91When compiled and run, this produces:
92.Bd -literal -offset indent
93$ ./a.out
940x4 0x1 0x20 0x0
95.Ed
96.Sh INTERFACE STABILITY
97.Sy Committed
98.Sh MT-LEVEL
99.Sy Async-Signal-Safe
100.Sh SEE ALSO
101.Xr stdc_bit_ceil 3C ,
102.Xr stdc_bit_floor 3C ,
103.Xr stdc_bit_width 3C ,
104.Xr stdc_count_ones 3C ,
105.Xr stdc_count_zeros 3C ,
106.Xr stdc_first_leading_one 3C ,
107.Xr stdc_first_leading_zero 3C ,
108.Xr stdc_first_trailing_one 3C ,
109.Xr stdc_first_trailing_zero 3C ,
110.Xr stdc_has_single_bit 3C ,
111.Xr stdc_leading_zeros 3C ,
112.Xr stdc_trailing_ones 3C ,
113.Xr stdc_trailing_zeros 3C ,
114.Xr stdbit.h 3HEAD
115