[m-rev.] diff: add more operations on 8-bit integers

Julien Fischer jfischer at opturion.com
Mon Apr 2 12:16:24 AEST 2018


Add more operations on 8-bit integers.

library/int8.m:
library/uint8.m:
      Add num_zeros/1, num_ones/1, num_leading_zeros/1, num_trailing_zeros/1 and
      reverse_bits/1.

library/int16.m:
      Implement num_trailing_zeros/1 by calling the unsigned version.

tests/hard_coded/Mmakefile:
tests/hard_coded/bit_twiddle_int8.{m,exp}:
tests/hard_coded/bit_twiddle_uint8.{m,exp}:
      Add tests of the new operations.

Julien.

diff --git a/library/int16.m b/library/int16.m
index 1df9d6d..202e711 100644
--- a/library/int16.m
+++ b/library/int16.m
@@ -525,8 +525,9 @@ num_leading_zeros(I16) = N :-
      U16 = uint16.cast_from_int16(I16),
      N = uint16.num_leading_zeros(U16).

-num_trailing_zeros(U) =
-    16 - num_leading_zeros(\ U /\ (U - 1i16)).
+num_trailing_zeros(I16) = N :-
+    U16 = uint16.cast_from_int16(I16),
+    N = uint16.num_trailing_zeros(U16).

  %---------------------------------------------------------------------------%

diff --git a/library/int8.m b/library/int8.m
index e1b018d..ae25bf6 100644
--- a/library/int8.m
+++ b/library/int8.m
@@ -194,10 +194,6 @@

  %---------------------------------------------------------------------------%

-    % Bitwise complement.
-    %
-:- func \ (int8::in) = (int8::uo) is det.
-
      % Bitwise and.
      %
  :- func (int8::in) /\ (int8::in) = (int8::uo) is det.
@@ -213,6 +209,42 @@
  :- mode xor(in, uo) = in is det.
  :- mode xor(uo, in) = in is det.

+    % Bitwise complement.
+    %
+:- func \ (int8::in) = (int8::uo) is det.
+
+%---------------------------------------------------------------------------%
+
+    % num_zeros(I) = N:
+    % N is the number of zeros in the binary representation of I.
+    %
+:- func num_zeros(int8) = int.
+
+    % num_ones(I) = N:
+    % N is the number of ones in the binary representation of I.
+    %
+:- func num_ones(int8) = int.
+
+    % num_leading_zeros(I) = N:
+    % N is the number of leading zeros in the binary representation of I,
+    % starting at the most significant bit position.
+    % Note that num_leading_zeros(0i8) = 8.
+    %
+:- func num_leading_zeros(int8) = int.
+
+    % num_trailing_zeros(I) = N:
+    % N is the number of trailing zeros in the binary representation of I,
+    % starting at the least significant bit position.
+    % Note that num_trailing_zeros(0i8) = 8.
+    %
+:- func num_trailing_zeros(int8) = int.
+
+    % reverse_bits(A) = B:
+    % B is the is value that results from reversing the bits in the binary
+    % representation of A.
+    %
+:- func reverse_bits(int8) = int8.
+
  %---------------------------------------------------------------------------%

  :- func min_int8 = int8.
@@ -236,6 +268,7 @@
  :- import_module require.
  :- import_module string.
  :- import_module uint.
+:- import_module uint8.

  %---------------------------------------------------------------------------%

@@ -427,6 +460,27 @@ odd(X) :-

  %---------------------------------------------------------------------------%

+num_zeros(I) = 8 - num_ones(I).
+
+num_ones(I8) = N :-
+    U8 = uint8.cast_from_int8(I8),
+    N = uint8.num_ones(U8).
+
+num_leading_zeros(I8) = N :-
+    U8 = uint8.cast_from_int8(I8),
+    N = uint8.num_leading_zeros(U8).
+
+num_trailing_zeros(I8) = N :-
+    U8 = uint8.cast_from_int8(I8),
+    N = uint8.num_trailing_zeros(U8).
+
+reverse_bits(I8) = RevI8 :-
+    U8 = uint8.cast_from_int8(I8),
+    RevU8 = uint8.reverse_bits(U8),
+    RevI8 = int8.cast_from_uint8(RevU8).
+
+%---------------------------------------------------------------------------%
+
  min_int8 = -128_i8.

  max_int8 = 127_i8.
