source: mainline/uspace/lib/c/include/bitops.h@ cd1e3fc0

Last change on this file since cd1e3fc0 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2006 Ondrej Palkovsky
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/** @addtogroup libc
8 * @{
9 */
10/** @file
11 */
12
13#ifndef _LIBC_BITOPS_H_
14#define _LIBC_BITOPS_H_
15
16#include <stddef.h>
17#include <stdint.h>
18
19/** Mask with bit @a n set. */
20#define BIT_V(type, n) \
21 ((type) 1 << (n))
22
23/** Mask with rightmost @a n bits set. */
24#define BIT_RRANGE(type, n) \
25 (BIT_V(type, (n)) - 1)
26
27/** Mask with bits @a hi .. @a lo set. @a hi >= @a lo. */
28#define BIT_RANGE(type, hi, lo) \
29 (BIT_RRANGE(type, (hi) - (lo) + 1) << (lo))
30
31/** Extract range of bits @a hi .. @a lo from @a value. */
32#define BIT_RANGE_EXTRACT(type, hi, lo, value) \
33 (((value) >> (lo)) & BIT_RRANGE(type, (hi) - (lo) + 1))
34
35/** Insert @a value between bits @a hi .. @a lo. */
36#define BIT_RANGE_INSERT(type, hi, lo, value) \
37 (((value) & BIT_RRANGE(type, (hi) - (lo) + 1)) << (lo))
38
39/** Return position of first non-zero bit from left (i.e. [log_2(arg)]).
40 *
41 * If number is zero, it returns 0
42 */
43static inline unsigned int fnzb32(uint32_t arg)
44{
45 unsigned int n = 0;
46
47 if (arg >> 16) {
48 arg >>= 16;
49 n += 16;
50 }
51
52 if (arg >> 8) {
53 arg >>= 8;
54 n += 8;
55 }
56
57 if (arg >> 4) {
58 arg >>= 4;
59 n += 4;
60 }
61
62 if (arg >> 2) {
63 arg >>= 2;
64 n += 2;
65 }
66
67 if (arg >> 1) {
68 arg >>= 1;
69 n += 1;
70 }
71
72 return n;
73}
74
75static inline unsigned int fnzb64(uint64_t arg)
76{
77 unsigned int n = 0;
78
79 if (arg >> 32) {
80 arg >>= 32;
81 n += 32;
82 }
83
84 return (n + fnzb32((uint32_t) arg));
85}
86
87static inline unsigned int fnzb(size_t arg)
88{
89 return fnzb64(arg);
90}
91
92#endif
93
94/** @}
95 */
Note: See TracBrowser for help on using the repository browser.