1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * bitmask header file 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * Copyright (c) 2004 Silicon Graphics, Inc. All rights reserved. 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * Paul Jackson <pj@sgi.com> 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci/* 10f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 11f08c3bdfSopenharmony_ci * it under the terms of the GNU Lesser General Public License as published by 12f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2.1 of the License, or 13f08c3bdfSopenharmony_ci * (at your option) any later version. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 16f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 17f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18f08c3bdfSopenharmony_ci * GNU Lesser General Public License for more details. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * You should have received a copy of the GNU Lesser General Public License 21f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 22f08c3bdfSopenharmony_ci * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci/* 26f08c3bdfSopenharmony_ci * bitmasks - dynamically sized multi-word bit masks, with many operators 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * link with -lbitmask 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * ==== Allocate and free `struct bitmask *` 31f08c3bdfSopenharmony_ci * 32f08c3bdfSopenharmony_ci * bitmask_alloc(n): Allocate a new struct bitmask with a size of n bits 33f08c3bdfSopenharmony_ci * bitmask_free(bmp): Free struct bitmask 34f08c3bdfSopenharmony_ci * 35f08c3bdfSopenharmony_ci * ==== Display and parse ascii string representations 36f08c3bdfSopenharmony_ci * 37f08c3bdfSopenharmony_ci * bitmask_displayhex(buf, len, bmp): Write hex word bmp to buf 38f08c3bdfSopenharmony_ci * bitmask_displaylist(buf, len, bmp): Write decimal list bmp to buf 39f08c3bdfSopenharmony_ci * bitmask_parsehex(buf, bmp): Parse hex words in buf to bmp 40f08c3bdfSopenharmony_ci * bitmask_parselist(buf, bmp): Parse decimal list in buf to bmp 41f08c3bdfSopenharmony_ci * 42f08c3bdfSopenharmony_ci * ==== Basic initialization operations 43f08c3bdfSopenharmony_ci * 44f08c3bdfSopenharmony_ci * bitmask_copy(bmp1, bmp2): Copy bmp2 to bmp1 45f08c3bdfSopenharmony_ci * bitmask_setall(bmp): Set all bits in bitmask: bmp = ~0 46f08c3bdfSopenharmony_ci * bitmask_clearall(bmp): Clear all bits in bitmask: bmp = 0 47f08c3bdfSopenharmony_ci * 48f08c3bdfSopenharmony_ci * ==== Interface to kernel sched_{set,get}affinity system calls 49f08c3bdfSopenharmony_ci * 50f08c3bdfSopenharmony_ci * bitmask_nbytes(bmp): Length in bytes of mask - use as second argument 51f08c3bdfSopenharmony_ci * bitmask_mask(bmp): Direct pointer to bit mask - use as third argument 52f08c3bdfSopenharmony_ci * 53f08c3bdfSopenharmony_ci * ==== Unary numeric queries 54f08c3bdfSopenharmony_ci * 55f08c3bdfSopenharmony_ci * bitmask_nbits(bmp): Size in bits of entire bitmask 56f08c3bdfSopenharmony_ci * bitmask_weight(bmp): Hamming Weight: number of set bits 57f08c3bdfSopenharmony_ci * 58f08c3bdfSopenharmony_ci * ==== Unary Boolean queries 59f08c3bdfSopenharmony_ci * 60f08c3bdfSopenharmony_ci * bitmask_isbitset(bmp, i): True if specified bit i is set 61f08c3bdfSopenharmony_ci * bitmask_isbitclear(bmp, i): True if specified bit i is clear 62f08c3bdfSopenharmony_ci * bitmask_isallset(bmp): True if all bits are set 63f08c3bdfSopenharmony_ci * bitmask_isallclear(bmp): True if all bits are clear 64f08c3bdfSopenharmony_ci * 65f08c3bdfSopenharmony_ci * ==== Single bit operations 66f08c3bdfSopenharmony_ci * 67f08c3bdfSopenharmony_ci * bitmask_setbit(bmp, i): Set a single bit i in bitmask 68f08c3bdfSopenharmony_ci * bitmask_clearbit(bmp, i): Clear a single bit i in bitmask 69f08c3bdfSopenharmony_ci * 70f08c3bdfSopenharmony_ci * ==== Binary Boolean operations: bmp1 op? bmp2 71f08c3bdfSopenharmony_ci * 72f08c3bdfSopenharmony_ci * bitmask_equal(bmp1, bmp2): True if two bitmasks are equal 73f08c3bdfSopenharmony_ci * bitmask_subset(bmp1, bmp2): True if first bitmask is subset of second 74f08c3bdfSopenharmony_ci * bitmask_disjoint(bmp1, bmp2): True if two bitmasks don't overlap 75f08c3bdfSopenharmony_ci * bitmask_intersects(bmp1, bmp2): True if two bitmasks do overlap 76f08c3bdfSopenharmony_ci * 77f08c3bdfSopenharmony_ci * ==== Range operations 78f08c3bdfSopenharmony_ci * 79f08c3bdfSopenharmony_ci * bitmask_setrange(bmp, i, j): Set bits of bitmask in specified range [i, j) 80f08c3bdfSopenharmony_ci * bitmask_clearrange(bmp, i, j): Clear bits of bitmask in specified range 81f08c3bdfSopenharmony_ci * bitmask_keeprange(bmp, i, j): Clear all but specified range 82f08c3bdfSopenharmony_ci * 83f08c3bdfSopenharmony_ci * ==== Unary operations 84f08c3bdfSopenharmony_ci * 85f08c3bdfSopenharmony_ci * bitmask_complement(bmp1, bmp2): Complement: bmp1 = ~bmp2 86f08c3bdfSopenharmony_ci * bitmask_shiftright(bmp1, bmp2, n): Right shift: bmp1 = bmp2 >> n 87f08c3bdfSopenharmony_ci * bitmask_shiftleft(bmp1, bmp2, n): Left shift: bmp1 = bmp2 << n 88f08c3bdfSopenharmony_ci * 89f08c3bdfSopenharmony_ci * ==== Binary operations 90f08c3bdfSopenharmony_ci * 91f08c3bdfSopenharmony_ci * bitmask_and(bmp1, bmp2, bmp3): Logical `and`: bmp1 = bmp2 & bmp3 92f08c3bdfSopenharmony_ci * bitmask_andnot(bmp1, bmp2, bmp3): Logical `andnot`: bmp1 = bmp2 & ~bmp3 93f08c3bdfSopenharmony_ci * bitmask_or(bmp1, bmp2, bmp3): Logical `or`: bmp1 = bmp2 | bmp3 94f08c3bdfSopenharmony_ci * bitmask_eor(bmp1, bmp2, bmp3): Logical `eor`: bmp1 = bmp2 ^ bmp3 95f08c3bdfSopenharmony_ci * 96f08c3bdfSopenharmony_ci * ==== Iteration operators 97f08c3bdfSopenharmony_ci * 98f08c3bdfSopenharmony_ci * bitmask_first(bmp): Number of lowest set bit (min) 99f08c3bdfSopenharmony_ci * bitmask_next(bmp, i): Number of next set bit at or above given bit i 100f08c3bdfSopenharmony_ci * bitmask_rel_to_abs_pos(bmp, n): Absolute position of nth set bit 101f08c3bdfSopenharmony_ci * bitmask_abs_to_rel_pos(bmp, n): Relative position amongst set bits of bit n 102f08c3bdfSopenharmony_ci * bitmask_last(bmp): Number of highest set bit (max) 103f08c3bdfSopenharmony_ci * 104f08c3bdfSopenharmony_ci * ==== Example: 105f08c3bdfSopenharmony_ci * 106f08c3bdfSopenharmony_ci * == allocate some bitmasks == 107f08c3bdfSopenharmony_ci * struct bitmask *a = bitmask_alloc(10); 108f08c3bdfSopenharmony_ci * struct bitmask *b = bitmask_alloc(10); 109f08c3bdfSopenharmony_ci * struct bitmask *c = bitmask_alloc(10); 110f08c3bdfSopenharmony_ci * struct bitmask *d = bitmask_alloc(10); 111f08c3bdfSopenharmony_ci * struct bitmask *e = bitmask_alloc(10); 112f08c3bdfSopenharmony_ci * struct bitmask *f = bitmask_alloc(10); 113f08c3bdfSopenharmony_ci * struct bitmask *g = bitmask_alloc(10); 114f08c3bdfSopenharmony_ci * 115f08c3bdfSopenharmony_ci * == stuff some data in first two == 116f08c3bdfSopenharmony_ci * bitmask_setrange(a, 2, 6); # set bits 2,3,4,5 117f08c3bdfSopenharmony_ci * bitmask_shiftleft(b, a, 2); # set bits 4,5,6,7 118f08c3bdfSopenharmony_ci * 119f08c3bdfSopenharmony_ci * == d is complement of union == 120f08c3bdfSopenharmony_ci * bitmask_complement(d, bitmask_or(c, a, b)); 121f08c3bdfSopenharmony_ci * 122f08c3bdfSopenharmony_ci * == g is intersection of complements == 123f08c3bdfSopenharmony_ci * bitmask_and(g, bitmask_complement(e, a), bitmask_complement(f, b)); 124f08c3bdfSopenharmony_ci * 125f08c3bdfSopenharmony_ci * == d should equal g == 126f08c3bdfSopenharmony_ci * if (bitmask_equal(d, g)) 127f08c3bdfSopenharmony_ci * puts("DeMorgan's Law works!"); 128f08c3bdfSopenharmony_ci * 129f08c3bdfSopenharmony_ci * == free up bitmasks == 130f08c3bdfSopenharmony_ci * bitmask_free(a); 131f08c3bdfSopenharmony_ci * bitmask_free(b); 132f08c3bdfSopenharmony_ci * bitmask_free(c); 133f08c3bdfSopenharmony_ci * bitmask_free(d); 134f08c3bdfSopenharmony_ci * bitmask_free(e); 135f08c3bdfSopenharmony_ci * bitmask_free(f); 136f08c3bdfSopenharmony_ci * bitmask_free(g); 137f08c3bdfSopenharmony_ci */ 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci#ifndef _BITMASK_H 140f08c3bdfSopenharmony_ci#define _BITMASK_H 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci#ifdef __cplusplus 143f08c3bdfSopenharmony_ciextern "C" { 144f08c3bdfSopenharmony_ci#endif 145f08c3bdfSopenharmony_ci 146f08c3bdfSopenharmony_cistruct bitmask; 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_cistruct bitmask *bitmask_alloc(unsigned int n); 149f08c3bdfSopenharmony_civoid bitmask_free(struct bitmask *bmp); 150f08c3bdfSopenharmony_ci 151f08c3bdfSopenharmony_ciint bitmask_displayhex(char *buf, int len, const struct bitmask *bmp); 152f08c3bdfSopenharmony_ciint bitmask_displaylist(char *buf, int len, const struct bitmask *bmp); 153f08c3bdfSopenharmony_ciint bitmask_parsehex(const char *buf, struct bitmask *bmp); 154f08c3bdfSopenharmony_ciint bitmask_parselist(const char *buf, struct bitmask *bmp); 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_cistruct bitmask *bitmask_copy(struct bitmask *bmp1, const struct bitmask *bmp2); 157f08c3bdfSopenharmony_cistruct bitmask *bitmask_setall(struct bitmask *bmp); 158f08c3bdfSopenharmony_cistruct bitmask *bitmask_clearall(struct bitmask *bmp); 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ciunsigned int bitmask_nbytes(struct bitmask *bmp); 161f08c3bdfSopenharmony_ciunsigned long *bitmask_mask(struct bitmask *bmp); 162f08c3bdfSopenharmony_ci 163f08c3bdfSopenharmony_ciunsigned int bitmask_nbits(const struct bitmask *bmp); 164f08c3bdfSopenharmony_ciunsigned int bitmask_weight(const struct bitmask *bmp); 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_ciint bitmask_isbitset(const struct bitmask *bmp, unsigned int i); 167f08c3bdfSopenharmony_ciint bitmask_isbitclear(const struct bitmask *bmp, unsigned int i); 168f08c3bdfSopenharmony_ciint bitmask_isallset(const struct bitmask *bmp); 169f08c3bdfSopenharmony_ciint bitmask_isallclear(const struct bitmask *bmp); 170f08c3bdfSopenharmony_ci 171f08c3bdfSopenharmony_cistruct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i); 172f08c3bdfSopenharmony_cistruct bitmask *bitmask_clearbit(struct bitmask *bmp, unsigned int i); 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_ciint bitmask_equal(const struct bitmask *bmp1, const struct bitmask *bmp2); 175f08c3bdfSopenharmony_ciint bitmask_subset(const struct bitmask *bmp1, const struct bitmask *bmp2); 176f08c3bdfSopenharmony_ciint bitmask_disjoint(const struct bitmask *bmp1, const struct bitmask *bmp2); 177f08c3bdfSopenharmony_ciint bitmask_intersects(const struct bitmask *bmp1, const struct bitmask *bmp2); 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_cistruct bitmask *bitmask_setrange(struct bitmask *bmp, 180f08c3bdfSopenharmony_ci unsigned int i, unsigned int j); 181f08c3bdfSopenharmony_cistruct bitmask *bitmask_clearrange(struct bitmask *bmp, 182f08c3bdfSopenharmony_ci unsigned int i, unsigned int j); 183f08c3bdfSopenharmony_cistruct bitmask *bitmask_keeprange(struct bitmask *bmp, 184f08c3bdfSopenharmony_ci unsigned int i, unsigned int j); 185f08c3bdfSopenharmony_ci 186f08c3bdfSopenharmony_cistruct bitmask *bitmask_complement(struct bitmask *bmp1, 187f08c3bdfSopenharmony_ci const struct bitmask *bmp2); 188f08c3bdfSopenharmony_cistruct bitmask *bitmask_shiftright(struct bitmask *bmp1, 189f08c3bdfSopenharmony_ci const struct bitmask *bmp2, unsigned int n); 190f08c3bdfSopenharmony_cistruct bitmask *bitmask_shiftleft(struct bitmask *bmp1, 191f08c3bdfSopenharmony_ci const struct bitmask *bmp2, unsigned int n); 192f08c3bdfSopenharmony_ci 193f08c3bdfSopenharmony_cistruct bitmask *bitmask_and(struct bitmask *bmp1,const struct bitmask *bmp2, 194f08c3bdfSopenharmony_ci const struct bitmask *bmp3); 195f08c3bdfSopenharmony_cistruct bitmask *bitmask_andnot(struct bitmask *bmp1, const struct bitmask *bmp2, 196f08c3bdfSopenharmony_ci const struct bitmask *bmp3); 197f08c3bdfSopenharmony_cistruct bitmask *bitmask_or(struct bitmask *bmp1, const struct bitmask *bmp2, 198f08c3bdfSopenharmony_ci const struct bitmask *bmp3); 199f08c3bdfSopenharmony_cistruct bitmask *bitmask_eor(struct bitmask *bmp1, const struct bitmask *bmp2, 200f08c3bdfSopenharmony_ci const struct bitmask *bmp3); 201f08c3bdfSopenharmony_ci 202f08c3bdfSopenharmony_ciunsigned int bitmask_first(const struct bitmask *bmp); 203f08c3bdfSopenharmony_ciunsigned int bitmask_next(const struct bitmask *bmp, unsigned int i); 204f08c3bdfSopenharmony_ciunsigned int bitmask_rel_to_abs_pos(const struct bitmask *bmp, unsigned int n); 205f08c3bdfSopenharmony_ciunsigned int bitmask_abs_to_rel_pos(const struct bitmask *bmp, unsigned int n); 206f08c3bdfSopenharmony_ciunsigned int bitmask_last(const struct bitmask *bmp); 207f08c3bdfSopenharmony_ci 208f08c3bdfSopenharmony_ci#ifdef __cplusplus 209f08c3bdfSopenharmony_ci} 210f08c3bdfSopenharmony_ci#endif 211f08c3bdfSopenharmony_ci 212f08c3bdfSopenharmony_ci#endif 213