diff --git a/library/uint8.m b/library/uint8.m
index 17b74e6..50976d4 100644
--- a/library/uint8.m
+++ b/library/uint8.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.
  %---------------------------------------------------------------------------%
@@ -179,6 +179,40 @@
      %
  :- func \ (uint8::in) = (uint8::uo) is det.

+%---------------------------------------------------------------------------%
+
+    % num_zeros(U) = N:
+    % N is the number of zeros in the binary representation of U.
+    %
+:- func num_zeros(uint8) = int.
+
+    % num_ones(U) = N:
+    % N is the number of ones in the binary representation of U.
+    %
+:- func num_ones(uint8) = int.
+
+    % num_leading_zeros(U) = N:
+    % N is the number of leading zeros in the binary representation of U,
+    % starting at teh most significant bit position.
+    % Note that num_leading_zeros(0u8) = 8.
+    %
+:- func num_leading_zeros(uint8) = int.
+
+    % num_trailing_zeros(U) = N:
+    % N is the number of trailing zeros in the binary representation of U,
+    % starting at the least significant bit position.
+    % Note that num_trailing_zeros(0u8) = 8.
+    %
+:- func num_trailing_zeros(uint8) = int.
+
+    % reverse_bits(A) = B:
+    % B is the is value that results from reversing the bits in the binary
+    % representation of A.
+    %
+:- func reverse_bits(uint8) = uint8.
+
+%---------------------------------------------------------------------------%
+
  :- func max_uint8 = uint8.

      % Convert an uint8 to a pretty_printer.doc for formatting.
@@ -358,6 +392,270 @@ odd(X) :-

  %---------------------------------------------------------------------------%

