~ubuntu-branches/debian/sid/ocaml/sid

« back to all changes in this revision

Viewing changes to byterun/ints.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephane Glondu
  • Date: 2009-06-24 12:47:31 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624124731-7wao0d0mnk4d71ee
Remove build-dependency to docbook-* (not needed anymore, since
policy has been moved to dh-ocaml)

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
/*                                                                     */
12
12
/***********************************************************************/
13
13
 
14
 
/* $Id: ints.c,v 1.51 2008/01/11 16:13:16 doligez Exp $ */
 
14
/* $Id: ints.c,v 1.51.4.2 2009/03/28 18:43:08 xleroy Exp $ */
15
15
 
16
16
#include <stdio.h>
17
17
#include <string.h>
248
248
 
249
249
CAMLprim value caml_int32_div(value v1, value v2)
250
250
{
 
251
  int32 dividend = Int32_val(v1);
251
252
  int32 divisor = Int32_val(v2);
252
253
  if (divisor == 0) caml_raise_zero_divide();
 
254
  /* PR#4740: on some processors, division crashes on overflow.
 
255
     Implement the same behavior as for type "int". */
 
256
  if (dividend == (1<<31) && divisor == -1) return v1;
253
257
#ifdef NONSTANDARD_DIV_MOD
254
 
  return caml_copy_int32(caml_safe_div(Int32_val(v1), divisor));
 
258
  return caml_copy_int32(caml_safe_div(dividend, divisor));
255
259
#else
256
 
  return caml_copy_int32(Int32_val(v1) / divisor);
 
260
  return caml_copy_int32(dividend / divisor);
257
261
#endif
258
262
}
259
263
 
260
264
CAMLprim value caml_int32_mod(value v1, value v2)
261
265
{
 
266
  int32 dividend = Int32_val(v1);
262
267
  int32 divisor = Int32_val(v2);
263
268
  if (divisor == 0) caml_raise_zero_divide();
 
269
  /* PR#4740: on some processors, modulus crashes if division overflows.
 
270
     Implement the same behavior as for type "int". */
 
271
  if (dividend == (1<<31) && divisor == -1) return caml_copy_int32(0);
264
272
#ifdef NONSTANDARD_DIV_MOD
265
 
  return caml_copy_int32(caml_safe_mod(Int32_val(v1), divisor));
 
273
  return caml_copy_int32(caml_safe_mod(dividend, divisor));
266
274
#else
267
 
  return caml_copy_int32(Int32_val(v1) % divisor);
 
275
  return caml_copy_int32(dividend % divisor);
268
276
#endif
269
277
}
270
278
 
430
438
 
431
439
CAMLprim value caml_int64_div(value v1, value v2)
432
440
{
 
441
  int64 dividend = Int64_val(v1);
433
442
  int64 divisor = Int64_val(v2);
434
443
  if (I64_is_zero(divisor)) caml_raise_zero_divide();
 
444
  /* PR#4740: on some processors, division crashes on overflow.
 
445
     Implement the same behavior as for type "int". */
 
446
  if (I64_is_min_int(dividend) && I64_is_minus_one(divisor)) return v1;
435
447
  return caml_copy_int64(I64_div(Int64_val(v1), divisor));
436
448
}
437
449
 
438
450
CAMLprim value caml_int64_mod(value v1, value v2)
439
451
{
 
452
  int64 dividend = Int64_val(v1);
440
453
  int64 divisor = Int64_val(v2);
441
454
  if (I64_is_zero(divisor)) caml_raise_zero_divide();
 
455
  /* PR#4740: on some processors, division crashes on overflow.
 
456
     Implement the same behavior as for type "int". */
 
457
  if (I64_is_min_int(dividend) && I64_is_minus_one(divisor)) {
 
458
    int64 zero = I64_literal(0,0);
 
459
    return caml_copy_int64(zero);
 
460
  }
442
461
  return caml_copy_int64(I64_mod(Int64_val(v1), divisor));
443
462
}
444
463
 
650
669
CAMLprim value caml_nativeint_mul(value v1, value v2)
651
670
{ return caml_copy_nativeint(Nativeint_val(v1) * Nativeint_val(v2)); }
652
671
 
 
672
#define Nativeint_min_int ((intnat) 1 << (sizeof(intnat) * 8 - 1))
 
673
 
653
674
CAMLprim value caml_nativeint_div(value v1, value v2)
654
675
{
 
676
  intnat dividend = Nativeint_val(v1);
655
677
  intnat divisor = Nativeint_val(v2);
656
678
  if (divisor == 0) caml_raise_zero_divide();
 
679
  /* PR#4740: on some processors, modulus crashes if division overflows.
 
680
     Implement the same behavior as for type "int". */
 
681
  if (dividend == Nativeint_min_int && divisor == -1) return v1;
657
682
#ifdef NONSTANDARD_DIV_MOD
658
 
  return caml_copy_nativeint(caml_safe_div(Nativeint_val(v1), divisor));
 
683
  return caml_copy_nativeint(caml_safe_div(dividend, divisor));
659
684
#else
660
 
  return caml_copy_nativeint(Nativeint_val(v1) / divisor);
 
685
  return caml_copy_nativeint(dividend / divisor);
661
686
#endif
662
687
}
663
688
 
664
689
CAMLprim value caml_nativeint_mod(value v1, value v2)
665
690
{
 
691
  intnat dividend = Nativeint_val(v1);
666
692
  intnat divisor = Nativeint_val(v2);
667
693
  if (divisor == 0) caml_raise_zero_divide();
 
694
  /* PR#4740: on some processors, modulus crashes if division overflows.
 
695
     Implement the same behavior as for type "int". */
 
696
  if (dividend == Nativeint_min_int && divisor == -1) return caml_copy_nativeint(0);
668
697
#ifdef NONSTANDARD_DIV_MOD
669
 
  return caml_copy_nativeint(caml_safe_mod(Nativeint_val(v1), divisor));
 
698
  return caml_copy_nativeint(caml_safe_mod(dividend, divisor));
670
699
#else
671
 
  return caml_copy_nativeint(Nativeint_val(v1) % divisor);
 
700
  return caml_copy_nativeint(dividend % divisor);
672
701
#endif
673
702
}
674
703