1 /*
2 **  OSSP uuid - Universally Unique Identifier
3 **  Copyright (c) 2004-2008 Ralf S. Engelschall <rse@engelschall.com>
4 **  Copyright (c) 2004-2008 The OSSP Project <http://www.ossp.org/>
5 **
6 **  This file is part of OSSP uuid, a library for the generation
7 **  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
8 **
9 **  Permission to use, copy, modify, and distribute this software for
10 **  any purpose with or without fee is hereby granted, provided that
11 **  the above copyright notice and this permission notice appear in all
12 **  copies.
13 **
14 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
15 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
18 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 **  SUCH DAMAGE.
26 **
27 **  uuid_bm.c: bitmask API implementation
28 */
29 
30 #ifndef __UUID_BM_H__
31 #define __UUID_BM_H__
32 
33 /*
34  *  Bitmask Calculation Macros (up to 32 bit only)
35  *  (Notice: bit positions are counted n...0, i.e. lowest bit is position 0)
36  */
37 
38 /* generate a bitmask consisting of 1 bits from (and including)
39    bit position `l' (left) to (and including) bit position `r' */
40 #define BM_MASK(l,r) \
41     ((((unsigned int)1<<(((l)-(r))+1))-1)<<(r))
42 
43 /* extract a value v from a word w at position `l' to `r' and return value */
44 #define BM_GET(w,l,r) \
45     (((w)>>(r))&BM_MASK((l)-(r),0))
46 
47 /* insert a value v into a word w at position `l' to `r' and return word */
48 #define BM_SET(w,l,r,v) \
49     ((w)|(((v)&BM_MASK((l)-(r),0))<<(r)))
50 
51 /* generate a single bit `b' (0 or 1) at bit position `n' */
52 #define BM_BIT(n,b) \
53     ((b)<<(n))
54 
55 /* generate a quad word octet of bits (a half byte, i.e. bit positions 3 to 0) */
56 #define BM_QUAD(b3,b2,b1,b0) \
57     (BM_BIT(3,(b3))|BM_BIT(2,(b2))|BM_BIT(1,(b1))|BM_BIT(0,(b0)))
58 
59 /* generate an octet word of bits (a byte, i.e. bit positions 7 to 0) */
60 #define BM_OCTET(b7,b6,b5,b4,b3,b2,b1,b0) \
61     ((BM_QUAD(b7,b6,b5,b4)<<4)|BM_QUAD(b3,b2,b1,b0))
62 
63 /* generate the value 2^n */
64 #define BM_POW2(n) \
65     BM_BIT(n,1)
66 
67 /* shift word w k bits to the left or to the right */
68 #define BM_SHL(w,k) \
69     ((w)<<(k))
70 #define BM_SHR(w,k) \
71     ((w)>>(k))
72 
73 /* rotate word w (of bits n..0) k bits to the left or to the right */
74 #define BM_ROL(w,n,k) \
75     ((BM_SHL((w),(k))&BM_MASK(n,0))|BM_SHR(((w)&BM_MASK(n,0)),(n)-(k)))
76 #define BM_ROR(w,n,k) \
77     ((BM_SHR(((w)&BM_MASK(n,0)),(k)))|BM_SHL(((w),(n)-(k))&BM_MASK(n,0)))
78 
79 #endif /* __UUID_BM_H__ */
80 
81