mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
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.
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user