[m-rev.] diff: more efficient conversion of integers to uint32s

Julien Fischer jfischer at opturion.com
Thu Jan 18 16:37:34 AEDT 2018


More efficient conversion of integers to uint32s.

library/integer.m:
     Make the bounds checks in to_int32/2 more efficient by (1) examining
     just the "sign" of the input to determine whether it is non-negative
     and (2) avoiding the creation of an integer corresponding to max_uint32.

Julien.

diff --git a/library/integer.m b/library/integer.m
index d6bc6fa..c1b08b4 100644
--- a/library/integer.m
+++ b/library/integer.m
@@ -1490,11 +1490,17 @@ det_to_int32(Integer) = Int32 :-
  %---------------------------------------------------------------------------%

  to_uint32(Integer, UInt32) :-
-    Integer >= integer.zero,
-    Integer =< integer.from_uint32(uint32.max_uint32),
-    Integer = i(_Sign, Digits),
+    Integer = i(Sign, Digits),
+    Sign >= 0,     % i.e. Integer >= 0.
+    Integer =< integer_max_uint32,
      UInt32 = uint32_list(Digits, 0u32).

+    % Return max_uint32 as an integer.
+    %
+:- func integer_max_uint32 = integer.
+
+integer_max_uint32 = i(3, [15, 16383, 16383]).
+
  :- func uint32_list(list(int), uint32) = uint32.

  uint32_list([], Accum) = Accum.


More information about the reviews mailing list