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
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/***********************************************************************/
/* */
/* Caml Special Light */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1995 Institut National de Recherche en Informatique et */
/* Automatique. Distributed only by permission. */
/* */
/***********************************************************************/
/* $Id$ */
#define CAML_LIGHT
#include "BigNum.h"
#include "BntoBnn.h"
#include "mlvalues.h"
#include "alloc.h"
#include "memory.h"
#include "nat.h"
/* Stub code for the BigNum package. */
value create_nat(size) /* ML */
value size;
{
mlsize_t sz = Long_val(size);
if (sz < Max_young_wosize) {
return alloc(sz, Nat_tag);
} else {
return alloc_shr(sz, Nat_tag);
}
}
value set_to_zero_nat(nat, ofs, len) /* ML */
value nat, ofs, len;
{
BnSetToZero(Bignum_val(nat), Long_val(ofs), Long_val(len));
return Val_unit;
}
value blit_nat(nat1, ofs1, nat2, ofs2, len) /* ML */
value nat1, ofs1, nat2, ofs2, len;
{
BnAssign(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2),
Long_val(len));
return Val_unit;
}
value set_digit_nat(nat, ofs, digit) /* ML */
value nat, ofs, digit;
{
BnSetDigit(Bignum_val(nat), Long_val(ofs), Long_val(digit));
return Val_unit;
}
value nth_digit_nat(nat, ofs) /* ML */
value nat, ofs;
{
return Val_long(BnGetDigit(Bignum_val(nat), Long_val(ofs)));
}
value num_digits_nat(nat, ofs, len) /* ML */
value nat, ofs, len;
{
return Val_long(BnNumDigits(Bignum_val(nat), Long_val(ofs), Long_val(len)));
}
value num_leading_zero_bits_in_digit(nat, ofs) /* ML */
value nat, ofs;
{
return
Val_long(BnNumLeadingZeroBitsInDigit(Bignum_val(nat), Long_val(ofs)));
}
value is_digit_int(nat, ofs) /* ML */
value nat, ofs;
{
return Val_bool(BnDoesDigitFitInWord(Bignum_val(nat), Long_val(ofs)));
}
value is_digit_zero(nat, ofs) /* ML */
value nat, ofs;
{
return Val_bool(BnIsDigitZero(Bignum_val(nat), Long_val(ofs)));
}
value is_digit_normalized(nat, ofs) /* ML */
value nat, ofs;
{
return Val_bool(BnIsDigitNormalized(Bignum_val(nat), Long_val(ofs)));
}
value is_digit_odd(nat, ofs) /* ML */
value nat, ofs;
{
return Val_bool(BnIsDigitOdd(Bignum_val(nat), Long_val(ofs)));
}
value incr_nat(nat, ofs, len, carry_in) /* ML */
value nat, ofs, len, carry_in;
{
return Val_long(BnAddCarry(Bignum_val(nat), Long_val(ofs),
Long_val(len), Long_val(carry_in)));
}
value add_nat_native(nat1, ofs1, len1, nat2, ofs2, len2, carry_in)
value nat1, ofs1, len1, nat2, ofs2, len2, carry_in;
{
return Val_long(BnAdd(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2),
Long_val(carry_in)));
}
value add_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return add_nat_native(argv[0], argv[1], argv[2], argv[3],
argv[4], argv[5], argv[6]);
}
value complement_nat(nat, ofs, len) /* ML */
value nat, ofs, len;
{
BnComplement(Bignum_val(nat), Long_val(ofs), Long_val(len));
return Val_unit;
}
value decr_nat(nat, ofs, len, carry_in) /* ML */
value nat, ofs, len, carry_in;
{
return Val_long(BnSubtractBorrow(Bignum_val(nat), Long_val(ofs),
Long_val(len), Long_val(carry_in)));
}
value sub_nat_native(nat1, ofs1, len1, nat2, ofs2, len2, carry_in)
value nat1, ofs1, len1, nat2, ofs2, len2, carry_in;
{
return Val_long(BnSubtract(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2),
Long_val(carry_in)));
}
value sub_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return sub_nat_native(argv[0], argv[1], argv[2], argv[3],
argv[4], argv[5], argv[6]);
}
value mult_digit_nat_native(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3)
value nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3;
{
return
Val_long(BnMultiplyDigit(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2),
Bignum_val(nat3), Long_val(ofs3)));
}
value mult_digit_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return mult_digit_nat_native(argv[0], argv[1], argv[2], argv[3],
argv[4], argv[5], argv[6], argv[7]);
}
value mult_nat_native(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3)
value nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3;
{
return
Val_long(BnMultiply(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2),
Bignum_val(nat3), Long_val(ofs3), Long_val(len3)));
}
value mult_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return mult_nat_native(argv[0], argv[1], argv[2], argv[3],
argv[4], argv[5], argv[6], argv[7], argv[8]);
}
value shift_left_nat_native(nat1, ofs1, len1, nat2, ofs2, nbits)
value nat1, ofs1, len1, nat2, ofs2, nbits;
{
BnShiftLeft(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(nbits));
return Val_unit;
}
value shift_left_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return shift_left_nat_native(argv[0], argv[1], argv[2],
argv[3], argv[4], argv[5]);
}
value div_digit_nat_native(natq, ofsq, natr, ofsr, nat1, ofs1, len1, nat2, ofs2)
value natq, ofsq, natr, ofsr, nat1, ofs1, len1, nat2, ofs2;
{
BnDivideDigit(Bignum_val(natq), Long_val(ofsq),
Bignum_val(natr), Long_val(ofsr),
Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2));
return Val_unit;
}
value div_digit_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return div_digit_nat_native(argv[0], argv[1], argv[2], argv[3],
argv[4], argv[5], argv[6], argv[7], argv[8]);
}
value div_nat_native(nat1, ofs1, len1, nat2, ofs2, len2)
value nat1, ofs1, len1, nat2, ofs2, len2;
{
BnDivide(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2));
return Val_unit;
}
value div_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return div_nat_native(argv[0], argv[1], argv[2],
argv[3], argv[4], argv[5]);
}
value shift_right_nat_native(nat1, ofs1, len1, nat2, ofs2, nbits)
value nat1, ofs1, len1, nat2, ofs2, nbits;
{
BnShiftRight(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(nbits));
return Val_unit;
}
value shift_right_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return shift_right_nat_native(argv[0], argv[1], argv[2],
argv[3], argv[4], argv[5]);
}
value compare_digits_nat(nat1, ofs1, nat2, ofs2) /* ML */
value nat1, ofs1, nat2, ofs2;
{
return Val_long(BnCompareDigits(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2)));
}
value compare_nat_native(nat1, ofs1, len1, nat2, ofs2, len2)
value nat1, ofs1, len1, nat2, ofs2, len2;
{
return Val_long(BnCompare(Bignum_val(nat1), Long_val(ofs1), Long_val(len1),
Bignum_val(nat2), Long_val(ofs2), Long_val(len2)));
}
value compare_nat(argv, argn) /* ML */
value * argv;
int argn;
{
return compare_nat_native(argv[0], argv[1], argv[2],
argv[3], argv[4], argv[5]);
}
value land_digit_nat(nat1, ofs1, nat2, ofs2) /* ML */
value nat1, ofs1, nat2, ofs2;
{
BnAndDigits(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2));
return Val_unit;
}
value lor_digit_nat(nat1, ofs1, nat2, ofs2) /* ML */
value nat1, ofs1, nat2, ofs2;
{
BnOrDigits(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2));
return Val_unit;
}
value lxor_digit_nat(nat1, ofs1, nat2, ofs2) /* ML */
value nat1, ofs1, nat2, ofs2;
{
BnXorDigits(Bignum_val(nat1), Long_val(ofs1),
Bignum_val(nat2), Long_val(ofs2));
return Val_unit;
}
|