[m-rev.] diff: fix a bug in math.unchecked_log2/1 with the C# and Java backends

Julien Fischer jfischer at opturion.com
Thu Apr 23 12:47:21 AEST 2026


Fix a bug in math.unchecked_log2/1 with the C# and Java backends.

The Java and C# implementations of math.unchecked_log2/1 are broken when
--intermodule-optimization is enabled.

In the Java grade, the constant ML_FLOAT_LN2 has private access. This causes
a compilation error when the foreign_proc_for unchecked_log2/1 is inlined
across module boundaries.

In both the C# and Java grades, the reference to ML_FLOAT_LN2 in the
foreign_procs needs to be fully-qualified. Not doing this results
in compilation errors when the foreign_proc for unchecked_log2/1 is inlined
across module boundaries.

library/math.m:
     Make the Java version of ML_FLOAT_LN2 public.

     Fully-qualify references to ML_FLOAT_LN2 in C# and Java foreign_procs.

     Add a note about more recent versions of the .NET providing Math.Log2.

Julien.

diff --git a/library/math.m b/library/math.m
index 550beca8e..d0f03b001 100644
--- a/library/math.m
+++ b/library/math.m
@@ -303,7 +303,7 @@

     // As for .NET, java does not have a built-in ln2

-    private static final double ML_FLOAT_LN2 = 0.69314718055994530941;
+    public static final double ML_FLOAT_LN2 = 0.69314718055994530941;

 ").

@@ -659,16 +659,18 @@ log2(X) = Log :-
     unchecked_log2(X::in) = (Log2::out),
     [will_not_call_mercury, thread_safe, promise_pure],
 "
-    Log2 = System.Math.Log(X) / ML_FLOAT_LN2;
+    Log2 = System.Math.Log(X) / mercury.math.ML_FLOAT_LN2;
 ").
 :- pragma foreign_proc("Java",
     unchecked_log2(X::in) = (Log2::out),
     [will_not_call_mercury, thread_safe, promise_pure],
 "
-    Log2 = java.lang.Math.log(X) / ML_FLOAT_LN2;
+    Log2 = java.lang.Math.log(X) / jmercury.math.ML_FLOAT_LN2;
 ").
 unchecked_log2(X) = math.unchecked_ln(X) / math.unchecked_ln(2.0).

+% NOTE: more recent versions of .NET provide System.Math.Log2.
+
 log(B, X) = Log :-
     ( if
         math_domain_checks,


More information about the reviews mailing list