249
249
CAMLprim value caml_int32_div(value v1, value v2)
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));
256
return caml_copy_int32(Int32_val(v1) / divisor);
260
return caml_copy_int32(dividend / divisor);
260
264
CAMLprim value caml_int32_mod(value v1, value v2)
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));
267
return caml_copy_int32(Int32_val(v1) % divisor);
275
return caml_copy_int32(dividend % divisor);
431
439
CAMLprim value caml_int64_div(value v1, value v2)
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));
438
450
CAMLprim value caml_int64_mod(value v1, value v2)
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);
442
461
return caml_copy_int64(I64_mod(Int64_val(v1), divisor));
650
669
CAMLprim value caml_nativeint_mul(value v1, value v2)
651
670
{ return caml_copy_nativeint(Nativeint_val(v1) * Nativeint_val(v2)); }
672
#define Nativeint_min_int ((intnat) 1 << (sizeof(intnat) * 8 - 1))
653
674
CAMLprim value caml_nativeint_div(value v1, value v2)
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));
660
return caml_copy_nativeint(Nativeint_val(v1) / divisor);
685
return caml_copy_nativeint(dividend / divisor);
664
689
CAMLprim value caml_nativeint_mod(value v1, value v2)
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));
671
return caml_copy_nativeint(Nativeint_val(v1) % divisor);
700
return caml_copy_nativeint(dividend % divisor);