summaryrefslogtreecommitdiff
path: root/shared/ossp_uuid/uuid_bm.h
blob: 4cdcec050350409b17fcdf453aaf1ad763e4a130 (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
75
76
77
78
79
80
/*
**  OSSP uuid - Universally Unique Identifier
**  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
**  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
**
**  This file is part of OSSP uuid, a library for the generation
**  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  uuid_bm.c: bitmask API implementation
*/

#ifndef __UUID_BM_H__
#define __UUID_BM_H__

/*
 *  Bitmask Calculation Macros (up to 32 bit only)
 *  (Notice: bit positions are counted n...0, i.e. lowest bit is position 0)
 */

/* generate a bitmask consisting of 1 bits from (and including)
   bit position `l' (left) to (and including) bit position `r' */
#define BM_MASK(l,r) \
    ((((unsigned int)1<<(((l)-(r))+1))-1)<<(r))

/* extract a value v from a word w at position `l' to `r' and return value */
#define BM_GET(w,l,r) \
    (((w)>>(r))&BM_MASK((l)-(r),0))

/* insert a value v into a word w at position `l' to `r' and return word */
#define BM_SET(w,l,r,v) \
    ((w)|(((v)&BM_MASK((l)-(r),0))<<(r)))

/* generate a single bit `b' (0 or 1) at bit position `n' */
#define BM_BIT(n,b) \
    ((b)<<(n))

/* generate a quad word octet of bits (a half byte, i.e. bit positions 3 to 0) */
#define BM_QUAD(b3,b2,b1,b0) \
    (BM_BIT(3,(b3))|BM_BIT(2,(b2))|BM_BIT(1,(b1))|BM_BIT(0,(b0)))

/* generate an octet word of bits (a byte, i.e. bit positions 7 to 0) */
#define BM_OCTET(b7,b6,b5,b4,b3,b2,b1,b0) \
    ((BM_QUAD(b7,b6,b5,b4)<<4)|BM_QUAD(b3,b2,b1,b0))

/* generate the value 2^n */
#define BM_POW2(n) \
    BM_BIT(n,1)

/* shift word w k bits to the left or to the right */
#define BM_SHL(w,k) \
    ((w)<<(k))
#define BM_SHR(w,k) \
    ((w)>>(k))

/* rotate word w (of bits n..0) k bits to the left or to the right */
#define BM_ROL(w,n,k) \
    ((BM_SHL((w),(k))&BM_MASK(n,0))|BM_SHR(((w)&BM_MASK(n,0)),(n)-(k)))
#define BM_ROR(w,n,k) \
    ((BM_SHR(((w)&BM_MASK(n,0)),(k)))|BM_SHL(((w),(n)-(k))&BM_MASK(n,0)))

#endif /* __UUID_BM_H__ */

bgstack15