aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/bitops.c
blob: 5593bc558f85033ff2758d018dc298997e4968dd (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
72
73
74
/*
 * Bit operations
 *
 * Copyright (c) 2020 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "bitops.h"
#include "compat.h"

/* Find most significant (last) bit set. */
uint8_t fls16(uint16_t value)
{
	uint8_t ret = 16u;

	if (value == 0u)
		return 0u;

	if ((value & 0xFF00u) == 0u) {
		value = (uint16_t)(value << 8);
		ret = (uint8_t)(ret - 8u);
	}
	if ((value & 0xF000u) == 0u) {
		value = (uint16_t)(value << 4);
		ret = (uint8_t)(ret - 4u);
	}
	if ((value & 0xC000u) == 0u) {
		value = (uint16_t)(value << 2);
		ret = (uint8_t)(ret - 2u);
	}
	if ((value & 0x8000u) == 0u) {
		value = (uint16_t)(value << 1);
		ret = (uint8_t)(ret - 1u);
	}

	return ret;
}

/* Find most significant (last) bit set. */
uint8_t fls8(uint8_t value)
{
	uint8_t ret = 8u;

	if (value == 0u)
		return 0u;

	if ((value & 0xF0u) == 0u) {
		value = (uint8_t)(value << 4);
		ret = (uint8_t)(ret - 4u);
	}
	if ((value & 0xC0u) == 0u) {
		value = (uint8_t)(value << 2);
		ret = (uint8_t)(ret - 2u);
	}
	if ((value & 0x80u) == 0u) {
		value = (uint8_t)(value << 1);
		ret = (uint8_t)(ret - 1u);
	}

	return ret;
}
bues.ch cgit interface