Files
mercury/tests/hard_coded/bit_twiddle_int32.m
Zoltan Somogyi 2bd7c5ee3e Rename X's aux modules as X_helper_N in hard_coded.
tests/hard_coded/*.m:
    Rename modules as mentioned above.

    In a few cases, where the main module's name itself had a suffix,
    such as "_mod_a" or "_main", remove that suffix. This entails
    renaming the .exp file as well. (In some cases, this meant that
    the name of a helper module was "taken over" by the main module
    of the test case.)

    Update all references to the moved modules.

    General updates to programming style, such as

    - replacing DCG notation with state var notation
    - replacing (C->T;E) with (if C then T else E)
    - moving pred/func declarations to just before their code
    - replacing io.write/io.nl sequences with io.write_line
    - replacing io.print/io.nl sequences with io.print_line
    - fixing too-long lines
    - fixing grammar errors in comments

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Update all references to the moved modules.

    Enable the constant_prop_int test case. The fact that it wasn't enabled
    before is probably an accident. (When constant_prop_int.m was created,
    the test case was added to a list in the Mmakefile, but that list
    was later removed due to never being referenced.)

tests/hard_coded/constant_prop_int.{m,exp}:
    Delete the calls to shift operations with negative shift amounts,
    since we have added a compile-time error for these since the test
    was originally created.
2023-06-16 08:33:22 +02:00

135 lines
3.9 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
% Test bit twiddling operations for signed 32-bit integers.
:- module bit_twiddle_int32.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int32.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
run_twiddle_test(int32.num_zeros, "num_zeros", !IO),
io.nl(!IO),
run_twiddle_test(int32.num_ones, "num_ones", !IO),
io.nl(!IO),
run_twiddle_test(int32.num_leading_zeros, "num_leading_zeros", !IO),
io.nl(!IO),
run_twiddle_test(int32.num_trailing_zeros, "num_trailing_zeros", !IO),
io.nl(!IO),
run_twiddle_test_b(int32.reverse_bits, "reverse_bits", !IO),
io.nl(!IO),
run_twiddle_test_b(int32.reverse_bytes, "reverse_bytes", !IO).
%---------------------------------------------------------------------------%
:- pred run_twiddle_test((func(int32) = int)::in, string::in,
io::di, io::uo) is det.
run_twiddle_test(Func, Desc, !IO) :-
io.format("*** Test function '%s' ***\n\n", [s(Desc)], !IO),
As = numbers,
list.foldl(run_twiddle_test_2(Func, Desc), As, !IO).
:- pred run_twiddle_test_2((func(int32) = int)::in, string::in,
int32::in, io::di, io::uo) is det.
run_twiddle_test_2(Func, Desc, A, !IO) :-
Result = Func(A),
int_to_string(Result, ResultStr),
io.format("%s(%s) = %s\n",
[s(Desc), s(to_binary_string_lz(A)), s(ResultStr)], !IO).
%---------------------------------------------------------------------------%
% Test int32 -> int32 functions.
:- pred run_twiddle_test_b((func(int32) = int32)::in, string::in,
io::di, io::uo) is det.
run_twiddle_test_b(Func, Desc, !IO) :-
io.format("*** Test function '%s' ***\n\n", [s(Desc)], !IO),
As = numbers,
list.foldl(run_twiddle_test_b_2(Func, Desc), As, !IO).
:- pred run_twiddle_test_b_2((func(int32) = int32)::in, string::in,
int32::in, io::di, io::uo) is det.
run_twiddle_test_b_2(Func, Desc, A, !IO) :-
Result = Func(A),
ResultStr = to_binary_string_lz(Result),
io.format("%s(%s) = %s\n",
[s(Desc), s(to_binary_string_lz(A)), s(ResultStr)], !IO).
%---------------------------------------------------------------------------%
:- func numbers = list(int32).
numbers = [
-2_147_483_648_i32,
-32_768_i32,
-128_i32,
0_i32,
1_i32,
2_i32,
8_i32,
10_i32,
16_i32,
127_i32,
32_767_i32,
2_147_483_647_i32
].
%---------------------------------------------------------------------------%
:- func to_binary_string_lz(int32::in) = (string::uo) is det.
:- pragma foreign_proc("C",
to_binary_string_lz(I::in) = (S::uo),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
"
int i;
uint32_t U = (uint32_t) I;
MR_allocate_aligned_string_msg(S, 32, MR_ALLOC_ID);
S[32] = '\\0';
for (i = 31; i >= 0; i--) {
S[i] = (U & 1) ? '1' : '0';
U = U >> 1;
}
").
:- pragma foreign_proc("C#",
to_binary_string_lz(I::in) = (S::uo),
[will_not_call_mercury, promise_pure, thread_safe],
"
S = System.Convert.ToString(I, 2).PadLeft(32, '0');
").
:- pragma foreign_proc("Java",
to_binary_string_lz(I::in) = (S::uo),
[will_not_call_mercury, promise_pure, thread_safe],
"
S = java.lang.String.format(""%32s"",
java.lang.Integer.toBinaryString(I)).replace(' ', '0');
").
%---------------------------------------------------------------------------%
:- end_module bit_twiddle_int32.
%---------------------------------------------------------------------------%