summaryrefslogtreecommitdiff
path: root/lib/libc/stdbit/stdc_bit_ceil.c
blob: 2dfd7bbcbea2360e9c79ee963fee700350b5725a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <assert.h>
#include <limits.h>
#include <stdbit.h>

/* Ensure we don't shift 1U out of range. */
static_assert(UCHAR_WIDTH < UINT_WIDTH,
    "stdc_bit_ceil_uc needs UCHAR_WIDTH < UINT_WIDTH");

unsigned char
stdc_bit_ceil_uc(unsigned char x)
{
	if (x <= 1)
		return (1);

	return (1U << (UINT_WIDTH - __builtin_clz(x - 1)));
}

/* Ensure we don't shift 1U out of range. */
static_assert(USHRT_WIDTH < UINT_WIDTH,
    "stdc_bit_ceil_us needs USHRT_WIDTH < UINT_WIDTH");

unsigned short
stdc_bit_ceil_us(unsigned short x)
{
	if (x <= 1)
		return (1);

	return (1U << (UINT_WIDTH - __builtin_clz(x - 1)));
}

unsigned int
stdc_bit_ceil_ui(unsigned int x)
{
	if (x <= 1)
		return (1);

	if (x > UINT_MAX/2 + 1)
		return (0);

	return (1U << (UINT_WIDTH - __builtin_clz(x - 1)));
}

unsigned long
stdc_bit_ceil_ul(unsigned long x)
{
	if (x <= 1)
		return (1);

	if (x > ULONG_MAX/2 + 1)
		return (0);

	return (1UL << (ULONG_WIDTH - __builtin_clzl(x - 1)));
}

unsigned long long
stdc_bit_ceil_ull(unsigned long long x)
{
	if (x <= 1)
		return (1);

	if (x > ULLONG_MAX/2 + 1)
		return (0);

	return (1ULL << (ULLONG_WIDTH - __builtin_clzll(x - 1)));
}