/
P6bigint.c
287 lines (251 loc) · 8.88 KB
/
P6bigint.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "moar.h"
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
/* Get a native int64 from an mp_int. */
static MVMint64 mp_get_int64(MVMThreadContext *tc, mp_int * a) {
MVMuint64 res;
MVMuint64 signed_max = 9223372036854775807ULL;
if (a->used == 0) {
return 0;
}
/* For 64-bit 2's complement numbers the positive max is 2**63-1
* but the negative max is 2**63. */
if (MP_NEG == SIGN(a)) {
++signed_max;
}
res = mp_get_long_long(a);
if (res == 0 || res > signed_max) {
/* The mp_int was bigger than a signed result could be. */
MVM_exception_throw_adhoc(tc, "Cannot unbox %d bit wide bigint into native integer", mp_count_bits(a));
}
return MP_NEG == SIGN(a) ? -res : res;
}
/* Get a native uint64 from an mp_int. */
static MVMuint64 mp_get_uint64(MVMThreadContext *tc, mp_int * a) {
MVMuint64 res;
if (a->used == 0) {
return 0;
}
res = mp_get_long_long(a);
if (res == 0) {
/* The mp_int was so big it overflowed a MVMuint64. */
MVM_exception_throw_adhoc(tc, "Cannot unbox %d bit wide bigint into native integer", mp_count_bits(a));
}
return res;
}
/* This representation's function pointer table. */
static const MVMREPROps P6bigint_this_repr;
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
MVMSTable *st = MVM_gc_allocate_stable(tc, &P6bigint_this_repr, HOW);
MVMROOT(tc, st, {
MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
st->size = sizeof(MVMP6bigint);
});
return st->WHAT;
}
/* Initializes a new instance. */
static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
}
/* Copies the body of one object to another. */
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
MVMP6bigintBody *src_body = (MVMP6bigintBody *)src;
MVMP6bigintBody *dest_body = (MVMP6bigintBody *)dest;
if (MVM_BIGINT_IS_BIG(src_body)) {
dest_body->u.bigint = MVM_malloc(sizeof(mp_int));
mp_init_copy(dest_body->u.bigint, src_body->u.bigint);
}
else {
dest_body->u.smallint.flag = src_body->u.smallint.flag;
dest_body->u.smallint.value = src_body->u.smallint.value;
}
}
static void set_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 value) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_IS_32BIT_INT(value)) {
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = (MVMint32)value;
}
else {
mp_int *i = MVM_malloc(sizeof(mp_int));
mp_init(i);
if (value >= 0) {
MVM_bigint_mp_set_uint64(i, (MVMuint64)value);
}
else {
MVM_bigint_mp_set_uint64(i, (MVMuint64)-value);
mp_neg(i, i);
}
body->u.bigint = i;
}
}
static MVMint64 get_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_BIGINT_IS_BIG(body)) {
mp_int *i = body->u.bigint;
return mp_get_int64(tc, i);
}
else {
return body->u.smallint.value;
}
}
static void set_uint(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMuint64 value) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (value < 2147483647ULL) {
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = (MVMint32)value;
}
else {
mp_int *i = MVM_malloc(sizeof(mp_int));
mp_init(i);
MVM_bigint_mp_set_uint64(i, value);
body->u.bigint = i;
}
}
static MVMuint64 get_uint(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_BIGINT_IS_BIG(body)) {
mp_int *i = body->u.bigint;
if (MP_NEG == SIGN(i))
MVM_exception_throw_adhoc(tc, "Cannot unbox negative bigint into native unsigned integer");
else
return mp_get_uint64(tc, i);
}
else {
return body->u.smallint.value;
}
}
static void * get_boxed_ref(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMuint32 repr_id) {
if (repr_id == MVM_REPR_ID_P6bigint)
return data;
MVM_exception_throw_adhoc(tc,
"P6bigint representation cannot unbox to other types");
}
static const MVMStorageSpec storage_spec = {
MVM_STORAGE_SPEC_INLINED, /* inlineable */
sizeof(MVMP6bigintBody) * 8, /* bits */
ALIGNOF(MVMP6bigintBody), /* align */
MVM_STORAGE_SPEC_BP_INT, /* boxed_primitive */
MVM_STORAGE_SPEC_CAN_BOX_INT, /* can_box */
0, /* is_unsigned */
};
/* Gets the storage specification for this representation. */
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
return &storage_spec;
}
/* Compose the representation. */
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
/* Nothing to do for this REPR. */
}
static void gc_cleanup(MVMThreadContext *tc, MVMSTable *st, void *data) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_BIGINT_IS_BIG(body)) {
mp_clear(body->u.bigint);
MVM_free(body->u.bigint);
}
}
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
MVMP6bigintBody *body = &((MVMP6bigint *)obj)->body;
if (MVM_BIGINT_IS_BIG(body)) {
mp_clear(body->u.bigint);
MVM_free(body->u.bigint);
}
}
/* Serializes the bigint. */
static void serialize(MVMThreadContext *tc, MVMSTable *st, void *data, MVMSerializationWriter *writer) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_BIGINT_IS_BIG(body)) {
mp_int *i = body->u.bigint;
int len;
char *buf;
MVMString *str;
mp_radix_size(i, 10, &len);
buf = (char *)MVM_malloc(len);
mp_toradix(i, buf, 10);
/* len - 1 because buf is \0-terminated */
str = MVM_string_ascii_decode(tc, tc->instance->VMString, buf, len - 1);
/* write the "is small" flag */
MVM_serialization_write_int(tc, writer, 0);
MVM_serialization_write_str(tc, writer, str);
MVM_free(buf);
}
else {
/* write the "is small" flag */
MVM_serialization_write_int(tc, writer, 1);
MVM_serialization_write_int(tc, writer, body->u.smallint.value);
}
}
/* Set the size on the STable. */
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
st->size = sizeof(MVMP6bigint);
}
/* Deserializes the bigint. */
static void deserialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMSerializationReader *reader) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_serialization_read_int(tc, reader) == 1) { /* Is it small int? */
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = MVM_serialization_read_int(tc, reader);
} else { /* big int */
char *buf = MVM_string_ascii_encode(tc, MVM_serialization_read_str(tc, reader), NULL, 0);
body->u.bigint = MVM_malloc(sizeof(mp_int));
mp_init(body->u.bigint);
mp_read_radix(body->u.bigint, buf, 10);
MVM_free(buf);
}
}
/* Calculates the non-GC-managed memory we hold on to. */
static MVMuint64 unmanaged_size(MVMThreadContext *tc, MVMSTable *st, void *data) {
MVMP6bigintBody *body = (MVMP6bigintBody *)data;
if (MVM_BIGINT_IS_BIG(body))
return body->u.bigint->alloc;
else
return 0;
}
/* Initializes the representation. */
const MVMREPROps * MVMP6bigint_initialize(MVMThreadContext *tc) {
return &P6bigint_this_repr;
}
static const MVMREPROps P6bigint_this_repr = {
type_object_for,
MVM_gc_allocate_object,
initialize,
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
{
set_int,
get_int,
MVM_REPR_DEFAULT_SET_NUM,
MVM_REPR_DEFAULT_GET_NUM,
MVM_REPR_DEFAULT_SET_STR,
MVM_REPR_DEFAULT_GET_STR,
set_uint,
get_uint,
get_boxed_ref
}, /* box_funcs */
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
MVM_REPR_DEFAULT_ELEMS,
get_storage_spec,
NULL, /* change_type */
serialize,
deserialize,
NULL, /* serialize_repr_data */
NULL, /* deserialize_repr_data */
deserialize_stable_size,
NULL, /* gc_mark */
gc_free,
gc_cleanup,
NULL, /* gc_mark_repr_data */
NULL, /* gc_free_repr_data */
compose,
NULL, /* spesh */
"P6bigint", /* name */
MVM_REPR_ID_P6bigint,
unmanaged_size, /* unmanaged_size */
NULL, /* describe_refs */
};