Files
mercury/tests/hard_coded/bitwise_int64.m
Julien Fischer f80463dbcb Add builtin 64-bit integer types -- Part 2.
Replace placeholder types with int64 and uint64 as appropriate throughout the
system.

Enable support for 64-bit integer literals in the compiler.

Add initial library support for 64-bit integers.

configure.ac:
     Check that the bootstrap compiler recognises int64 and uint64 as
     builtins.

library/int64.m:
library/uint64.m:
     Populate these two modules to the extent that we can now run
     basic tests of 64-bit integer support.

     Note that since the bootstrap compiler will not recognise
     64-bit integer literals, any such literals are current written
     as conversions from ints; this will be replaced once this change
     has bootstrapped.

library/private_builtin.m:
    Replace the placeholder definitions for builtin unification and
    comparison of 64-bit integers with their actual definitions.

library/integer.m:
    Add procedures for converting integers to- and from int64 and uint64.

library/string.m:
    Add functions for converting 64-bit integers into strings.

library/io.m:
    Add predicates for writing 64-bit integers to text streams.
    (Support for 64-bit integers with binary streams will be done
    separately.)

library/stream.string_writer.m:
    Add put_int64/4 and put_uint/64.

    Extend the implementations of print and write to cover int64 and
    uint64.

library/pprint.m:
    Make int64 and uint64 instances of the doc/1 type class.

library/erlang_rtti_implementation.m:
library/rtti_implementation.m:
    Handle int64 and uint64 properly in deconstruct.

library/term.m:
    Add functions for converting 64-bit integers into terms.

library/term_conversion.m:
    Support int64 and uint64 in univ -> term conversion.

library/Mercury.options:
    Avoid a warning about the import of the require being
    unused in the int64 and uint64 modules.  It *is* used,
    but only in the definitions used by the Erlang backend.

compiler/superhomogeneous.m:
     Accept 64-bit integer literals.

compiler/c_util.m:
     In C, write out the value of the min_int64 as the symbolic
     constant INT64_MIN.  This expands in such a way as to avoid
     generating warnings from the C compiler.

compiler/builtin_ops.m:
compiler/bytecode.m:
compiler/elds.m:
compiler/elds_to_erlang.m:
compiler/hlds_data.m:
compiler/hlds_out_util.m:
compiler/llds.m:
compiler/llds_out_data.m:
compiler/lookup_switch.m:
compiler/mercury_to_mercury.m:
compiler/mlds.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_java.m:
compiler/opt_debug.m:
compiler/parse_tree_out_info.m:
compiler/parse_tree_to_term.m:
compiler/prog_data.m:
compiler/prog_out.m:
compiler/prog_rep.m:
     Replace the use of int as a placeholder with int64 or uint64 as
     appropriate.

tests/hard_coded/Mmakefile:
tests/hard_coded/arith_int64.{m,exp}:
tests/hard_coded/arith_uint64.{m,exp}:
tests/hard_coded/bitwise_int64.{m,exp}:
tests/hard_coded/bitwise_uint64.{m,exp}:
tests/hard_coded/cmp_int64.{m,exp}:
tests/hard_coded/cmp_uint64.{m,exp}:
tests/hard_coded/integer_int64_conv.{m,exp}:
tests/hard_coded/integer_uint64_conv.{m,exp}:
     Add tests of basic operations on 64-bit integers.

tests/hard_coded/construct_test.{m,exp}:
    Extend this test to cover 64-bit integers.
2018-02-02 10:33:25 -05:00

