mutated_ocaml / otherlibs / num / nat_stubs.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/***********************************************************************/
/*                                                                     */
/*                                OCaml                                */
/*                                                                     */
/*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1996 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License, with    */
/*  the special exception on linking described in file ../../LICENSE.  */
/*                                                                     */
/***********************************************************************/

/* $Id: nat_stubs.c 12800 2012-07-30 18:59:07Z doligez $ */

#include "alloc.h"
#include "config.h"
#include "custom.h"
#include "intext.h"
#include "fail.h"
#include "hash.h"
#include "memory.h"
#include "mlvalues.h"

#include "bng.h"
#include "nat.h"

/* Stub code for the Nat module. */

static intnat hash_nat(value);
static void serialize_nat(value, uintnat *, uintnat *);
static uintnat deserialize_nat(void * dst);

static struct custom_operations nat_operations = {
  "_nat",
  custom_finalize_default,
  custom_compare_default,
  hash_nat,
  serialize_nat,
  deserialize_nat,
  custom_compare_ext_default
};

CAMLprim value initialize_nat(value unit)
{
  bng_init();
  register_custom_operations(&nat_operations);
  return Val_unit;
}

CAMLprim value create_nat(value size)
{
  mlsize_t sz = Long_val(size);

  return alloc_custom(&nat_operations, sz * sizeof(value), 0, 1);
}

CAMLprim value length_nat(value nat)
{
  return Val_long(Wosize_val(nat) - 1);
}

CAMLprim value set_to_zero_nat(value nat, value ofs, value len)
{
  bng_zero(&Digit_val(nat, Long_val(ofs)), Long_val(len));
  return Val_unit;
}

CAMLprim value blit_nat(value nat1, value ofs1,
                        value nat2, value ofs2,
                        value len)
{
  bng_assign(&Digit_val(nat1, Long_val(ofs1)),
             &Digit_val(nat2, Long_val(ofs2)),
             Long_val(len));
  return Val_unit;
}

CAMLprim value set_digit_nat(value nat, value ofs, value digit)
{
  Digit_val(nat, Long_val(ofs)) = Long_val(digit);
  return Val_unit;
}

CAMLprim value nth_digit_nat(value nat, value ofs)
{
  return Val_long(Digit_val(nat, Long_val(ofs)));
}

CAMLprim value set_digit_nat_native(value nat, value ofs, value digit)
{
  Digit_val(nat, Long_val(ofs)) = Nativeint_val(digit);
  return Val_unit;
}

CAMLprim value nth_digit_nat_native(value nat, value ofs)
{
  return caml_copy_nativeint(Digit_val(nat, Long_val(ofs)));
}

CAMLprim value num_digits_nat(value nat, value ofs, value len)
{
  return Val_long(bng_num_digits(&Digit_val(nat, Long_val(ofs)),
                                 Long_val(len)));
}

CAMLprim value num_leading_zero_bits_in_digit(value nat, value ofs)
{
  return
    Val_long(bng_leading_zero_bits(Digit_val(nat, Long_val(ofs))));
}

CAMLprim value is_digit_int(value nat, value ofs)
{
  return Val_bool(Digit_val(nat, Long_val(ofs)) <= Max_long);
}

CAMLprim value is_digit_zero(value nat, value ofs)
{
  return Val_bool(Digit_val(nat, Long_val(ofs)) == 0);
}

CAMLprim value is_digit_normalized(value nat, value ofs)
{
  return
    Val_bool(Digit_val(nat, Long_val(ofs)) & ((bngdigit)1 << (BNG_BITS_PER_DIGIT-1)));
}

CAMLprim value is_digit_odd(value nat, value ofs)
{
  return Val_bool(Digit_val(nat, Long_val(ofs)) & 1);
}

CAMLprim value incr_nat(value nat, value ofs, value len, value carry_in)
{
  return Val_long(bng_add_carry(&Digit_val(nat, Long_val(ofs)),
                                Long_val(len), Long_val(carry_in)));
}

value add_nat_native(value nat1, value ofs1, value len1,
                     value nat2, value ofs2, value len2, value carry_in)
{
  return Val_long(bng_add(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                          &Digit_val(nat2, Long_val(ofs2)), Long_val(len2),
                          Long_val(carry_in)));
}

