[m-rev.] for review: add more operations on 32-bit unsigned integers
Julien Fischer
jfischer at opturion.com
Wed Mar 14 18:01:48 AEDT 2018
On Wed, 14 Mar 2018, Peter Wang wrote:
> 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
...
>> + % 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)
Done.
>> +
>> + % 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.
Done (from the least signficant bit position).
>> 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?
It was taken from my inttypes library and predates the addition of
support for uint32s to Mercury. It's probably worth adding something
like it to the standard library at some point (in which case it could
be implemented Mercury, and this test would just call that).
> (a for-loop would be clearer)
Done.
> The names sound fine to me.
Thanks for that.
Julien.
More information about the reviews
mailing list