208 lines
5.7 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
% Test bitwise operations for signed 64-bit integers.
:- module bitwise_int64.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is cc_multi.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int64.
:- import_module exception.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
run_unop_test(int64.(\), "\\", !IO),
io.nl(!IO),
run_binop_test(int64.(/\), "/\\", !IO),
io.nl(!IO),
run_binop_test(int64.(\/), "\\/", !IO),
io.nl(!IO),
run_binop_test((func(X, Y) = int64.xor(X, Y)), "xor", !IO),
io.nl(!IO),
run_shift_test(int64.(>>), ">>", !IO),
io.nl(!IO),
run_shift_test(int64.(<<), "<<", !IO).
%---------------------------------------------------------------------------%
:- pred run_unop_test((func(int64) = int64)::in, string::in,
io::di, io::uo) is cc_multi.
run_unop_test(UnOpFunc, Desc, !IO) :-
io.format("*** Test unary operation '%s' ***\n\n", [s(Desc)], !IO),
As = numbers,
list.foldl(run_unop_test_2(UnOpFunc, Desc), As, !IO).
:- pred run_unop_test_2((func(int64) = int64)::in, string::in,
int64::in, io::di, io::uo) is cc_multi.
run_unop_test_2(UnOpFunc, Desc, A, !IO) :-
( try []
Result0 = UnOpFunc(A)
then
ResultStr = to_binary_string_lz(Result0)
catch_any _ ->
ResultStr = "<<exception>>"
),
io.format("%s %s =\n %s\n",
[s(Desc), s(to_binary_string_lz(A)), s(ResultStr)], !IO),
io.nl(!IO).
%---------------------------------------------------------------------------%
:- pred run_binop_test((func(int64, int64) = int64)::in, string::in,
io::di, io::uo) is cc_multi.
run_binop_test(BinOpFunc, Desc, !IO) :-
io.format("*** Test binary operation '%s' ***\n\n", [s(Desc)], !IO),
As = numbers,
Bs = numbers,
list.foldl(run_binop_test_2(BinOpFunc, Desc, Bs), As, !IO).
:- pred run_binop_test_2((func(int64, int64) = int64)::in, string::in,
list(int64)::in, int64::in, io::di, io::uo) is cc_multi.
run_binop_test_2(BinOpFunc, Desc, Bs, A, !IO) :-
list.foldl(run_binop_test_3(BinOpFunc, Desc, A), Bs, !IO).
:- pred run_binop_test_3((func(int64, int64) = int64)::in, string::in,
int64::in, int64::in, io::di, io::uo) is cc_multi.
run_binop_test_3(BinOpFunc, Desc, A, B, !IO) :-
( try []
Result0 = BinOpFunc(A, B)
then
ResultStr = to_binary_string_lz(Result0)
catch_any _ ->
ResultStr = "<<exception>>"
),
io.format("%s %s\n%s =\n%s\n",
[s(to_binary_string_lz(A)), s(Desc),
s(to_binary_string_lz(B)), s(ResultStr)], !IO),
io.nl(!IO).
%---------------------------------------------------------------------------%
:- pred run_shift_test((func(int64, int) = int64)::in, string::in,
io::di, io::uo) is cc_multi.
run_shift_test(ShiftOpFunc, Desc, !IO) :-
io.format("*** Test binary operation '%s' ***\n\n", [s(Desc)], !IO),
As = numbers,
Bs = shift_amounts,
list.foldl(run_shift_test_2(ShiftOpFunc, Desc, Bs), As, !IO).
:- pred run_shift_test_2((func(int64, int) = int64)::in, string::in,
list(int)::in, int64::in, io::di, io::uo) is cc_multi.
run_shift_test_2(ShiftOpFunc, Desc, Bs, A, !IO) :-
list.foldl(run_shift_test_3(ShiftOpFunc, Desc, A), Bs, !IO).
:- pred run_shift_test_3((func(int64, int) = int64)::in, string::in,
int64::in, int::in, io::di, io::uo) is cc_multi.
run_shift_test_3(ShiftOpFunc, Desc, A, B, !IO) :-
( try []
Result0 = ShiftOpFunc(A, B)
then
ResultStr = to_binary_string_lz(Result0)
catch_any _ ->
ResultStr = "<<exception>>"
),
io.format("%s %s %d =\n%s\n",
[s(to_binary_string_lz(A)), s(Desc), i(B), s(ResultStr)], !IO),
io.nl(!IO).
%---------------------------------------------------------------------------%
:- func numbers = list(int64).
numbers = [
-9_223_372_036_854_775_808_i64,
-2_147_483_648_i64,
-32_768_i64,
-128_i64,
0_i64,
1_i64,
2_i64,
8_i64,
10_i64,
16_i64,
16_i64,
127_i64,
32_767_i64,
2_147_483_647_i64,
9_223_372_036_854_775_807_i64
].
:- func shift_amounts = list(int).
shift_amounts = [
-1,
0,
1,
2,
3,
4,
8,
16,
24,
31,
32,
63,
64
].
%---------------------------------------------------------------------------%
:- func to_binary_string_lz(int64::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 = 64;
uint64_t U = I;
MR_allocate_aligned_string_msg(S, 64, MR_ALLOC_ID);
S[64] = '\\0';
while (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(64, '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(""%64s"",
java.lang.Long.toBinaryString(U)).replace(' ', '0');
").
%---------------------------------------------------------------------------%
:- end_module bitwise_int64.
%---------------------------------------------------------------------------%