From 398afda2a84a00f7b4a362b3d0c4cf414d9d43c7 Mon Sep 17 00:00:00 2001
From: Maxime Jacquemin <maxime.jacquemin@cea.fr>
Date: Wed, 31 Jan 2024 10:50:21 +0100
Subject: [PATCH] Marking the power function as returning an option and
 avoiding exceptions

---
 src/libraries/stdlib/integer.ml                 | 2 +-
 src/libraries/stdlib/integer.mli                | 2 +-
 src/libraries/utils/floating_point.ml           | 8 ++++----
 src/plugins/eva/engine/subdivided_evaluation.ml | 5 ++---
 4 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/libraries/stdlib/integer.ml b/src/libraries/stdlib/integer.ml
index 3c50b502862..41a79b24d49 100644
--- a/src/libraries/stdlib/integer.ml
+++ b/src/libraries/stdlib/integer.ml
@@ -38,7 +38,7 @@ let two_power n =
   else
     two_power_of_int k
 
-let power_int_positive_int n e =
+let power_int_positive_int_opt n e =
   try Some (Big_int_Z.power_int_positive_int n e)
   with Invalid_argument _ -> None
 
diff --git a/src/libraries/stdlib/integer.mli b/src/libraries/stdlib/integer.mli
index 8647dbcbd7b..7c51e8732a8 100644
--- a/src/libraries/stdlib/integer.mli
+++ b/src/libraries/stdlib/integer.mli
@@ -203,7 +203,7 @@ val two_power : t -> t
 val two_power_of_int : int -> t
 (** Computes [2^n] *)
 
-val power_int_positive_int: int -> int -> t option
+val power_int_positive_int_opt : int -> int -> t option
 (** Exponentiation *)
 
 val extract_bits : start:t -> stop:t -> t -> t
diff --git a/src/libraries/utils/floating_point.ml b/src/libraries/utils/floating_point.ml
index aa389e9f96d..905b63e4aeb 100644
--- a/src/libraries/utils/floating_point.ml
+++ b/src/libraries/utils/floating_point.ml
@@ -143,10 +143,10 @@ let parse_positive_float_with_shortcut ~man_size ~min_exp ~max_exp s =
     let frac = Str.matched_group 2 s in
     let len_frac = String.length frac in
     let num = Integer.of_string (n ^ frac) in
-    let* den = Integer.power_int_positive_int 5 len_frac in
+    let* den = Integer.power_int_positive_int_opt 5 len_frac in
     if Integer.is_zero num then raise (Shortcut zero) ;
     let exp10 = match_exp 3 in
-    let+ pow5 = Integer.power_int_positive_int 5 (abs exp10) in
+    let+ pow5 = Integer.power_int_positive_int_opt 5 (abs exp10) in
     let num = if exp10 >= 0 then Integer.mul num pow5 else num in
     let den = if exp10 >= 0 then den else Integer.mul den pow5 in
     let exp = exp10 - len_frac in
@@ -156,14 +156,14 @@ let parse_positive_float_with_shortcut ~man_size ~min_exp ~max_exp s =
     let frac = Str.matched_group 2 s in
     let len_frac = String.length frac in
     let num = Integer.of_string (n ^ frac) in
-    let+ den = Integer.power_int_positive_int 5 len_frac in
+    let+ den = Integer.power_int_positive_int_opt 5 len_frac in
     return ~num ~den ~exp:(~- len_frac)
   else if Str.string_match num_exp s 0 then
     let n = Str.matched_group 1 s in
     let num = Integer.of_string n in
     if Integer.is_zero num then raise (Shortcut zero) ;
     let exp10 = match_exp 2 in
-    let+ pow5 = Integer.power_int_positive_int 5 (abs exp10) in
+    let+ pow5 = Integer.power_int_positive_int_opt 5 (abs exp10) in
     let num = if exp10 >= 0 then Integer.mul num pow5 else num in
     let den = if exp10 >= 0 then Integer.one else pow5 in
     return ~num ~den ~exp:exp10
diff --git a/src/plugins/eva/engine/subdivided_evaluation.ml b/src/plugins/eva/engine/subdivided_evaluation.ml
index 0e0c9d130e8..bfd3bd4e9a8 100644
--- a/src/plugins/eva/engine/subdivided_evaluation.ml
+++ b/src/plugins/eva/engine/subdivided_evaluation.ml
@@ -767,9 +767,8 @@ module Make
                subvalues that are all evaluated. Limits the number of splits to
                keep the number of evaluations linear on [nb]. *)
             let subdivnb =
-              let pow n e = Integer.power_int_positive_int n e |> Option.get in
-              if nb > 3
-              then (subdivnb * nb) / (Integer.to_int_exn (pow 2 (nb - 1)))
+              if 3 < nb && nb < 63
+              then (subdivnb * nb) / (1 lsl (nb - 1))
               else subdivnb
             in
             Self.result ~current:true ~once:true ~dkey
-- 
GitLab