From 13c8fc6fae30e4f524ab8b6f9b1ffcf02ca133fd Mon Sep 17 00:00:00 2001 From: Julien Fischer Date: Thu, 18 Jan 2018 00:39:46 -0500 Subject: [PATCH] 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. --- library/integer.m | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/library/integer.m b/library/integer.m index d6bc6fa55..c1b08b4d0 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.