librfn
An ad-hoc utility library
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
librfn
bitops.c
Go to the documentation of this file.
1
/*
2
* bitops.c
3
*
4
* Part of librfn (a general utility library from redfelineninja.org.uk)
5
*
6
* Copyright (C) 2012 Daniel Thompson <daniel@redfelineninja.org.uk>
7
* Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
8
*
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published
11
* by the Free Software Foundation; either version 3 of the License, or
12
* (at your option) any later version.
13
*/
14
15
#include <assert.h>
16
#include <stdint.h>
17
18
#include "
librfn/bitops.h
"
19
20
int
bitcnt
(uint32_t x)
21
{
22
uint32_t n;
23
24
n = (x >> 1) & 0x77777777;
25
x = x - n;
26
n = (n >> 1) & 0x77777777;
27
x = x - n;
28
n = (n >> 1) & 0x77777777;
29
x = x - n;
30
x = (x + (x >> 4)) & 0x0F0F0F0F;
31
x = x * 0x01010101;
32
33
return
x >> 24;
34
}
35
36
int
clz
(uint32_t x)
37
{
38
x = x | (x >> 1);
39
x = x | (x >> 2);
40
x = x | (x >> 4);
41
x = x | (x >> 8);
42
x = x | (x >>16);
43
return
bitcnt
(~x);
44
}
45
46
int
ctz
(uint32_t x)
47
{
48
return
bitcnt
(~x & (x - 1));
49
}
50
51
int
ilog2
(uint32_t x)
52
{
53
assert(x);
54
return
31 -
clz
(x);
55
}
bitcnt
int bitcnt(uint32_t x)
Definition:
bitops.c:20
bitops.h
ctz
int ctz(uint32_t x)
Definition:
bitops.c:46
ilog2
int ilog2(uint32_t x)
Definition:
bitops.c:51
clz
int clz(uint32_t x)
Definition:
bitops.c:36
Generated on Sat Feb 6 2016 21:25:32 for librfn by
1.8.10