authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-08 22:31:07+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-08 23:57:09+02:00
log92fd2df054d1e1523489c6a959354f5e75f647a4
tree0c43f6799cd4276ccfc65b87509b0918eec9e627
parentc00167e91a452bb3c051c66157686e11487df94b

compiler-rt: Add __truncdfsf2

Add AEABI builtin __aeabi_d2f

3 files changed, 45 insertions(+), 0 deletions(-)

std/special/compiler_rt.zig+4
......@@ -86,6 +86,8 @@ comptime {
8686 @export("__trunctfdf2", @import("compiler_rt/truncXfYf2.zig").__trunctfdf2, linkage);
8787 @export("__trunctfsf2", @import("compiler_rt/truncXfYf2.zig").__trunctfsf2, linkage);
8888
89 @export("__truncdfsf2", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage);
90
8991 @export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage);
9092 @export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage);
9193 @export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage);
......@@ -176,6 +178,8 @@ comptime {
176178 @export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage);
177179
178180 @export("__aeabi_i2f", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage);
181 @export("__aeabi_d2f", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage);
182
179183 @export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage);
180184 @export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage);
181185 @export("__aeabi_fsub", @import("compiler_rt/addXf3.zig").__subsf3, linkage);
std/special/compiler_rt/truncXfYf2.zig+4
......@@ -16,6 +16,10 @@ pub extern fn __trunctfdf2(a: f128) f64 {
1616 return truncXfYf2(f64, f128, a);
1717}
1818
19pub extern fn __truncdfsf2(a: f64) f32 {
20 return truncXfYf2(f32, f64, a);
21}
22
1923inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t {
2024 const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits);
2125 const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits);
std/special/compiler_rt/truncXfYf2_test.zig+37
......@@ -200,3 +200,40 @@ test "trunctfdf2" {
200200 test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000);
201201 test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab);
202202}
203
204const __truncdfsf2 = @import("truncXfYf2.zig").__truncdfsf2;
205
206fn test__truncdfsf2(a: f64, expected: u32) void {
207 const x = __truncdfsf2(a);
208
209 const rep = @bitCast(u32, x);
210 if (rep == expected) {
211 return;
212 }
213 // test other possible NaN representation(signal NaN)
214 else if (expected == 0x7fc00000) {
215 if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
216 return;
217 }
218 }
219
220 @import("std").debug.warn("got 0x{x} wanted 0x{x}\n", rep, expected);
221
222 @panic("__trunctfsf2 test failure");
223}
224
225test "truncdfsf2" {
226 // nan & qnan
227 test__truncdfsf2(@bitCast(f64, u64(0x7ff8000000000000)), 0x7fc00000);
228 test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000001)), 0x7fc00000);
229 // inf
230 test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000000)), 0x7f800000);
231 test__truncdfsf2(@bitCast(f64, u64(0xfff0000000000000)), 0xff800000);
232
233 test__truncdfsf2(0.0, 0x0);
234 test__truncdfsf2(1.0, 0x3f800000);
235 test__truncdfsf2(-1.0, 0xbf800000);
236
237 // huge number becomes inf
238 test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);
239}