summaryrefslogtreecommitdiffstats
path: root/mpn_init.c
blob: 2770b9f7f3c47f120d35733eed636b0a13725f57 (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.
 *
 * ----------------------------------------------------------------------- */

/*
 * mpn_int.c
 */

#define MPN_INIT 1
#include <string.h>
#include "mpnint.h"

struct mpn *mpn_new(int len)
{
    struct mpn *mpn;

    mpn = _mpn_zalloc(sizeof(struct mpn)+(len-1)*sizeof(mpn_atom_t));
    if (!mpn)
	return NULL;

    mpn->ref = 1;
    mpn->len = len;

    return mpn;
}

void mpn_free(struct mpn *mpn)
{
    if (! --mpn->ref)
	_mpn_free(mpn);
}

#if MPN_ATOM_BITS != 32 && MPN_ATOM_BITS != 64
# error "Fix mpn_ilog2p1 for new MPN_ATOM_BITS size"
#endif

static int mpn_ilog2p1(mpn_atom_t v)
{
    int p = 1;

#if MPN_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 mpn *mpn_int(mpn_satom_t v)
{
    struct mpn *mpn = mpn_new(1);
    if (!mpn)
	return NULL;

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

    mpn->num[0] = v;
    mpn->bits   = mpn_ilog2p1(v);
    return mpn;
}

/* Produce a duplicate */
struct mpn *mpn_dup(const struct mpn *src)
{
    int bytes = sizeof(struct mpn)+(src->len-1)*sizeof(mpn_atom_t);
    struct mpn *dst = _mpn_malloc(bytes);

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

    return dst;
}

/* Produce a duplicate, possibly extended */
struct mpn *mpn_dupx(const struct mpn *src, int len)
{
    int slack = src->len >= len ? 0 : len-src->len;
    int bytes = sizeof(struct mpn)+(src->len-1)*sizeof(mpn_atom_t);
    struct mpn *dst = _mpn_malloc(bytes+slack*sizeof(mpn_atom_t));

    if (!dst)
	return NULL;

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

    return dst;
}

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

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

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

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

    for (i = mpn->len-1; i >= 0; i--) {
	v = mpn->num[i];
	if (v) {
	    mpn->bits = i*MPN_ATOM_BITS + mpn_ilog2p1(v);
	    return;
	}
    }

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