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
RevLine 
[f8ddd17]1/*
[d7f7a4a]2 * SPDX-FileCopyrightText: 2006 Ondrej Palkovsky
[f8ddd17]3 *
[d7f7a4a]4 * SPDX-License-Identifier: BSD-3-Clause
[f8ddd17]5 */
6
[174156fd]7/** @addtogroup libc
[f8ddd17]8 * @{
9 */
10/** @file
11 */
12
[4805495]13#ifndef _LIBC_BITOPS_H_
14#define _LIBC_BITOPS_H_
[f8ddd17]15
[8d2dd7f2]16#include <stddef.h>
17#include <stdint.h>
[f8ddd17]18
[64e6945d]19/** Mask with bit @a n set. */
20#define BIT_V(type, n) \
[972c60ce]21 ((type) 1 << (n))
[64e6945d]22
23/** Mask with rightmost @a n bits set. */
24#define BIT_RRANGE(type, n) \
[6896409c]25 (BIT_V(type, (n)) - 1)
[64e6945d]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))
[f8ddd17]34
[48197c1]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
[f8ddd17]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 */
[db24058]43static inline unsigned int fnzb32(uint32_t arg)
[f8ddd17]44{
[db24058]45 unsigned int n = 0;
[a35b458]46
[f8ddd17]47 if (arg >> 16) {
48 arg >>= 16;
49 n += 16;
50 }
[a35b458]51
[f8ddd17]52 if (arg >> 8) {
53 arg >>= 8;
54 n += 8;
55 }
[a35b458]56
[f8ddd17]57 if (arg >> 4) {
58 arg >>= 4;
59 n += 4;
60 }
[a35b458]61
[f8ddd17]62 if (arg >> 2) {
63 arg >>= 2;
64 n += 2;
65 }
[a35b458]66
[f8ddd17]67 if (arg >> 1) {
68 arg >>= 1;
69 n += 1;
70 }
[a35b458]71
[f8ddd17]72 return n;
73}
74
[db24058]75static inline unsigned int fnzb64(uint64_t arg)
[f8ddd17]76{
[db24058]77 unsigned int n = 0;
[a35b458]78
[f8ddd17]79 if (arg >> 32) {
80 arg >>= 32;
81 n += 32;
82 }
[a35b458]83
[db24058]84 return (n + fnzb32((uint32_t) arg));
[f8ddd17]85}
86
[db24058]87static inline unsigned int fnzb(size_t arg)
88{
89 return fnzb64(arg);
90}
[f8ddd17]91
92#endif
93
94/** @}
95 */
Note: See TracBrowser for help on using the repository browser.