CAMLprim value add_nat(value *argv, int argn)
{
  return add_nat_native(argv[0], argv[1], argv[2], argv[3],
                        argv[4], argv[5], argv[6]);
}

CAMLprim value complement_nat(value nat, value ofs, value len)
{
  bng_complement(&Digit_val(nat, Long_val(ofs)), Long_val(len));
  return Val_unit;
}

CAMLprim value decr_nat(value nat, value ofs, value len, value carry_in)
{
  return Val_long(1 ^ bng_sub_carry(&Digit_val(nat, Long_val(ofs)),
                                    Long_val(len), 1 ^ Long_val(carry_in)));
}

value sub_nat_native(value nat1, value ofs1, value len1,
                     value nat2, value ofs2, value len2, value carry_in)
{
  return Val_long(1 ^ bng_sub(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                              &Digit_val(nat2, Long_val(ofs2)), Long_val(len2),
                              1 ^ Long_val(carry_in)));
}

CAMLprim value sub_nat(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(value nat1, value ofs1, value len1,
                            value nat2, value ofs2, value len2,
                            value nat3, value ofs3)
{
  return
    Val_long(bng_mult_add_digit(
                   &Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                   &Digit_val(nat2, Long_val(ofs2)), Long_val(len2),
                   Digit_val(nat3, Long_val(ofs3))));
}

CAMLprim value mult_digit_nat(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(value nat1, value ofs1, value len1,
                      value nat2, value ofs2, value len2,
                      value nat3, value ofs3, value len3)
{
  return
    Val_long(bng_mult_add(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                          &Digit_val(nat2, Long_val(ofs2)), Long_val(len2),
                          &Digit_val(nat3, Long_val(ofs3)), Long_val(len3)));
}

CAMLprim value mult_nat(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 square_nat_native(value nat1, value ofs1, value len1,
                        value nat2, value ofs2, value len2)
{
  return
    Val_long(bng_square_add(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                            &Digit_val(nat2, Long_val(ofs2)), Long_val(len2)));
}

CAMLprim value square_nat(value *argv, int argn)
{
  return square_nat_native(argv[0], argv[1], argv[2],
                           argv[3], argv[4], argv[5]);
}

value shift_left_nat_native(value nat1, value ofs1, value len1,
                            value nat2, value ofs2, value nbits)
{
  Digit_val(nat2, Long_val(ofs2)) =
    bng_shift_left(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                   Long_val(nbits));
  return Val_unit;
}

CAMLprim value shift_left_nat(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(value natq, value ofsq,
                           value natr, value ofsr,
                           value nat1, value ofs1, value len1,
                           value nat2, value ofs2)
{
  Digit_val(natr, Long_val(ofsr)) =
    bng_div_rem_digit(&Digit_val(natq, Long_val(ofsq)),
                      &Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                      Digit_val(nat2, Long_val(ofs2)));
  return Val_unit;
}

CAMLprim value div_digit_nat(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(value nat1, value ofs1, value len1,
                     value nat2, value ofs2, value len2)
{
  bng_div_rem(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
              &Digit_val(nat2, Long_val(ofs2)), Long_val(len2));
  return Val_unit;
}

CAMLprim value div_nat(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(value nat1, value ofs1, value len1,
                             value nat2, value ofs2, value nbits)
{
  Digit_val(nat2, Long_val(ofs2)) =
    bng_shift_right(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                    Long_val(nbits));
  return Val_unit;
}

CAMLprim value shift_right_nat(value *argv, int argn)
{
  return shift_right_nat_native(argv[0], argv[1], argv[2],
                                argv[3], argv[4], argv[5]);
}

CAMLprim value compare_digits_nat(value nat1, value ofs1,
                                  value nat2, value ofs2)
{
  bngdigit d1 = Digit_val(nat1, Long_val(ofs1));
  bngdigit d2 = Digit_val(nat2, Long_val(ofs2));
  if (d1 > d2) return Val_int(1);
  if (d1 < d2) return Val_int(-1);
  return Val_int(0);
}

value compare_nat_native(value nat1, value ofs1, value len1,
                         value nat2, value ofs2, value len2)
{
  return
    Val_int(bng_compare(&Digit_val(nat1, Long_val(ofs1)), Long_val(len1),
                        &Digit_val(nat2, Long_val(ofs2)), Long_val(len2)));
}

CAMLprim value compare_nat(value *argv, int argn)
{
  return compare_nat_native(argv[0], argv[1], argv[2],
                            argv[3], argv[4], argv[5]);
}

CAMLprim value land_digit_nat(value nat1, value ofs1, value nat2, value ofs2)
{
  Digit_val(nat1, Long_val(ofs1)) &= Digit_val(nat2, Long_val(ofs2));
  return Val_unit;
}

CAMLprim value lor_digit_nat(value nat1, value ofs1, value nat2, value ofs2)
{
  Digit_val(nat1, Long_val(ofs1)) |= Digit_val(nat2, Long_val(ofs2));
  return Val_unit;
}

CAMLprim value lxor_digit_nat(value nat1, value ofs1, value nat2, value ofs2)
{
  Digit_val(nat1, Long_val(ofs1)) ^= Digit_val(nat2, Long_val(ofs2));
  return Val_unit;
}

/* The wire format for a nat is:
   - 32-bit word: number of 32-bit words in nat
   - N 32-bit words (big-endian format)
   For little-endian platforms, the memory layout between 32-bit and 64-bit
   machines is identical, so we can write the nat using serialize_block_4.
   For big-endian 64-bit platforms, we need to swap the two 32-bit halves
   of 64-bit words to obtain the correct behavior. */

static void serialize_nat(value nat,
                          uintnat * wsize_32,
                          uintnat * wsize_64)
{
  mlsize_t len = Wosize_val(nat) - 1;

#ifdef ARCH_SIXTYFOUR
  len = len * 2; /* two 32-bit words per 64-bit digit  */
  if (len >= ((mlsize_t)1 << 32))
    failwith("output_value: nat too big");
#endif
  serialize_int_4((int32) len);
#if defined(ARCH_SIXTYFOUR) && defined(ARCH_BIG_ENDIAN)
  { int32 * p;
    mlsize_t i;
    for (i = len, p = Data_custom_val(nat); i > 0; i -= 2, p += 2) {
      serialize_int_4(p[1]);    /* low 32 bits of 64-bit digit */
      serialize_int_4(p[0]);    /* high 32 bits of 64-bit digit */
    }
  }
#else
  serialize_block_4(Data_custom_val(nat), len);
#endif
  *wsize_32 = len * 4;
  *wsize_64 = len * 4;
}

static uintnat deserialize_nat(void * dst)
{
  mlsize_t len;

  len = deserialize_uint_4();
#if defined(ARCH_SIXTYFOUR) && defined(ARCH_BIG_ENDIAN)
  { uint32 * p;
    mlsize_t i;
    for (i = len, p = dst; i > 1; i -= 2, p += 2) {
      p[1] = deserialize_uint_4();   /* low 32 bits of 64-bit digit */
      p[0] = deserialize_uint_4();   /* high 32 bits of 64-bit digit */
    }
    if (i > 0){
      p[1] = deserialize_uint_4();   /* low 32 bits of 64-bit digit */
      p[0] = 0;                      /* high 32 bits of 64-bit digit */
      ++ len;
    }
  }
#else
  deserialize_block_4(dst, len);
#if defined(ARCH_SIXTYFOUR)
  if (len & 1){
    ((uint32 *) dst)[len] = 0;
    ++ len;
  }
#endif
#endif
  return len * 4;
}

static intnat hash_nat(value v)
{
  bngsize len, i;
  uint32 h;

  len = bng_num_digits(&Digit_val(v,0), Wosize_val(v) - 1);
  h = 0;
  for (i = 0; i < len; i++) {
    bngdigit d = Digit_val(v, i);
#ifdef ARCH_SIXTYFOUR
    /* Mix the two 32-bit halves as if we were on a 32-bit platform,
       namely low 32 bits first, then high 32 bits.
       Also, ignore final 32 bits if they are zero. */
    h = caml_hash_mix_uint32(h, (uint32) d);
    d = d >> 32;
    if (d == 0 && i + 1 == len) break;
    h = caml_hash_mix_uint32(h, (uint32) d);
#else
    h = caml_hash_mix_uint32(h, d);
#endif
  }
  return h;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.