llbit.c
1.56 KB
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
#include "os.h"
#define BITS_PER_LONGLONG 64
/*
* Need two extract routines because
* the unsigned extract should not sign-extend the value,
* while the signed extract should.
*/
s64
__ll_bit_extract (u64 *addr, unsigned start_bit, unsigned length)
{
/* assume 32 < length < 64 */
unsigned words = start_bit / BITS_PER_LONGLONG;
unsigned lbits = start_bit % BITS_PER_LONGLONG;
unsigned rbits = BITS_PER_LONGLONG - (lbits + length);
u64 mask;
addr += words;
mask = ((long long)1 << length) - 1;
mask = mask << rbits; /* all 1's in field */
return((*addr & mask) >> rbits);
}
u64
__ull_bit_extract (u64 *addr, unsigned start_bit, unsigned length)
{
/* assume 32 < length < 64 */
unsigned words = start_bit / BITS_PER_LONGLONG;
unsigned lbits = start_bit % BITS_PER_LONGLONG;
unsigned rbits = BITS_PER_LONGLONG - (lbits + length);
u64 mask;
addr += words;
mask = ((long long)1 << length) - 1;
mask = mask << rbits; /* all 1's in field */
return((*addr & mask) >> rbits);
}
u64
__ll_bit_insert (u64 *addr, unsigned start_bit, unsigned length, u64 val)
{
/* assume 32 < length < 64 */
unsigned words = start_bit / BITS_PER_LONGLONG;
unsigned lbits = start_bit % BITS_PER_LONGLONG;
unsigned rbits = BITS_PER_LONGLONG - (lbits + length);
unsigned long long llval, mask;
addr += words;
mask = ((long long)1 << length) - 1;
mask <<= rbits; /* all 1's in field */
/* clear lhs, rhs */
llval = (val << ( BITS_PER_LONGLONG - length)) >> lbits;
*addr = (*addr & ~mask) | llval;
llval = llval>> rbits; /* truncated val */
return llval;
}