+num_zeros(U) = 8 - num_ones(U).
+
+:- pragma foreign_decl("C",
+    "extern const uint8_t ML_uint8_num_ones_table[];").
+
+:- pragma foreign_code("C", "
+
+const uint8_t ML_uint8_num_ones_table[256] = {
+    0,1,1,2,1,2,2,3,
+    1,2,2,3,2,3,3,4,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    4,5,5,6,5,6,6,7,
+    5,6,6,7,6,7,7,8
+};
+").
+
+:- pragma foreign_proc("C",
+    num_ones(U::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    N = ML_uint8_num_ones_table[U];
+").
+
+:- pragma foreign_code("C#", "
+
+public static byte[] num_ones_table = {
+    0,1,1,2,1,2,2,3,
+    1,2,2,3,2,3,3,4,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    1,2,2,3,2,3,3,4,
+    2,3,3,4,3,4,4,5,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    2,3,3,4,3,4,4,5,
+    3,4,4,5,4,5,5,6,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    3,4,4,5,4,5,5,6,
+    4,5,5,6,5,6,6,7,
+    4,5,5,6,5,6,6,7,
+    5,6,6,7,6,7,7,8
+};
+
+").
+
+:- pragma foreign_proc("C#",
+    num_ones(U::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N = mercury.uint8.num_ones_table[U];
+").
+
+:- pragma foreign_proc("Java",
+    num_ones(U::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N = java.lang.Integer.bitCount(U << 24);
+").
+
+%---------------------------------------------------------------------------%
+
+:- pragma foreign_decl("C", "extern const uint8_t ML_uint8_nlz_table[];").
+
+:- pragma foreign_code("C", "
+
+const uint8_t ML_uint8_nlz_table[256] = {
+  8,7,6,6,5,5,5,5,
+  4,4,4,4,4,4,4,4,
+  3,3,3,3,3,3,3,3,
+  3,3,3,3,3,3,3,3,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0
+};
+
+").
+
+:- pragma foreign_proc("C",
+    num_leading_zeros(I::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    N = ML_uint8_nlz_table[I];
+").
+:- pragma foreign_code("C#", "
+
+public static byte[] nlz_table = {
+  8,7,6,6,5,5,5,5,
+  4,4,4,4,4,4,4,4,
+  3,3,3,3,3,3,3,3,
+  3,3,3,3,3,3,3,3,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0
+};
+
+").
+
+:- pragma foreign_proc("C#",
+    num_leading_zeros(U::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N = mercury.uint8.nlz_table[U];
+").
+
+:- pragma foreign_code("Java", "
+
+public static byte[] nlz_table = {
+  8,7,6,6,5,5,5,5,
+  4,4,4,4,4,4,4,4,
+  3,3,3,3,3,3,3,3,
+  3,3,3,3,3,3,3,3,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  2,2,2,2,2,2,2,2,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  1,1,1,1,1,1,1,1,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0,
+  0,0,0,0,0,0,0,0
+};
+
+").
+
+:- pragma foreign_proc("Java",
+    num_leading_zeros(U::in) = (N::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N = jmercury.uint8.nlz_table[U & 0xff];
+").
+
+num_trailing_zeros(U) =
+    8 - num_leading_zeros(\ U /\ (U - 1u8)).
+
+%---------------------------------------------------------------------------%
+
+reverse_bits(!.A) = B :-
+    !:A = ((!.A /\ 0xf0_u8) >> 4) \/ ((!.A /\ 0x0f_u8) << 4),
+    !:A = ((!.A /\ 0xcc_u8) >> 2) \/ ((!.A /\ 0x33_u8) << 2),
+    !:A = ((!.A /\ 0xaa_u8) >> 1) \/ ((!.A /\ 0x55_u8) << 1),
+    B = !.A.
+
+:- pragma foreign_proc("Java",
+    reverse_bits(A::in) = (B::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    B = (byte) (java.lang.Integer.reverse(A << 24) & 0xff);
+").
+
+%---------------------------------------------------------------------------%
+
  max_uint8 = 255_u8.

  %---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index fa4732a..575c8fe 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -26,9 +26,11 @@ ORDINARY_PROGS =	\
  	bit_twiddle_int16 \
  	bit_twiddle_int32 \
  	bit_twiddle_int64 \
+	bit_twiddle_int8 \
  	bit_twiddle_uint16 \
  	bit_twiddle_uint32 \
  	bit_twiddle_uint64 \
+	bit_twiddle_uint8 \
  	boyer \
  	brace \
  	bug103 \
diff --git a/tests/hard_coded/bit_twiddle_int8.exp b/tests/hard_coded/bit_twiddle_int8.exp
index e69de29..1d8fda7 100644
--- a/tests/hard_coded/bit_twiddle_int8.exp
+++ b/tests/hard_coded/bit_twiddle_int8.exp
@@ -0,0 +1,79 @@
+*** Test function 'num_zeros' ***
+
+num_zeros(10000000) = 7
+num_zeros(11110000) = 4
+num_zeros(11110110) = 2
+num_zeros(11111000) = 3
+num_zeros(11111110) = 1
+num_zeros(11111111) = 0
+num_zeros(00000000) = 8
+num_zeros(00000001) = 7
+num_zeros(00000010) = 7
+num_zeros(00001000) = 7
+num_zeros(00001010) = 6
+num_zeros(00010000) = 7
+num_zeros(01111111) = 1
+
+*** Test function 'num_ones' ***
+
+num_ones(10000000) = 1
+num_ones(11110000) = 4
+num_ones(11110110) = 6
+num_ones(11111000) = 5
+num_ones(11111110) = 7
+num_ones(11111111) = 8
+num_ones(00000000) = 0
+num_ones(00000001) = 1
+num_ones(00000010) = 1
+num_ones(00001000) = 1
+num_ones(00001010) = 2
+num_ones(00010000) = 1
+num_ones(01111111) = 7
+
+*** Test function 'num_leading_zeros' ***
+
+num_leading_zeros(10000000) = 0
+num_leading_zeros(11110000) = 0
+num_leading_zeros(11110110) = 0
+num_leading_zeros(11111000) = 0
+num_leading_zeros(11111110) = 0
+num_leading_zeros(11111111) = 0
+num_leading_zeros(00000000) = 8
+num_leading_zeros(00000001) = 7
+num_leading_zeros(00000010) = 6
+num_leading_zeros(00001000) = 4
+num_leading_zeros(00001010) = 4
+num_leading_zeros(00010000) = 3
+num_leading_zeros(01111111) = 1
+
+*** Test function 'num_trailing_zeros' ***
+
+num_trailing_zeros(10000000) = 7
+num_trailing_zeros(11110000) = 4
+num_trailing_zeros(11110110) = 1
+num_trailing_zeros(11111000) = 3
+num_trailing_zeros(11111110) = 1
+num_trailing_zeros(11111111) = 0
+num_trailing_zeros(00000000) = 8
+num_trailing_zeros(00000001) = 0
+num_trailing_zeros(00000010) = 1
+num_trailing_zeros(00001000) = 3
+num_trailing_zeros(00001010) = 1
+num_trailing_zeros(00010000) = 4
+num_trailing_zeros(01111111) = 0
+
+*** Test function 'reverse_bits' ***
+
+reverse_bits(10000000) = 00000001
+reverse_bits(11110000) = 00001111
+reverse_bits(11110110) = 01101111
+reverse_bits(11111000) = 00011111
+reverse_bits(11111110) = 01111111
+reverse_bits(11111111) = 11111111
+reverse_bits(00000000) = 00000000
+reverse_bits(00000001) = 10000000
+reverse_bits(00000010) = 01000000
+reverse_bits(00001000) = 00010000
+reverse_bits(00001010) = 01010000
+reverse_bits(00010000) = 00001000
+reverse_bits(01111111) = 11111110
diff --git a/tests/hard_coded/bit_twiddle_int8.m b/tests/hard_coded/bit_twiddle_int8.m
index e69de29..ea3cfaa 100644
--- a/tests/hard_coded/bit_twiddle_int8.m
+++ b/tests/hard_coded/bit_twiddle_int8.m
@@ -0,0 +1,133 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test bit twiddling operations for signed 8-bit integers.
+
+:- module bit_twiddle_int8.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module int8.
+
+:- import_module list.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    run_twiddle_test(int8.num_zeros, "num_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test(int8.num_ones, "num_ones", !IO),
+    io.nl(!IO),
+    run_twiddle_test(int8.num_leading_zeros, "num_leading_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test(int8.num_trailing_zeros, "num_trailing_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test_b(int8.reverse_bits, "reverse_bits", !IO).
+
+%---------------------------------------------------------------------------%
+
+:- pred run_twiddle_test((func(int8) = 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(int8) = int)::in, string::in,
+    int8::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 int8 -> int8 functions.
+
+:- pred run_twiddle_test_b((func(int8) = int8)::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(int8) = int8)::in, string::in,
+    int8::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(int8).
+
+numbers = [
+    -128_i8,
+    -16_i8,
+    -10_i8,
+    -8_i8,
+    -2_i8,
+    -1_i8,
+    0_i8,
+    1_i8,
+    2_i8,
+    8_i8,
+    10_i8,
+    16_i8,
+    127_i8
+].
+
+%---------------------------------------------------------------------------%
+
+:- func to_binary_string_lz(int8::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 = 8;
+    uint8_t U = I;
+
+    MR_allocate_aligned_string_msg(S, 8, MR_ALLOC_ID);
+    S[8] = '\\0';
+    for (i = 7; i >=0; i--) {
+        S[i] = (U & 1) ? '1' : '0';
+        U = U >> 1;
+    }
+").
+
+:- pragma foreign_proc("C#",
+    to_binary_string_lz(U::in) = (S::uo),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    S = System.Convert.ToString((byte)U, 2).PadLeft(8, '0');
+").
+
+:- pragma foreign_proc("Java",
+    to_binary_string_lz(U::in) = (S::uo),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    S = java.lang.String.format(""%8s"",
+        java.lang.Integer.toBinaryString(U & 0xff)).replace(' ', '0');
+").
+
+%---------------------------------------------------------------------------%
+:- end_module bit_twiddle_int8.
+%---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/bit_twiddle_uint8.exp b/tests/hard_coded/bit_twiddle_uint8.exp
index e69de29..7501a7e 100644
--- a/tests/hard_coded/bit_twiddle_uint8.exp
+++ b/tests/hard_coded/bit_twiddle_uint8.exp
@@ -0,0 +1,49 @@
+*** Test function 'num_zeros' ***
+
+num_zeros(00000000) = 8
+num_zeros(00000001) = 7
+num_zeros(00000010) = 7
+num_zeros(00001000) = 7
+num_zeros(00001111) = 4
+num_zeros(00010000) = 7
+num_zeros(11111111) = 0
+
+*** Test function 'num_ones' ***
+
+num_ones(00000000) = 0
+num_ones(00000001) = 1
+num_ones(00000010) = 1
+num_ones(00001000) = 1
+num_ones(00001111) = 4
+num_ones(00010000) = 1
+num_ones(11111111) = 8
+
+*** Test function 'num_leading_zeros' ***
+
+num_leading_zeros(00000000) = 8
+num_leading_zeros(00000001) = 7
+num_leading_zeros(00000010) = 6
+num_leading_zeros(00001000) = 4
+num_leading_zeros(00001111) = 4
+num_leading_zeros(00010000) = 3
+num_leading_zeros(11111111) = 0
+
+*** Test function 'num_trailing_zeros' ***
+
+num_trailing_zeros(00000000) = 8
+num_trailing_zeros(00000001) = 0
+num_trailing_zeros(00000010) = 1
+num_trailing_zeros(00001000) = 3
+num_trailing_zeros(00001111) = 0
+num_trailing_zeros(00010000) = 4
+num_trailing_zeros(11111111) = 0
+
+*** Test function 'reverse_bits' ***
+
+reverse_bits(00000000) = 00000000
+reverse_bits(00000001) = 10000000
+reverse_bits(00000010) = 01000000
+reverse_bits(00001000) = 00010000
+reverse_bits(00001111) = 11110000
+reverse_bits(00010000) = 00001000
+reverse_bits(11111111) = 11111111
diff --git a/tests/hard_coded/bit_twiddle_uint8.m b/tests/hard_coded/bit_twiddle_uint8.m
index e69de29..057ecee 100644
--- a/tests/hard_coded/bit_twiddle_uint8.m
+++ b/tests/hard_coded/bit_twiddle_uint8.m
@@ -0,0 +1,128 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test bit twiddling operations for unsigned 8-bit integers.
+
+:- module bit_twiddle_uint8.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module uint8.
+
+:- import_module list.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    run_twiddle_test(uint8.num_zeros, "num_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test(uint8.num_ones, "num_ones", !IO),
+    io.nl(!IO),
+    run_twiddle_test(uint8.num_leading_zeros, "num_leading_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test(uint8.num_trailing_zeros, "num_trailing_zeros", !IO),
+    io.nl(!IO),
+    run_twiddle_test_b(uint8.reverse_bits, "reverse_bits", !IO).
+
+%---------------------------------------------------------------------------%
+
+% Test uint8 -> int functions.
+
+:- pred run_twiddle_test((func(uint8) = 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(uint8) = int)::in, string::in,
+    uint8::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 uint8 -> uint8 functions.
+
+:- pred run_twiddle_test_b((func(uint8) = uint8)::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(uint8) = uint8)::in, string::in,
+    uint8::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(uint8).
+
+numbers = [
+    0_u8,
+    1_u8,
+    2_u8,
+    8_u8,
+    15_u8,
+    16_u8,
+    255_u8
+].
+
+%---------------------------------------------------------------------------%
+
+:- func to_binary_string_lz(uint8::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 = 8;
+
+    MR_allocate_aligned_string_msg(S, 8, MR_ALLOC_ID);
+    S[8] = '\\0';
+    for (i = 7; i >= 0; i--) {
+        S[i] = (U & 1) ? '1' : '0';
+        U = U >> 1;
+    }
+").
+
+:- pragma foreign_proc("C#",
+    to_binary_string_lz(U::in) = (S::uo),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    S = System.Convert.ToString(U, 2).PadLeft(8, '0');
+").
+
+:- pragma foreign_proc("Java",
+    to_binary_string_lz(U::in) = (S::uo),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    S = java.lang.String.format(""%8s"",
+        java.lang.Integer.toBinaryString(U & 0xff)).replace(' ', '0');
+").
+
+%---------------------------------------------------------------------------%
+:- end_module bit_twiddle_uint8.
+%---------------------------------------------------------------------------%


More information about the reviews mailing list