From b29677dd12b5a19618295757a6044c0b0d741dbb Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Thu, 15 Apr 2021 18:20:50 +0200 Subject: [PATCH] compiler-rt: Implement __extendhftf2 --- lib/std/special/compiler_rt/extendXfYf2.zig | 4 ++ .../special/compiler_rt/extendXfYf2_test.zig | 49 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/std/special/compiler_rt/extendXfYf2.zig b/lib/std/special/compiler_rt/extendXfYf2.zig index c5b93fa51e..59be8441fa 100644 --- a/lib/std/special/compiler_rt/extendXfYf2.zig +++ b/lib/std/special/compiler_rt/extendXfYf2.zig @@ -23,6 +23,10 @@ pub fn __extendhfsf2(a: u16) callconv(.C) f32 { return @call(.{ .modifier = .always_inline }, extendXfYf2, .{ f32, f16, a }); } +pub fn __extendhftf2(a: u16) callconv(.C) f128 { + return @call(.{ .modifier = .always_inline }, extendXfYf2, .{ f128, f16, a }); +} + pub fn __aeabi_h2f(arg: u16) callconv(.AAPCS) f32 { @setRuntimeSafety(false); return @call(.{ .modifier = .always_inline }, __extendhfsf2, .{arg}); diff --git a/lib/std/special/compiler_rt/extendXfYf2_test.zig b/lib/std/special/compiler_rt/extendXfYf2_test.zig index 6a3f69d8e9..d97a9c2502 100644 --- a/lib/std/special/compiler_rt/extendXfYf2_test.zig +++ b/lib/std/special/compiler_rt/extendXfYf2_test.zig @@ -4,9 +4,10 @@ // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. const builtin = @import("builtin"); -const __extenddftf2 = @import("extendXfYf2.zig").__extenddftf2; const __extendhfsf2 = @import("extendXfYf2.zig").__extendhfsf2; +const __extendhftf2 = @import("extendXfYf2.zig").__extendhftf2; const __extendsftf2 = @import("extendXfYf2.zig").__extendsftf2; +const __extenddftf2 = @import("extendXfYf2.zig").__extenddftf2; fn test__extenddftf2(a: f64, expectedHi: u64, expectedLo: u64) void { const x = __extenddftf2(a); @@ -161,3 +162,49 @@ fn makeNaN32(rand: u32) f32 { fn makeInf32() f32 { return @bitCast(f32, @as(u32, 0x7f800000)); } + +fn test__extendhftf2(a: f16, expectedHi: u64, expectedLo: u64) void { + const x = __extendhftf2(@bitCast(u16, a)); + + const rep = @bitCast(u128, x); + const hi = @intCast(u64, rep >> 64); + const lo = @truncate(u64, rep); + + if (hi == expectedHi and lo == expectedLo) + return; + + // 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; + } + } + + @panic("__extendhftf2 test failure"); +} + +test "extendhftf2" { + // qNaN + test__extendhftf2(@bitCast(f16, @as(u16, 0x7e00)), 0x7fff800000000000, 0x0); + // NaN + test__extendhftf2(@bitCast(f16, @as(u16, 0x7d00)), 0x7fff400000000000, 0x0); + // inf + test__extendhftf2(@bitCast(f16, @as(u16, 0x7c00)), 0x7fff000000000000, 0x0); + test__extendhftf2(-@bitCast(f16, @as(u16, 0x7c00)), 0xffff000000000000, 0x0); + // zero + test__extendhftf2(@bitCast(f16, @as(u16, 0x0)), 0x0, 0x0); + test__extendhftf2(@bitCast(f16, @as(u16, 0x8000)), 0x8000000000000000, 0x0); + // denormal + test__extendhftf2(@bitCast(f16, @as(u16, 0x0010)), 0x3feb000000000000, 0x0000000000000000); + test__extendhftf2(@bitCast(f16, @as(u16, 0x0001)), 0x3fe7000000000000, 0x0000000000000000); + test__extendhftf2(@bitCast(f16, @as(u16, 0x8001)), 0xbfe7000000000000, 0x0000000000000000); + + // pi + test__extendhftf2(@bitCast(f16, @as(u16, 0x4248)), 0x4000920000000000, 0x0000000000000000); + test__extendhftf2(@bitCast(f16, @as(u16, 0xc248)), 0xc000920000000000, 0x0000000000000000); + + test__extendhftf2(@bitCast(f16, @as(u16, 0x508c)), 0x4004230000000000, 0x0); + test__extendhftf2(@bitCast(f16, @as(u16, 0x1bb7)), 0x3ff6edc000000000, 0x0); +}