[m-rev.] diff: add bitwise rotations for uint16 and uint8s

Julien Fischer jfischer at opturion.com
Mon Mar 29 17:01:55 AEDT 2021


Add bitwise rotations for uint16s and uint8s.

library/uint8.m:
library/uint16.m:
     As above.

tests/hard_coded/Mmakefile:
tests/hard_coded/rotate_uint{8,16}.{m,ex}:
     Add tests for the new functions.

Julien.

diff --git a/library/uint16.m b/library/uint16.m
index 59ba0b7..06cd2ad 100644
--- a/library/uint16.m
+++ b/library/uint16.m
@@ -338,6 +338,34 @@
      %
  :- func reverse_bits(uint16) = uint16.

+    % rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % left by D bits. Throws an exception if D is not in [0, 15].
+    %
+:- func rotate_left(uint16, uint) = uint16.
+
+    % unchecked_rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % left by an amount given by the lowest 4 bits of D.
+    %
+:- func unchecked_rotate_left(uint16, uint) = uint16.
+
+    % rotate_right(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % right by D bits. Throws an exception if D is not in [0, 15].
+    %
+:- func rotate_right(uint16, uint) = uint16.
+
+    % unchecked_rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % right by an amount given by the lowest 4 bits of D.
+    %
+:- func unchecked_rotate_right(uint16, uint) = uint16.
+
  %---------------------------------------------------------------------------%
  %
  % Limits.
@@ -764,6 +792,73 @@ reverse_bits(!.A) = B :-

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

+rotate_left(X, N) =
+    ( if N < 16u then
+        unchecked_rotate_left(X, N)
+    else
+        func_error($pred, "rotate amount exceeds 15 bits")
+    ).
+
+:- pragma foreign_proc("C",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    // XXX clang has intrinsics for rotation -- we should use those instead.
+    Result = (X << N) | (X >> (-N & 15));
+").
+
+:- pragma foreign_proc("C#",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    Result = (ushort) ((X << (int) N) | (X >> (int) (-N & 15)));
+").
+
+:- pragma foreign_proc("Java",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    Result = (short) ((X << (int) N) | (X >>> (int) (-N & 15)));
+").
+
+%---------------------------------------------------------------------------%
+
+rotate_right(X, N) =
+    ( if N < 16u then
+        unchecked_rotate_right(X, N)
+    else
+        func_error($pred, "rotate amount exceeds 15 bits")
+    ).
+
+:- pragma foreign_proc("C",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    Result = (X >> N) | (X << (-N & 15));
+").
+
+:- pragma foreign_proc("C#",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    Result = (ushort) ((X >> (int) N) | (X << (int) (-N & 15)));
+").
+
+:- pragma foreign_proc("Java",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 15;
+    Result = (short) ((X >>> (int) N) | (X << (int) (-N & 15)));
+").
+
+%---------------------------------------------------------------------------%
+
  max_uint16 = 65_535_u16.

  %---------------------------------------------------------------------------%
diff --git a/library/uint8.m b/library/uint8.m
index b2ce2df..bb32790 100644
--- a/library/uint8.m
+++ b/library/uint8.m
@@ -291,6 +291,34 @@
      %
  :- func reverse_bits(uint8) = uint8.

+    % rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % left by D bits. Throws an exception if D is not in [0, 7].
+    %
+:- func rotate_left(uint8, uint) = uint8.
+
+    % unchecked_rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % left by an amount given by the lowest 3 bits of D.
+    %
+:- func unchecked_rotate_left(uint8, uint) = uint8.
+
+    % rotate_right(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % right by D bits. Throws an exception if D is not in [0, 7].
+    %
+:- func rotate_right(uint8, uint) = uint8.
+
+    % unchecked_rotate_left(U, D) = N:
+    %
+    % N is the value obtained by rotating the binary representation of U
+    % right by an amount given by the lowest 3 bits of D.
+    %
+:- func unchecked_rotate_right(uint8, uint) = uint8.
+
  %---------------------------------------------------------------------------%
  %
  % Limits.
@@ -777,6 +805,73 @@ reverse_bits(!.A) = B :-

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

+rotate_left(X, N) =
+    ( if N < 8u then
+        unchecked_rotate_left(X, N)
+    else
+        func_error($pred, "rotate amount exceeds 7 bits")
+    ).
+
+:- pragma foreign_proc("C",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    // XXX clang has intrinsics for rotation -- we should use those instead.
+    Result = (X << N) | (X >> (-N & 7));
+").
+
+:- pragma foreign_proc("C#",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    Result = (byte) ((X << (int) N) | (X >> (int) (-N & 7)));
+").
+
+:- pragma foreign_proc("Java",
+    unchecked_rotate_left(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    Result = (byte) ((X << (int) N) | (X >>> (int) (-N & 7)));
+").
+
+%---------------------------------------------------------------------------%
+
+rotate_right(X, N) =
+    ( if N < 8u then
+        unchecked_rotate_right(X, N)
+    else
+        func_error($pred, "rotate amount exceeds 7 bits")
+    ).
+
+:- pragma foreign_proc("C",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    Result = (X >> N) | (X << (-N & 7));
+").
+
+:- pragma foreign_proc("C#",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    Result = (byte) ((X >> (int) N) | (X << (int) (-N & 7)));
+").
+
+:- pragma foreign_proc("Java",
+    unchecked_rotate_right(X::in, N::in) = (Result::out),
+    [will_not_call_mercury, promise_pure, thread_safe],
+"
+    N &= 7;
+    Result = (byte) ((X >>> (int) N) | (X << (int) (-N & 7)));
+").
+
+%---------------------------------------------------------------------------%
+
  max_uint8 = 255_u8.

  %---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 5b5bf05..5a30010 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -772,8 +772,10 @@ ifeq "$(findstring profdeep,$(GRADE))" ""
  		map_merge_test \
  		mutable_excp \
  		null_char \
+		rotate_uint16 \
  		rotate_uint32 \
  		rotate_uint64 \
+		rotate_uint8 \
  		shift_test \
  		string_append_pieces \
  		string_first_char_ilseq \
diff --git a/tests/hard_coded/rotate_uint16.exp b/tests/hard_coded/rotate_uint16.exp
index e69de29..495bbb2 100644
--- a/tests/hard_coded/rotate_uint16.exp
+++ b/tests/hard_coded/rotate_uint16.exp
@@ -0,0 +1,88 @@
+*** Test 'rotate_left' ***
+
+          rotate_left(0000000000000001, 0) = 0000000000000001
+unchecked_rotate_left(0000000000000001, 0) = 0000000000000001
+
+          rotate_left(0000000000000001, 1) = 0000000000000010
+unchecked_rotate_left(0000000000000001, 1) = 0000000000000010
+
+          rotate_left(0000000000000001, 2) = 0000000000000100
+unchecked_rotate_left(0000000000000001, 2) = 0000000000000100
+
+          rotate_left(0000000000000001, 15) = 1000000000000000
+unchecked_rotate_left(0000000000000001, 15) = 1000000000000000
+
+          rotate_left(0000000000000001, 16) = <<exception>>
+unchecked_rotate_left(0000000000000001, 16) = 0000000000000001
+
+          rotate_left(0000000000000001, 31) = <<exception>>
+unchecked_rotate_left(0000000000000001, 31) = 1000000000000000
+
+          rotate_left(0000000000000001, 32) = <<exception>>
+unchecked_rotate_left(0000000000000001, 32) = 0000000000000001
+
+          rotate_left(0000000011111111, 0) = 0000000011111111
+unchecked_rotate_left(0000000011111111, 0) = 0000000011111111
+
+          rotate_left(0000000011111111, 1) = 0000000111111110
+unchecked_rotate_left(0000000011111111, 1) = 0000000111111110
+
+          rotate_left(0000000011111111, 2) = 0000001111111100
+unchecked_rotate_left(0000000011111111, 2) = 0000001111111100
+
+          rotate_left(0000000011111111, 15) = 1000000001111111
+unchecked_rotate_left(0000000011111111, 15) = 1000000001111111
+
+          rotate_left(0000000011111111, 16) = <<exception>>
+unchecked_rotate_left(0000000011111111, 16) = 0000000011111111
+
+          rotate_left(0000000011111111, 31) = <<exception>>
+unchecked_rotate_left(0000000011111111, 31) = 1000000001111111
+
+          rotate_left(0000000011111111, 32) = <<exception>>
+unchecked_rotate_left(0000000011111111, 32) = 0000000011111111
+
+*** Test 'rotate_right' ***
+
+          rotate_right(0000000000000001, 0) = 0000000000000001
+unchecked_rotate_right(0000000000000001, 0) = 0000000000000001
+
+          rotate_right(0000000000000001, 1) = 1000000000000000
+unchecked_rotate_right(0000000000000001, 1) = 1000000000000000
+
+          rotate_right(0000000000000001, 2) = 0100000000000000
+unchecked_rotate_right(0000000000000001, 2) = 0100000000000000
+
+          rotate_right(0000000000000001, 15) = 0000000000000010
+unchecked_rotate_right(0000000000000001, 15) = 0000000000000010
+
+          rotate_right(0000000000000001, 16) = <<exception>>
+unchecked_rotate_right(0000000000000001, 16) = 0000000000000001
+
+          rotate_right(0000000000000001, 31) = <<exception>>
+unchecked_rotate_right(0000000000000001, 31) = 0000000000000010
+
+          rotate_right(0000000000000001, 32) = <<exception>>
+unchecked_rotate_right(0000000000000001, 32) = 0000000000000001
+
+          rotate_right(0000000011111111, 0) = 0000000011111111
+unchecked_rotate_right(0000000011111111, 0) = 0000000011111111
+
+          rotate_right(0000000011111111, 1) = 1000000001111111
+unchecked_rotate_right(0000000011111111, 1) = 1000000001111111
+
+          rotate_right(0000000011111111, 2) = 1100000000111111
+unchecked_rotate_right(0000000011111111, 2) = 1100000000111111
+
+          rotate_right(0000000011111111, 15) = 0000000111111110
+unchecked_rotate_right(0000000011111111, 15) = 0000000111111110
+
+          rotate_right(0000000011111111, 16) = <<exception>>
+unchecked_rotate_right(0000000011111111, 16) = 0000000011111111
+
+          rotate_right(0000000011111111, 31) = <<exception>>
+unchecked_rotate_right(0000000011111111, 31) = 0000000111111110
+
+          rotate_right(0000000011111111, 32) = <<exception>>
+unchecked_rotate_right(0000000011111111, 32) = 0000000011111111
+
diff --git a/tests/hard_coded/rotate_uint16.m b/tests/hard_coded/rotate_uint16.m
index e69de29..a2a7e32 100644
--- a/tests/hard_coded/rotate_uint16.m
+++ b/tests/hard_coded/rotate_uint16.m
@@ -0,0 +1,124 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+
+:- module rotate_uint16.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+:- implementation.
+
+:- import_module exception.
+:- import_module list.
+:- import_module uint16.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    run_test(rotate_left, unchecked_rotate_left, "rotate_left", !IO),
+    run_test(rotate_right,unchecked_rotate_right,  "rotate_right", !IO).
+
+%---------------------------------------------------------------------------%
+
+:- pred run_test((func(uint16, uint) = uint16)::in,
+    (func(uint16, uint) = uint16)::in,
+    string::in, io::di, io::uo) is cc_multi.
+
+run_test(CheckedFunc, UncheckedFunc, Desc, !IO) :-
+    io.format("*** Test '%s' ***\n\n", [s(Desc)], !IO),
+    Ns = numbers,
+    Ds = distances,
+    list.foldl(run_test_2(CheckedFunc, UncheckedFunc, Desc, Ds), Ns, !IO).
+
+:- pred run_test_2((func(uint16, uint) = uint16)::in,
+    (func(uint16, uint) = uint16)::in, string::in,
+    list(uint)::in, uint16::in, io::di, io::uo) is cc_multi.
+
+run_test_2(CheckedFunc, UncheckedFunc, Desc, Ds, N, !IO) :-
+    list.foldl(run_test_3(CheckedFunc, UncheckedFunc, Desc, N), Ds, !IO).
+
+:- pred run_test_3((func(uint16, uint) = uint16)::in,
+    (func(uint16, uint) = uint16)::in, string::in, uint16::in,
+    uint::in, io::di, io::uo) is cc_multi.
+
+run_test_3(CheckedFunc, UncheckedFunc, Desc, N, D, !IO) :-
+    do_eval(CheckedFunc, N, D, CheckedResult),
+    io.format("          %s(%s, %u) = %s\n",
+        [s(Desc), s(to_binary_string_lz(N)), u(D), s(CheckedResult)], !IO),
+    do_eval(UncheckedFunc, N, D, UncheckedResult),
+    io.format("unchecked_%s(%s, %u) = %s\n",
+        [s(Desc), s(to_binary_string_lz(N)), u(D), s(UncheckedResult)], !IO),
+    io.nl(!IO).
+
+:- pred do_eval((func(uint16, uint) = uint16)::in, uint16::in, uint::in,
+    string::out) is cc_multi.
+
+do_eval(Func, N, D, ResultStr) :-
+    ( try []
+        Result0 = Func(N, D)
+    then
+        ResultStr = to_binary_string_lz(Result0)
+    catch_any _ ->
+        ResultStr = "<<exception>>"
+    ).
+
+%---------------------------------------------------------------------------%
+
+:- func numbers = list(uint16).
+
+numbers = [
+    1_u16,
+    255_u16
+].
+
+:- func distances = list(uint).
+
+distances = [
+    0_u,
+    1_u,
+    2_u,
+    15_u,
+    16_u,
+    31_u,
+    32_u
+].
+
+%---------------------------------------------------------------------------%
+
+:- func to_binary_string_lz(uint16::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;
+
+    MR_allocate_aligned_string_msg(S, 16, MR_ALLOC_ID);
+    S[16] = '\\0';
+    for (i = 15; 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(16, '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(""%16s"",
+        java.lang.Integer.toBinaryString(U & 0xffff)).replace(' ', '0');
+").
+
+%---------------------------------------------------------------------------%
+:- end_module rotate_uint16.
+%---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/rotate_uint8.exp b/tests/hard_coded/rotate_uint8.exp
index e69de29..50885df 100644
--- a/tests/hard_coded/rotate_uint8.exp
+++ b/tests/hard_coded/rotate_uint8.exp
@@ -0,0 +1,88 @@
+*** Test 'rotate_left' ***
+
+          rotate_left(00000001, 0) = 00000001
+unchecked_rotate_left(00000001, 0) = 00000001
+
+          rotate_left(00000001, 1) = 00000010
+unchecked_rotate_left(00000001, 1) = 00000010
+
+          rotate_left(00000001, 2) = 00000100
+unchecked_rotate_left(00000001, 2) = 00000100
+
+          rotate_left(00000001, 7) = 10000000
+unchecked_rotate_left(00000001, 7) = 10000000
+
+          rotate_left(00000001, 8) = <<exception>>
+unchecked_rotate_left(00000001, 8) = 00000001
+
+          rotate_left(00000001, 15) = <<exception>>
+unchecked_rotate_left(00000001, 15) = 10000000
+
+          rotate_left(00000001, 16) = <<exception>>
+unchecked_rotate_left(00000001, 16) = 00000001
+
+          rotate_left(00001111, 0) = 00001111
+unchecked_rotate_left(00001111, 0) = 00001111
+
+          rotate_left(00001111, 1) = 00011110
+unchecked_rotate_left(00001111, 1) = 00011110
+
+          rotate_left(00001111, 2) = 00111100
+unchecked_rotate_left(00001111, 2) = 00111100
+
+          rotate_left(00001111, 7) = 10000111
+unchecked_rotate_left(00001111, 7) = 10000111
+
+          rotate_left(00001111, 8) = <<exception>>
+unchecked_rotate_left(00001111, 8) = 00001111
+
+          rotate_left(00001111, 15) = <<exception>>
+unchecked_rotate_left(00001111, 15) = 10000111
+
+          rotate_left(00001111, 16) = <<exception>>
+unchecked_rotate_left(00001111, 16) = 00001111
+
+*** Test 'rotate_right' ***
+
+          rotate_right(00000001, 0) = 00000001
+unchecked_rotate_right(00000001, 0) = 00000001
+
+          rotate_right(00000001, 1) = 10000000
+unchecked_rotate_right(00000001, 1) = 10000000
+
+          rotate_right(00000001, 2) = 01000000
+unchecked_rotate_right(00000001, 2) = 01000000
+
+          rotate_right(00000001, 7) = 00000010
+unchecked_rotate_right(00000001, 7) = 00000010
+
+          rotate_right(00000001, 8) = <<exception>>
+unchecked_rotate_right(00000001, 8) = 00000001
+
+          rotate_right(00000001, 15) = <<exception>>
+unchecked_rotate_right(00000001, 15) = 00000010
+
+          rotate_right(00000001, 16) = <<exception>>
+unchecked_rotate_right(00000001, 16) = 00000001
+
+          rotate_right(00001111, 0) = 00001111
+unchecked_rotate_right(00001111, 0) = 00001111
+
+          rotate_right(00001111, 1) = 10000111
+unchecked_rotate_right(00001111, 1) = 10000111
+
+          rotate_right(00001111, 2) = 11000011
+unchecked_rotate_right(00001111, 2) = 11000011
+
+          rotate_right(00001111, 7) = 00011110
+unchecked_rotate_right(00001111, 7) = 00011110
+
+          rotate_right(00001111, 8) = <<exception>>
+unchecked_rotate_right(00001111, 8) = 00001111
+
+          rotate_right(00001111, 15) = <<exception>>
+unchecked_rotate_right(00001111, 15) = 00011110
+
+          rotate_right(00001111, 16) = <<exception>>
+unchecked_rotate_right(00001111, 16) = 00001111
+
diff --git a/tests/hard_coded/rotate_uint8.m b/tests/hard_coded/rotate_uint8.m
index e69de29..d4d795b 100644
--- a/tests/hard_coded/rotate_uint8.m
+++ b/tests/hard_coded/rotate_uint8.m
@@ -0,0 +1,124 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+
+:- module rotate_uint8.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+:- implementation.
+
+:- import_module exception.
+:- import_module list.
+:- import_module uint8.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    run_test(rotate_left, unchecked_rotate_left, "rotate_left", !IO),
+    run_test(rotate_right,unchecked_rotate_right,  "rotate_right", !IO).
+
+%---------------------------------------------------------------------------%
+
+:- pred run_test((func(uint8, uint) = uint8)::in,
+    (func(uint8, uint) = uint8)::in,
+    string::in, io::di, io::uo) is cc_multi.
+
+run_test(CheckedFunc, UncheckedFunc, Desc, !IO) :-
+    io.format("*** Test '%s' ***\n\n", [s(Desc)], !IO),
+    Ns = numbers,
+    Ds = distances,
+    list.foldl(run_test_2(CheckedFunc, UncheckedFunc, Desc, Ds), Ns, !IO).
+
+:- pred run_test_2((func(uint8, uint) = uint8)::in,
+    (func(uint8, uint) = uint8)::in, string::in,
+    list(uint)::in, uint8::in, io::di, io::uo) is cc_multi.
+
+run_test_2(CheckedFunc, UncheckedFunc, Desc, Ds, N, !IO) :-
+    list.foldl(run_test_3(CheckedFunc, UncheckedFunc, Desc, N), Ds, !IO).
+
+:- pred run_test_3((func(uint8, uint) = uint8)::in,
+    (func(uint8, uint) = uint8)::in, string::in, uint8::in,
+    uint::in, io::di, io::uo) is cc_multi.
+
+run_test_3(CheckedFunc, UncheckedFunc, Desc, N, D, !IO) :-
+    do_eval(CheckedFunc, N, D, CheckedResult),
+    io.format("          %s(%s, %u) = %s\n",
+        [s(Desc), s(to_binary_string_lz(N)), u(D), s(CheckedResult)], !IO),
+    do_eval(UncheckedFunc, N, D, UncheckedResult),
+    io.format("unchecked_%s(%s, %u) = %s\n",
+        [s(Desc), s(to_binary_string_lz(N)), u(D), s(UncheckedResult)], !IO),
+    io.nl(!IO).
+
+:- pred do_eval((func(uint8, uint) = uint8)::in, uint8::in, uint::in,
+    string::out) is cc_multi.
+
+do_eval(Func, N, D, ResultStr) :-
+    ( try []
+        Result0 = Func(N, D)
+    then
+        ResultStr = to_binary_string_lz(Result0)
+    catch_any _ ->
+        ResultStr = "<<exception>>"
+    ).
+
+%---------------------------------------------------------------------------%
+
+:- func numbers = list(uint8).
+
+numbers = [
+    1_u8,
+    15_u8
+].
+
+:- func distances = list(uint).
+
+distances = [
+    0_u,
+    1_u,
+    2_u,
+    7_u,
+    8_u,
+    15_u,
+    16_u
+].
+
+%---------------------------------------------------------------------------%
+
+:- 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;
+
+    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 rotate_uint8.
+%---------------------------------------------------------------------------%


More information about the reviews mailing list