summaryrefslogtreecommitdiff
path: root/lib/libc/stdbit/stdc_bit_floor.c
blob: 0a491d6ec7b3de1da555fc92cebaa5b802e3d40b (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
/*
 * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

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

unsigned char
stdc_bit_floor_uc(unsigned char x)
{
	if (x == 0)
		return (0);

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

unsigned short
stdc_bit_floor_us(unsigned short x)
{
	if (x == 0)
		return (0);

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

unsigned int
stdc_bit_floor_ui(unsigned int x)
{
	if (x == 0)
		return (0);

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

unsigned long
stdc_bit_floor_ul(unsigned long x)
{
	if (x == 0)
		return (0);

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

unsigned long long
stdc_bit_floor_ull(unsigned long long x)
{
	if (x == 0)
		return (0);

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