summaryrefslogtreecommitdiffstats
path: root/pbn_bit.c
blob: 0ede6ead567f757a61797a87bf8c59d347d1e2b9 (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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include "pbnint.h"

struct pbn *pbn_set_bit(struct pbn *pbn, int bit)
{
    int len;
    pbn_limb_t mask;

    len = (bit+PBN_LIMB_BITS-1)/PBN_LIMB_BITS;
    if (len < pbn->len)
	len = pbn->len;

    pbn = pbn_cow(pbn, len);
    mask = (pbn_limb_t)1 << (bit % PBN_LIMB_BITS);
    pbn->num[bit/PBN_LIMB_BITS] |= mask;

    bit++;
    pbn->bits = (bit > pbn->bits) ? bit : pbn->bits;

    return pbn;
}

struct pbn *pbn_clr_bit(struct pbn *pbn, int bit)
{
    pbn_limb_t mask;

    if (bit >= pbn->bits)
	return pbn;

    pbn = pbn_cow(pbn, pbn->len);
    mask = (pbn_limb_t)1 << (bit % PBN_LIMB_BITS);
    pbn->num[bit / PBN_LIMB_BITS] &= ~mask;

    bit++;
    if (bit == pbn->bits)
	return pbn_adjust_bits(pbn); /* We just cleared the MSB */
    else
	return pbn;
}

int pbn_bit(const struct pbn *pbn, int bit)
{
    if (bit >= pbn->bits)
	return 0;

    return (pbn->num[bit/PBN_LIMB_BITS] >> (bit % PBN_LIMB_BITS)) & 1;
}