[m-rev.] for review: add more operations on 32-bit unsigned integers

Peter Wang novalazy at gmail.com
Wed Mar 14 17:43:18 AEDT 2018


On Fri, 9 Mar 2018 07:37:37 -0500 (EST), Julien Fischer <jfischer at opturion.com> wrote:
> 
> Add more operations 32-bit unsigned integers.
> 
> library/uint32.m:
>      Add num_zeros/1, num_ones/1, num_leading_zeros/1, num_trailing_zeros/1
>      and reverse_bits/1.
> 
> tests/hard_coded/Mmakefile:
> tests/hard_coded/bit_twiddle_uint32.{m,exp}:
>       Add tests of the above operations.
> 
> diff --git a/library/uint32.m b/library/uint32.m
> index 3adccd2..e855864 100644
> --- a/library/uint32.m
> +++ b/library/uint32.m
> @@ -1,7 +1,7 @@
>   %---------------------------------------------------------------------------%
>   % vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
>   %---------------------------------------------------------------------------%
> -% Copyright (C) 2017 The Mercury team.
> +% Copyright (C) 2017-2018 The Mercury team.
>   % This file may only be copied under the terms of the GNU Library General
>   % Public License - see the file COPYING.LIB in the Mercury distribution.
>   %---------------------------------------------------------------------------%
> @@ -191,12 +191,44 @@
>       %
>   :- func \ (uint32::in) = (uint32::uo) is det.
> 
> +%---------------------------------------------------------------------------%
> +
> +    % num_zeros(U) = N:
> +    % N is the number of zeros in the binary representation of U.
> +    %
> +:- func num_zeros(uint32) = int.
> +
> +    % num_ones(U) = N:
> +    % N is the number of ones in the binary representation of U.
> +    %
> +:- func num_ones(uint32) = int.
> +
> +    % num_leading_zeros(U) = N:
> +    % N is the number of leading zeros in the binary representation of U.
> +    % Note that num_leading_zeros(0u32) = 32.
> +    %
> +:- func num_leading_zeros(uint32) = int.

Add "starting at the most significant bit position"?
(from the gcc documentation for __builtin_clz)

> +
> +    % num_trailing_zeros(U) = N:
> +    % N is the number of trailing zeros in the binary representation of U.
> +    % Note that num_trailing_zeros(0u32) = 32.
> +    %
> +:- func num_trailing_zeros(uint32) = int.

Same here.

> diff --git a/tests/hard_coded/bit_twiddle_uint32.m b/tests/hard_coded/bit_twiddle_uint32.m
> index e69de29..056770c 100644
> --- a/tests/hard_coded/bit_twiddle_uint32.m
> +++ b/tests/hard_coded/bit_twiddle_uint32.m
...
> +%---------------------------------------------------------------------------%
> +
> +:- func to_binary_string_lz(uint32::in) = (string::uo) is det.
> +
> +:- pragma foreign_proc("C",
> +    to_binary_string_lz(U::in) = (S::uo),
> +    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
> +"
> +    int i = 32;
> +
> +    MR_allocate_aligned_string_msg(S, 32, MR_ALLOC_ID);
> +    S[32] = '\\0';
> +    while (i > 0) {
> +        i--;
> +        S[i] = (U & 1) ? '1' : '0';
> +        U = U >> 1;
> +    }
> +").

Any reason to write this predicate in foreign code?
(a for-loop would be clearer)

The names sound fine to me.

Peter


More information about the reviews mailing list