authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-15 18:21:15+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-15 18:23:09+02:00
log5bc1dc59e693287e1ffcd14023048b2abc7e1764
tree142935d2b5e466903a747e9800350e17ae8558ec
parentb29677dd12b5a19618295757a6044c0b0d741dbb

compiler-rt: Implement __trunctfhf2


2 files changed, 60 insertions(+), 0 deletions(-)

lib/std/special/compiler_rt/truncXfYf2.zig+4
...@@ -13,6 +13,10 @@ pub fn __truncdfhf2(a: f64) callconv(.C) u16 {...@@ -13,6 +13,10 @@ pub fn __truncdfhf2(a: f64) callconv(.C) u16 {
13 return @bitCast(u16, @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f16, f64, a }));13 return @bitCast(u16, @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f16, f64, a }));
14}14}
1515
16pub fn __trunctfhf2(a: f128) callconv(.C) u16 {
17 return @bitCast(u16, @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f16, f128, a }));
18}
19
16pub fn __trunctfsf2(a: f128) callconv(.C) f32 {20pub fn __trunctfsf2(a: f128) callconv(.C) f32 {
17 return @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f32, f128, a });21 return @call(.{ .modifier = .always_inline }, truncXfYf2, .{ f32, f128, a });
18}22}
lib/std/special/compiler_rt/truncXfYf2_test.zig+56
...@@ -242,3 +242,59 @@ test "truncdfsf2" {...@@ -242,3 +242,59 @@ test "truncdfsf2" {
242 // huge number becomes inf242 // huge number becomes inf
243 test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);243 test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);
244}244}
245
246const __trunctfhf2 = @import("truncXfYf2.zig").__trunctfhf2;
247
248fn test__trunctfhf2(a: f128, expected: u16) void {
249 const x = __trunctfhf2(a);
250
251 const rep = @bitCast(u16, x);
252 if (rep == expected) {
253 return;
254 }
255
256 @import("std").debug.warn("got 0x{x} wanted 0x{x}\n", .{ rep, expected });
257
258 @panic("__trunctfhf2 test failure");
259}
260
261test "trunctfhf2" {
262 // qNaN
263 test__trunctfhf2(@bitCast(f128, @as(u128, 0x7fff8000000000000000000000000000)), 0x7e00);
264 // NaN
265 test__trunctfhf2(@bitCast(f128, @as(u128, 0x7fff0000000000000000000000000001)), 0x7e00);
266 // inf
267 test__trunctfhf2(@bitCast(f128, @as(u128, 0x7fff0000000000000000000000000000)), 0x7c00);
268 test__trunctfhf2(-@bitCast(f128, @as(u128, 0x7fff0000000000000000000000000000)), 0xfc00);
269 // zero
270 test__trunctfhf2(0.0, 0x0);
271 test__trunctfhf2(-0.0, 0x8000);
272
273 test__trunctfhf2(3.1415926535, 0x4248);
274 test__trunctfhf2(-3.1415926535, 0xc248);
275 test__trunctfhf2(0x1.987124876876324p+100, 0x7c00);
276 test__trunctfhf2(0x1.987124876876324p+12, 0x6e62);
277 test__trunctfhf2(0x1.0p+0, 0x3c00);
278 test__trunctfhf2(0x1.0p-14, 0x0400);
279 // denormal
280 test__trunctfhf2(0x1.0p-20, 0x0010);
281 test__trunctfhf2(0x1.0p-24, 0x0001);
282 test__trunctfhf2(-0x1.0p-24, 0x8001);
283 test__trunctfhf2(0x1.5p-25, 0x0001);
284 // and back to zero
285 test__trunctfhf2(0x1.0p-25, 0x0000);
286 test__trunctfhf2(-0x1.0p-25, 0x8000);
287 // max (precise)
288 test__trunctfhf2(65504.0, 0x7bff);
289 // max (rounded)
290 test__trunctfhf2(65519.0, 0x7bff);
291 // max (to +inf)
292 test__trunctfhf2(65520.0, 0x7c00);
293 test__trunctfhf2(65536.0, 0x7c00);
294 test__trunctfhf2(-65520.0, 0xfc00);
295
296 test__trunctfhf2(0x1.23a2abb4a2ddee355f36789abcdep+5, 0x508f);
297 test__trunctfhf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9, 0x1b8f);
298 test__trunctfhf2(0x1.234eebb5faa678f4488693abcdefp+453, 0x7c00);
299 test__trunctfhf2(0x1.edcba9bb8c76a5a43dd21f334634p-43, 0x0);
300}