2019-03-19 18:42:29 +00:00
|
|
|
// Ported from:
|
|
|
|
//
|
|
|
|
// https://github.com/llvm/llvm-project/blob/2ffb1b0413efa9a24eb3c49e710e36f92e2cb50b/compiler-rt/test/builtins/Unit/multf3_test.c
|
|
|
|
|
2022-04-19 04:22:43 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const math = std.math;
|
2023-07-24 11:34:16 +01:00
|
|
|
const qnan128: f128 = @bitCast(@as(u128, 0x7fff800000000000) << 64);
|
|
|
|
const inf128: f128 = @bitCast(@as(u128, 0x7fff000000000000) << 64);
|
2019-03-19 18:42:29 +00:00
|
|
|
|
2022-06-16 23:14:12 +01:00
|
|
|
const __multf3 = @import("multf3.zig").__multf3;
|
|
|
|
const __mulxf3 = @import("mulxf3.zig").__mulxf3;
|
|
|
|
const __muldf3 = @import("muldf3.zig").__muldf3;
|
|
|
|
const __mulsf3 = @import("mulsf3.zig").__mulsf3;
|
2019-03-19 18:42:29 +00:00
|
|
|
|
|
|
|
// return true if equal
|
|
|
|
// use two 64-bit integers intead of one 128-bit integer
|
|
|
|
// because 128-bit integer constant can't be assigned directly
|
|
|
|
fn compareResultLD(result: f128, expectedHi: u64, expectedLo: u64) bool {
|
2023-07-24 11:34:16 +01:00
|
|
|
const rep: u128 = @bitCast(result);
|
|
|
|
const hi: u64 = @intCast(rep >> 64);
|
|
|
|
const lo: u64 = @truncate(rep);
|
2019-03-19 18:42:29 +00:00
|
|
|
|
|
|
|
if (hi == expectedHi and lo == expectedLo) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// test other possible NaN representation(signal NaN)
|
|
|
|
if (expectedHi == 0x7fff800000000000 and expectedLo == 0x0) {
|
|
|
|
if ((hi & 0x7fff000000000000) == 0x7fff000000000000 and
|
|
|
|
((hi & 0xffffffffffff) > 0 or lo > 0))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-06 07:32:29 +01:00
|
|
|
fn test__multf3(a: f128, b: f128, expected_hi: u64, expected_lo: u64) !void {
|
2019-03-19 18:42:29 +00:00
|
|
|
const x = __multf3(a, b);
|
|
|
|
|
|
|
|
if (compareResultLD(x, expected_hi, expected_lo))
|
|
|
|
return;
|
|
|
|
|
|
|
|
@panic("__multf3 test failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn makeNaN128(rand: u64) f128 {
|
2019-11-07 04:25:57 +00:00
|
|
|
const int_result = @as(u128, 0x7fff000000000000 | (rand & 0xffffffffffff)) << 64;
|
2023-07-24 11:34:16 +01:00
|
|
|
return @bitCast(int_result);
|
2019-03-19 18:42:29 +00:00
|
|
|
}
|
|
|
|
test "multf3" {
|
|
|
|
// qNaN * any = qNaN
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(qnan128, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0);
|
2019-03-19 18:42:29 +00:00
|
|
|
|
|
|
|
// NaN * any = NaN
|
|
|
|
const a = makeNaN128(0x800030000000);
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(a, 0x1.23456789abcdefp+5, 0x7fff800000000000, 0x0);
|
2019-03-19 18:42:29 +00:00
|
|
|
// inf * any = inf
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(inf128, 0x1.23456789abcdefp+5, 0x7fff000000000000, 0x0);
|
2019-03-19 18:42:29 +00:00
|
|
|
|
|
|
|
// any * any
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(
|
2023-06-22 18:46:56 +01:00
|
|
|
@as(f128, @bitCast(@as(u128, 0x40042eab345678439abcdefea5678234))),
|
|
|
|
@as(f128, @bitCast(@as(u128, 0x3ffeedcb34a235253948765432134675))),
|
2019-03-19 18:42:29 +00:00
|
|
|
0x400423e7f9e3c9fc,
|
|
|
|
0xd906c2c2a85777c4,
|
|
|
|
);
|
|
|
|
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(
|
2023-06-22 18:46:56 +01:00
|
|
|
@as(f128, @bitCast(@as(u128, 0x3fcd353e45674d89abacc3a2ebf3ff50))),
|
|
|
|
@as(f128, @bitCast(@as(u128, 0x3ff6ed8764648369535adf4be3214568))),
|
2019-03-19 18:42:29 +00:00
|
|
|
0x3fc52a163c6223fc,
|
|
|
|
0xc94c4bf0430768b4,
|
|
|
|
);
|
|
|
|
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(
|
2019-03-19 18:42:29 +00:00
|
|
|
0x1.234425696abcad34a35eeffefdcbap+456,
|
|
|
|
0x451.ed98d76e5d46e5f24323dff21ffp+600,
|
|
|
|
0x44293a91de5e0e94,
|
|
|
|
0xe8ed17cc2cdf64ac,
|
|
|
|
);
|
|
|
|
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(
|
2023-06-22 18:46:56 +01:00
|
|
|
@as(f128, @bitCast(@as(u128, 0x3f154356473c82a9fabf2d22ace345df))),
|
|
|
|
@as(f128, @bitCast(@as(u128, 0x3e38eda98765476743ab21da23d45679))),
|
2019-03-19 18:42:29 +00:00
|
|
|
0x3d4f37c1a3137cae,
|
|
|
|
0xfc6807048bc2836a,
|
|
|
|
);
|
|
|
|
|
2021-05-06 07:32:29 +01:00
|
|
|
try test__multf3(0x1.23456734245345p-10000, 0x1.edcba524498724p-6497, 0x0, 0x0);
|
2021-05-14 11:17:58 +01:00
|
|
|
|
|
|
|
// Denormal operands.
|
|
|
|
try test__multf3(
|
|
|
|
0x0.0000000000000000000000000001p-16382,
|
2021-05-27 22:08:54 +01:00
|
|
|
0x1p16383,
|
2021-05-14 11:17:58 +01:00
|
|
|
0x3f90000000000000,
|
|
|
|
0x0,
|
|
|
|
);
|
|
|
|
try test__multf3(
|
2021-05-27 22:08:54 +01:00
|
|
|
0x1p16383,
|
2021-05-14 11:17:58 +01:00
|
|
|
0x0.0000000000000000000000000001p-16382,
|
|
|
|
0x3f90000000000000,
|
|
|
|
0x0,
|
|
|
|
);
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
try test__multf3(0x1.0000_0000_0000_0000_0000_0000_0001p+0, 0x1.8p+5, 0x4004_8000_0000_0000, 0x0000_0000_0000_0002);
|
|
|
|
try test__multf3(0x1.0000_0000_0000_0000_0000_0000_0002p+0, 0x1.8p+5, 0x4004_8000_0000_0000, 0x0000_0000_0000_0003);
|
2022-04-26 00:42:13 +01:00
|
|
|
try test__multf3(2.0, math.floatTrueMin(f128), 0x0000_0000_0000_0000, 0x0000_0000_0000_0002);
|
2022-04-19 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:34:16 +01:00
|
|
|
const qnan80: f80 = @bitCast(@as(u80, @bitCast(math.nan(f80))) | (1 << (math.floatFractionalBits(f80) - 1)));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
fn test__mulxf3(a: f80, b: f80, expected: u80) !void {
|
|
|
|
const x = __mulxf3(a, b);
|
2023-07-24 11:34:16 +01:00
|
|
|
const rep: u80 = @bitCast(x);
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
if (rep == expected)
|
|
|
|
return;
|
|
|
|
|
2023-06-22 18:46:56 +01:00
|
|
|
if (math.isNan(@as(f80, @bitCast(expected))) and math.isNan(x))
|
2022-04-19 04:22:43 +01:00
|
|
|
return; // We don't currently test NaN payload propagation
|
|
|
|
|
|
|
|
return error.TestFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "mulxf3" {
|
|
|
|
// NaN * any = NaN
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(qnan80, 0x1.23456789abcdefp+5, @as(u80, @bitCast(qnan80)));
|
|
|
|
try test__mulxf3(@as(f80, @bitCast(@as(u80, 0x7fff_8000_8000_3000_0000))), 0x1.23456789abcdefp+5, @as(u80, @bitCast(qnan80)));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// any * NaN = NaN
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(0x1.23456789abcdefp+5, qnan80, @as(u80, @bitCast(qnan80)));
|
|
|
|
try test__mulxf3(0x1.23456789abcdefp+5, @as(f80, @bitCast(@as(u80, 0x7fff_8000_8000_3000_0000))), @as(u80, @bitCast(qnan80)));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// NaN * inf = NaN
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(qnan80, math.inf(f80), @as(u80, @bitCast(qnan80)));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// inf * NaN = NaN
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(math.inf(f80), qnan80, @as(u80, @bitCast(qnan80)));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// inf * inf = inf
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(math.inf(f80), math.inf(f80), @as(u80, @bitCast(math.inf(f80))));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// inf * -inf = -inf
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(math.inf(f80), -math.inf(f80), @as(u80, @bitCast(-math.inf(f80))));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// -inf + inf = -inf
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(-math.inf(f80), math.inf(f80), @as(u80, @bitCast(-math.inf(f80))));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// inf * any = inf
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(math.inf(f80), 0x1.2335653452436234723489432abcdefp+5, @as(u80, @bitCast(math.inf(f80))));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// any * inf = inf
|
2023-06-22 18:46:56 +01:00
|
|
|
try test__mulxf3(0x1.2335653452436234723489432abcdefp+5, math.inf(f80), @as(u80, @bitCast(math.inf(f80))));
|
2022-04-19 04:22:43 +01:00
|
|
|
|
|
|
|
// any * any
|
|
|
|
try test__mulxf3(0x1.0p+0, 0x1.dcba987654321p+5, 0x4004_ee5d_4c3b_2a19_0800);
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0004p+0, 0x1.8p+5, 0x4004_C000_0000_0000_0003); // exact
|
|
|
|
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0002p+0, 0x1.0p+5, 0x4004_8000_0000_0000_0001); // exact
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0002p+0, 0x1.7ffep+5, 0x4004_BFFF_0000_0000_0001); // round down
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0002p+0, 0x1.8p+5, 0x4004_C000_0000_0000_0002); // round up to even
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0002p+0, 0x1.8002p+5, 0x4004_C001_0000_0000_0002); // round up
|
|
|
|
try test__mulxf3(0x1.0000_0000_0000_0002p+0, 0x1.0p+6, 0x4005_8000_0000_0000_0001); // exact
|
|
|
|
|
|
|
|
try test__mulxf3(0x1.0000_0001p+0, 0x1.0000_0001p+0, 0x3FFF_8000_0001_0000_0000); // round down to even
|
|
|
|
try test__mulxf3(0x1.0000_0001p+0, 0x1.0000_0001_0002p+0, 0x3FFF_8000_0001_0001_0001); // round up
|
2022-04-26 00:42:13 +01:00
|
|
|
try test__mulxf3(0x0.8000_0000_0000_0000p-16382, 2.0, 0x0001_8000_0000_0000_0000); // denormal -> normal
|
|
|
|
try test__mulxf3(0x0.7fff_ffff_ffff_fffep-16382, 0x2.0000_0000_0000_0008p0, 0x0001_8000_0000_0000_0000); // denormal -> normal
|
|
|
|
try test__mulxf3(0x0.7fff_ffff_ffff_fffep-16382, 0x1.0000_0000_0000_0000p0, 0x0000_3FFF_FFFF_FFFF_FFFF); // denormal -> denormal
|
2019-03-19 18:42:29 +00:00
|
|
|
}
|