summaryrefslogtreecommitdiffstats
path: root/pbn_init.c
blob: 53edb873fde915667edba1634b2d432318bf10db (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ----------------------------------------------------------------------- *
 *   
 *   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.
 *
 * ----------------------------------------------------------------------- */

/*
 * pbn_int.c
 */

#define PBN_INIT 1
#include <string.h>
#include "pbnint.h"

struct pbn *pbn_new(int len)
{
    struct pbn *pbn;

    pbn = _pbn_zalloc(sizeof(struct pbn)+(len-1)*sizeof(pbn_atom_t));
    if (!pbn)
	return NULL;

    pbn->ref = 1;
    pbn->len = len;

    return pbn;
}

void pbn_free(struct pbn *pbn)
{
    if (! --pbn->ref)
	_pbn_free(pbn);
}

#if PBN_ATOM_BITS != 32 && PBN_ATOM_BITS != 64
# error "Fix pbn_ilog2p1 for new PBN_ATOM_BITS size"
#endif

static int pbn_ilog2p1(pbn_atom_t v)
{
    int p = 1;

#if PBN_ATOM_BITS >= 64
    if (v & 0xffffffff00000000) {
	p += 32;
	v >>= 32;
    }
#endif

    if (v & 0xffff0000) {
	p += 16;
	v >>= 16;
    }
    if (v & 0xff00) {
	p += 8;
	v >>= 8;
    }
    if (v & 0xf0) {
	p += 4;
	v >>= 4;
    }
    if (v & 0xc) {
	p += 2;
	v >>= 2;
    }
    if (v & 0x2) {
	p += 1;
	v >>= 1;
    }

    return p;
}

struct pbn *pbn_int(pbn_satom_t v)
{
    struct pbn *pbn = pbn_new(1);
    if (!pbn)
	return NULL;

    if (v < 0) {
	v = -v;
	pbn->minus = 1;
    } else {
	pbn->minus = 0;
    }

    pbn->num[0] = v;
    pbn->bits   = pbn_ilog2p1(v);
    return pbn;
}

/* Produce a duplicate */
struct pbn *pbn_dup(const struct pbn *src)
{
    int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_atom_t);
    struct pbn *dst = _pbn_malloc(bytes);

    memcpy(dst, src, bytes);
    dst->ref = 1;

    return dst;
}

/* Produce a duplicate, possibly extended */
struct pbn *pbn_dupx(const struct pbn *src, int len)
{
    int slack = src->len >= len ? 0 : len-src->len;
    int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_atom_t);
    struct pbn *dst = _pbn_malloc(bytes+slack*sizeof(pbn_atom_t));

    if (!dst)
	return NULL;

    memcpy(dst, src, bytes);
    dst->ref = 1;
    memset(&dst->num[slack], 0, slack*sizeof(pbn_atom_t));

    return dst;
}

/* Returns a duplicate or clears src for reuse, consumes a reference */
struct pbn *pbn_cow(struct pbn *src, int len)
{
    int olen = src->len;

    if (src->ref <= 1) {
	src->ref = 1;
	if (len > olen) {
	    src = _pbn_realloc(src, sizeof(struct pbn)+
			       (len-1)*sizeof(pbn_atom_t));
	    src->len = len;
	}
	memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_atom_t));
	return src;
    }

    src->ref--;
    return pbn_dupx(src, len);
}

/* Adjust the bits field of a specific pbn, and clear the minus flag
   if the value is actually zero. */
void _pbn_adjust_bits(struct pbn *pbn)
{
    int i;
    pbn_atom_t v;

    for (i = pbn->len-1; i >= 0; i--) {
	v = pbn->num[i];
	if (v) {
	    pbn->bits = i*PBN_ATOM_BITS + pbn_ilog2p1(v);
	    return;
	}
    }

    pbn->bits = 0;
    pbn->minus = 0;
}