summaryrefslogtreecommitdiffstats
path: root/pbn_init.c
blob: 5c38c15fb1cfd390a5b0d2924380d1aeae471adc (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* ----------------------------------------------------------------------- *
 *
 *   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 <limits.h>
#include "pbnint.h"

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

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

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

    return pbn;
}

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

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

#if defined(__GNUC__) && __GNUC__ >= 4
    if (sizeof(pbn_limb_t) == sizeof(int))
        return (__builtin_clz(v) ^ (PBN_LIMB_BITS-1)) + 1;
    if (sizeof(pbn_limb_t) == sizeof(long))
        return (__builtin_clzl(v) ^ (PBN_LIMB_BITS-1)) + 1;
    if (sizeof(pbn_limb_t) == sizeof(long long))
        return (__builtin_clzll(v) ^ (PBN_LIMB_BITS-1)) + 1;
    /* else fall through, wtf... */
#endif

#if PBN_LIMB_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_slimb_t v)
{
    struct pbn *pbn = pbn_new(1);
    if (!pbn)
	return NULL;

    if (v == 0) {
	/* pbn->num[0] = pbn->bits = pbn->minus = 0; */
	return pbn;
    } else if (v < 0) {
	v = -v;
	pbn->minus = 1;
    } else {
	pbn->minus = 0;
    }

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

struct pbn *pbn_uint(pbn_limb_t v)
{
    struct pbn *pbn = pbn_new(1);
    if (!pbn)
	return NULL;

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

/* Produce a duplicate */
struct pbn *pbn_dup(const struct pbn *src)
{
    int bytes = sizeof(struct pbn)+(src->len-1)*sizeof(pbn_limb_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_limb_t);
    struct pbn *dst = _pbn_malloc(bytes+slack*sizeof(pbn_limb_t));

    if (!dst)
	return NULL;

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

    return dst;
}

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

    if (src->ref <= 1) {
	src->ref = 1;
	if (len > olen) {
	    srcx = _pbn_realloc(src, sizeof(struct pbn)+
                                (len-1)*sizeof(pbn_limb_t));
            if (!srcx)
                goto dupx;      /* Probably futile... */

            src = srcx;
	    src->len = len;
            memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_limb_t));
	}
	return src;
    }

dupx:
    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.  Returns its argument, to make
   tailcalling possible. */
struct pbn *pbn_adjust_bits(struct pbn *pbn)
{
    int i;
    pbn_limb_t v;

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

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