:- module(buffer_tests, []).

:- use_module(library(array/buffer)).

% The float formats C has no type for, bfloat16 and float8_e4m3, held as their bits. The bits
% expected are those torch 2.7.1 gives, torch.bfloat16 and torch.float8_e4m3fn.

% bits(+Format, +Ints, +Values, -Bits): the elements of Values in Format, read as Ints.
bits(Format, Ints, Values, Bits) :-
   length(Values, N),
   compound_name_arguments(Data, data, Values),
   buffer_from_data(Buffer, Format, N, Data),
   buffer_to_data(Buffer, Ints, N, Data1),
   compound_name_arguments(Data1, _, Bits).

% values(+Format, +Ints, +Bits, -Values): the elements of Format whose bits are Bits.
values(Format, Ints, Bits, Values) :-
   length(Bits, N),
   compound_name_arguments(Data, data, Bits),
   buffer_from_data(Buffer, Ints, N, Data),
   buffer_to_data(Buffer, Format, N, Data1),
   compound_name_arguments(Data1, _, Values).

float(E, V) :- V is float(E).

:- begin_tests(buffer).

test(sizes, [timeout(10), true(Ss == [2, 1])]) :-
   maplist(buffer_type_size, [bfloat16, float8_e4m3], Ss).

test(bfloat16_bits, [timeout(10), true(Bits == [0, 0x8000, 0x3f80, 0x3dcd, 0x43e0, 0x447a, 0xc053])]) :-
   bits(bfloat16, uint16, [0.0, -0.0, 1.0, 0.1, 448.0, 1000.0, -3.3], Bits).

test(bfloat16_ties_to_even, [timeout(10), true(Bits == [0x3f80, 0x3f81, 0x3f82])]) :-
   % 1 + 2^-8 is half way to the next, 1 + 2^-7: the even one; a double just above the tie
   % is above it, which rounding it to a float32 first would lose
   maplist(float, [1 + 2** -8, 1 + 2** -8 + 2** -40, 1 + 3 * 2** -8], Vs),
   bits(bfloat16, uint16, Vs, Bits).

test(bfloat16_overflows_to_infinity, [timeout(10), true(Bits == [0x7f7f, 0x7f80, 0xff80, 0x7f80, 0xff80])]) :-
   maplist(float, [3.39e38, 3.4e38, -1.0e300, inf, -inf], Vs),
   bits(bfloat16, uint16, Vs, Bits).

test(bfloat16_subnormals, [timeout(10), true(Bits == [1, 0, 0])]) :-
   bits(bfloat16, uint16, [1.0e-40, 1.0e-45, 1.0e-320], Bits).

test(bfloat16_not_a_number, [timeout(10), true(V =\= V)]) :-
   X is nan,
   bits(bfloat16, uint16, [X], Bits),
   values(bfloat16, uint16, Bits, [V]).

test(bfloat16_values, [timeout(10), true(Vs == [1.0, 0.10009765625, -3.296875, 1000.0])]) :-
   values(bfloat16, uint16, [0x3f80, 0x3dcd, 0xc053, 0x447a], Vs).

test(float8_e4m3_bits, [timeout(10), true(Bits == [0, 0x80, 0x38, 0x1d, 0x7e, 0xc5])]) :-
   bits(float8_e4m3, uint8, [0.0, -0.0, 1.0, 0.1, 448.0, -3.3], Bits).

test(float8_e4m3_ties_to_even, [timeout(10), true(Bits == [0x38, 0x39, 0x3a, 0x7e, 0x7e])]) :-
   % 1.0625 is half way from 1 to 1.125, 1.1875 from 1.125 to 1.25, 432 from 416 to 448
   bits(float8_e4m3, uint8, [1.0625, 1.0626, 1.1875, 432.0, 440.0], Bits).

test(float8_e4m3_overflows_to_not_a_number, [timeout(10), true(Bits == [0x7e, 0x7e, 0x7e, 0x7f, 0x7f, 0xff, 0x7f, 0xff])]) :-
   % there is no infinity: up to half way past the greatest, 448, a value is 448, and more
   % is not a number
   maplist(float, [449.0, 463.9, 464.0, 464.0000000001, 1000.0, -480.0, inf, -inf], Vs),
   bits(float8_e4m3, uint8, Vs, Bits).

test(float8_e4m3_subnormals, [timeout(10), true(Bits == [1, 0, 0, 1, 2, 8, 4, 0])]) :-
   % the least is 2^-9: half of it is a tie, to 0
   maplist(float, [2** -9, 2** -10, 2** -11, 1.5 * 2** -10, 2.5 * 2** -9, 1.0625 * 2** -6, 1.1 * 2** -7, 1.0e-30], Vs),
   bits(float8_e4m3, uint8, Vs, Bits).

test(float8_e4m3_values, [timeout(10), true(Vs == [0.0, 0.001953125, 0.015625, 1.0, 1.125, 448.0, -0.0, -3.25])]) :-
   values(float8_e4m3, uint8, [0, 1, 8, 0x38, 0x39, 0x7e, 0x80, 0xc5], Vs).

test(float8_e4m3_not_a_number, [timeout(10), true(( V1 =\= V1, V2 =\= V2, Bits == [0x7f] ))]) :-
   values(float8_e4m3, uint8, [0x7f, 0xff], [V1, V2]),
   X is nan,
   bits(float8_e4m3, uint8, [X], Bits).

test(float8_e4m3_every_element, [timeout(10), true(Bits == Is)]) :-
   % every element but the two that are not a number is its own nearest, and they ascend
   numlist(0, 126, Is),
   values(float8_e4m3, uint8, Is, Vs),
   bits(float8_e4m3, uint8, Vs, Bits),
   msort(Vs, Vs).

test(convert_to_float32, [timeout(10), true(Vs == [1.0, 0.10009765625, 448.0])]) :-
   bits(bfloat16, uint16, [1.0, 0.1, 448.0], Bits),
   compound_name_arguments(Data, data, Bits),
   buffer_from_data(B, uint16, 3, Data),
   buffer_convert(B, bfloat16, 3, float32, F),
   buffer_to_data(F, float32, 3, Data1),
   compound_name_arguments(Data1, _, Vs).

:- end_tests(buffer).
