| author | |
| committer | |
| log | 4b9e12be50a8d075d06b127712573b531d068837 |
| tree | 5c63091849d556c7777ab05e1466ed3626c759bd |
| parent | 46e1c34fcfe0973735c75a0c210db314030798f0 |
| parent | 4208fa9ad9102471060c1909effaa6a9315155af |
| signature |
More more builtins11 files changed, 419 insertions(+), 28 deletions(-)
CMakeLists.txt+4| ... | @@ -677,9 +677,13 @@ set(ZIG_STD_FILES | ... | @@ -677,9 +677,13 @@ set(ZIG_STD_FILES |
| 677 | "special/compiler_rt/fixunstfdi.zig" | 677 | "special/compiler_rt/fixunstfdi.zig" |
| 678 | "special/compiler_rt/fixunstfsi.zig" | 678 | "special/compiler_rt/fixunstfsi.zig" |
| 679 | "special/compiler_rt/fixunstfti.zig" | 679 | "special/compiler_rt/fixunstfti.zig" |
| 680 | "special/compiler_rt/floatdidf.zig" | ||
| 681 | "special/compiler_rt/floatsiXf.zig" | ||
| 682 | "special/compiler_rt/floatunsidf.zig" | ||
| 680 | "special/compiler_rt/floattidf.zig" | 683 | "special/compiler_rt/floattidf.zig" |
| 681 | "special/compiler_rt/floattisf.zig" | 684 | "special/compiler_rt/floattisf.zig" |
| 682 | "special/compiler_rt/floattitf.zig" | 685 | "special/compiler_rt/floattitf.zig" |
| 686 | "special/compiler_rt/floatundidf.zig" | ||
| 683 | "special/compiler_rt/floatunditf.zig" | 687 | "special/compiler_rt/floatunditf.zig" |
| 684 | "special/compiler_rt/floatunsitf.zig" | 688 | "special/compiler_rt/floatunsitf.zig" |
| 685 | "special/compiler_rt/floatuntidf.zig" | 689 | "special/compiler_rt/floatuntidf.zig" |
std/special/compiler_rt.zig+73-24| ... | @@ -62,6 +62,13 @@ comptime { | ... | @@ -62,6 +62,13 @@ comptime { |
| 62 | @export("__divsf3", @import("compiler_rt/divsf3.zig").__divsf3, linkage); | 62 | @export("__divsf3", @import("compiler_rt/divsf3.zig").__divsf3, linkage); |
| 63 | @export("__divdf3", @import("compiler_rt/divdf3.zig").__divdf3, linkage); | 63 | @export("__divdf3", @import("compiler_rt/divdf3.zig").__divdf3, linkage); |
| 64 | 64 | ||
| 65 | @export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); | ||
| 66 | @export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); | ||
| 67 | @export("__floatdidf", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); | ||
| 68 | @export("__floatsitf", @import("compiler_rt/floatsiXf.zig").__floatsitf, linkage); | ||
| 69 | @export("__floatunsidf", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); | ||
| 70 | @export("__floatundidf", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); | ||
| 71 | |||
| 65 | @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage); | 72 | @export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage); |
| 66 | @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage); | 73 | @export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage); |
| 67 | @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage); | 74 | @export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage); |
| ... | @@ -82,6 +89,10 @@ comptime { | ... | @@ -82,6 +89,10 @@ comptime { |
| 82 | @export("__trunctfdf2", @import("compiler_rt/truncXfYf2.zig").__trunctfdf2, linkage); | 89 | @export("__trunctfdf2", @import("compiler_rt/truncXfYf2.zig").__trunctfdf2, linkage); |
| 83 | @export("__trunctfsf2", @import("compiler_rt/truncXfYf2.zig").__trunctfsf2, linkage); | 90 | @export("__trunctfsf2", @import("compiler_rt/truncXfYf2.zig").__trunctfsf2, linkage); |
| 84 | 91 | ||
| 92 | @export("__truncdfsf2", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage); | ||
| 93 | |||
| 94 | @export("__extendsfdf2", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage); | ||
| 95 | |||
| 85 | @export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage); | 96 | @export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage); |
| 86 | @export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage); | 97 | @export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage); |
| 87 | @export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage); | 98 | @export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage); |
| ... | @@ -107,6 +118,7 @@ comptime { | ... | @@ -107,6 +118,7 @@ comptime { |
| 107 | @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage); | 118 | @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage); |
| 108 | @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage); | 119 | @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage); |
| 109 | 120 | ||
| 121 | @export("__divmoddi4", __divmoddi4, linkage); | ||
| 110 | @export("__divsi3", __divsi3, linkage); | 122 | @export("__divsi3", __divsi3, linkage); |
| 111 | @export("__divdi3", __divdi3, linkage); | 123 | @export("__divdi3", __divdi3, linkage); |
| 112 | @export("__udivsi3", __udivsi3, linkage); | 124 | @export("__udivsi3", __udivsi3, linkage); |
| ... | @@ -122,10 +134,15 @@ comptime { | ... | @@ -122,10 +134,15 @@ comptime { |
| 122 | @export("__negdf2", @import("compiler_rt/negXf2.zig").__negdf2, linkage); | 134 | @export("__negdf2", @import("compiler_rt/negXf2.zig").__negdf2, linkage); |
| 123 | 135 | ||
| 124 | if (is_arm_arch and !is_arm_64) { | 136 | if (is_arm_arch and !is_arm_64) { |
| 137 | @export("__aeabi_unwind_cpp_pr0", __aeabi_unwind_cpp_pr0, strong_linkage); | ||
| 138 | @export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage); | ||
| 139 | @export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage); | ||
| 140 | |||
| 141 | @export("__aeabi_ldivmod", __aeabi_ldivmod, linkage); | ||
| 125 | @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage); | 142 | @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage); |
| 126 | 143 | ||
| 127 | @export("__aeabi_idiv", __divsi3, linkage); | 144 | @export("__aeabi_idiv", __divsi3, linkage); |
| 128 | @export("__aeabi_idivmod", __divmodsi4, linkage); | 145 | @export("__aeabi_idivmod", __aeabi_idivmod, linkage); |
| 129 | @export("__aeabi_uidiv", __udivsi3, linkage); | 146 | @export("__aeabi_uidiv", __udivsi3, linkage); |
| 130 | @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage); | 147 | @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage); |
| 131 | 148 | ||
| ... | @@ -149,6 +166,12 @@ comptime { | ... | @@ -149,6 +166,12 @@ comptime { |
| 149 | @export("__aeabi_memcmp4", __aeabi_memcmp, linkage); | 166 | @export("__aeabi_memcmp4", __aeabi_memcmp, linkage); |
| 150 | @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); | 167 | @export("__aeabi_memcmp8", __aeabi_memcmp, linkage); |
| 151 | 168 | ||
| 169 | @export("__aeabi_f2d", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage); | ||
| 170 | @export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage); | ||
| 171 | @export("__aeabi_l2d", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage); | ||
| 172 | @export("__aeabi_ui2d", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage); | ||
| 173 | @export("__aeabi_ul2d", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage); | ||
| 174 | |||
| 152 | @export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage); | 175 | @export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage); |
| 153 | @export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage); | 176 | @export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage); |
| 154 | 177 | ||
| ... | @@ -168,6 +191,9 @@ comptime { | ... | @@ -168,6 +191,9 @@ comptime { |
| 168 | @export("__aeabi_h2f", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage); | 191 | @export("__aeabi_h2f", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage); |
| 169 | @export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); | 192 | @export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); |
| 170 | 193 | ||
| 194 | @export("__aeabi_i2f", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage); | ||
| 195 | @export("__aeabi_d2f", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage); | ||
| 196 | |||
| 171 | @export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage); | 197 | @export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage); |
| 172 | @export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage); | 198 | @export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage); |
| 173 | @export("__aeabi_fsub", @import("compiler_rt/addXf3.zig").__subsf3, linkage); | 199 | @export("__aeabi_fsub", @import("compiler_rt/addXf3.zig").__subsf3, linkage); |
| ... | @@ -250,6 +276,24 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn | ... | @@ -250,6 +276,24 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn |
| 250 | } | 276 | } |
| 251 | } | 277 | } |
| 252 | 278 | ||
| 279 | extern fn __aeabi_unwind_cpp_pr0() void { | ||
| 280 | unreachable; | ||
| 281 | } | ||
| 282 | extern fn __aeabi_unwind_cpp_pr1() void { | ||
| 283 | unreachable; | ||
| 284 | } | ||
| 285 | extern fn __aeabi_unwind_cpp_pr2() void { | ||
| 286 | unreachable; | ||
| 287 | } | ||
| 288 | |||
| 289 | extern fn __divmoddi4(a: i64, b: i64, rem: *i64) i64 { | ||
| 290 | @setRuntimeSafety(is_test); | ||
| 291 | |||
| 292 | const d = __divdi3(a, b); | ||
| 293 | rem.* = a -% (d *% b); | ||
| 294 | return d; | ||
| 295 | } | ||
| 296 | |||
| 253 | extern fn __divdi3(a: i64, b: i64) i64 { | 297 | extern fn __divdi3(a: i64, b: i64) i64 { |
| 254 | @setRuntimeSafety(is_test); | 298 | @setRuntimeSafety(is_test); |
| 255 | 299 | ||
| ... | @@ -290,14 +334,35 @@ extern fn __umoddi3(a: u64, b: u64) u64 { | ... | @@ -290,14 +334,35 @@ extern fn __umoddi3(a: u64, b: u64) u64 { |
| 290 | return r; | 334 | return r; |
| 291 | } | 335 | } |
| 292 | 336 | ||
| 293 | const AeabiUlDivModResult = extern struct { | 337 | extern fn __aeabi_uidivmod(n: u32, d: u32) extern struct{q: u32, r: u32} { |
| 294 | quot: u64, | ||
| 295 | rem: u64, | ||
| 296 | }; | ||
| 297 | extern fn __aeabi_uldivmod(numerator: u64, denominator: u64) AeabiUlDivModResult { | ||
| 298 | @setRuntimeSafety(is_test); | 338 | @setRuntimeSafety(is_test); |
| 299 | var result: AeabiUlDivModResult = undefined; | 339 | |
| 300 | result.quot = __udivmoddi4(numerator, denominator, &result.rem); | 340 | var result: @typeOf(__aeabi_uidivmod).ReturnType = undefined; |
| 341 | result.q = __udivmodsi4(n, d, &result.r); | ||
| 342 | return result; | ||
| 343 | } | ||
| 344 | |||
| 345 | extern fn __aeabi_uldivmod(n: u64, d: u64) extern struct{q: u64, r: u64} { | ||
| 346 | @setRuntimeSafety(is_test); | ||
| 347 | |||
| 348 | var result: @typeOf(__aeabi_uldivmod).ReturnType = undefined; | ||
| 349 | result.q = __udivmoddi4(n, d, &result.r); | ||
| 350 | return result; | ||
| 351 | } | ||
| 352 | |||
| 353 | extern fn __aeabi_idivmod(n: i32, d: i32) extern struct{q: i32, r: i32} { | ||
| 354 | @setRuntimeSafety(is_test); | ||
| 355 | |||
| 356 | var result: @typeOf(__aeabi_idivmod).ReturnType = undefined; | ||
| 357 | result.q = __divmodsi4(n, d, &result.r); | ||
| 358 | return result; | ||
| 359 | } | ||
| 360 | |||
| 361 | extern fn __aeabi_ldivmod(n: i64, d: i64) extern struct{q: i64, r:i64} { | ||
| 362 | @setRuntimeSafety(is_test); | ||
| 363 | |||
| 364 | var result: @typeOf(__aeabi_ldivmod).ReturnType = undefined; | ||
| 365 | result.q = __divmoddi4(n, d, &result.r); | ||
| 301 | return result; | 366 | return result; |
| 302 | } | 367 | } |
| 303 | 368 | ||
| ... | @@ -391,22 +456,6 @@ test "usesThumb1" { | ... | @@ -391,22 +456,6 @@ test "usesThumb1" { |
| 391 | //etc. | 456 | //etc. |
| 392 | } | 457 | } |
| 393 | 458 | ||
| 394 | nakedcc fn __aeabi_uidivmod() void { | ||
| 395 | @setRuntimeSafety(false); | ||
| 396 | asm volatile ( | ||
| 397 | \\ push { lr } | ||
| 398 | \\ sub sp, sp, #4 | ||
| 399 | \\ mov r2, sp | ||
| 400 | \\ bl __udivmodsi4 | ||
| 401 | \\ ldr r1, [sp] | ||
| 402 | \\ add sp, sp, #4 | ||
| 403 | \\ pop { pc } | ||
| 404 | : | ||
| 405 | : | ||
| 406 | : "r2", "r1" | ||
| 407 | ); | ||
| 408 | } | ||
| 409 | |||
| 410 | nakedcc fn __aeabi_memcpy() noreturn { | 459 | nakedcc fn __aeabi_memcpy() noreturn { |
| 411 | @setRuntimeSafety(false); | 460 | @setRuntimeSafety(false); |
| 412 | if (use_thumb_1) { | 461 | if (use_thumb_1) { |
std/special/compiler_rt/extendXfYf2.zig+10-4| ... | @@ -2,21 +2,27 @@ const std = @import("std"); | ... | @@ -2,21 +2,27 @@ const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const is_test = builtin.is_test; | 3 | const is_test = builtin.is_test; |
| 4 | 4 | ||
| 5 | pub extern fn __extendsfdf2(a: f32) f64 { | ||
| 6 | return @inlineCall(extendXfYf2, f64, f32, @bitCast(u32, a)); | ||
| 7 | } | ||
| 8 | |||
| 5 | pub extern fn __extenddftf2(a: f64) f128 { | 9 | pub extern fn __extenddftf2(a: f64) f128 { |
| 6 | return extendXfYf2(f128, f64, @bitCast(u64, a)); | 10 | return @inlineCall(extendXfYf2, f128, f64, @bitCast(u64, a)); |
| 7 | } | 11 | } |
| 8 | 12 | ||
| 9 | pub extern fn __extendsftf2(a: f32) f128 { | 13 | pub extern fn __extendsftf2(a: f32) f128 { |
| 10 | return extendXfYf2(f128, f32, @bitCast(u32, a)); | 14 | return @inlineCall(extendXfYf2, f128, f32, @bitCast(u32, a)); |
| 11 | } | 15 | } |
| 12 | 16 | ||
| 13 | pub extern fn __extendhfsf2(a: u16) f32 { | 17 | pub extern fn __extendhfsf2(a: u16) f32 { |
| 14 | return extendXfYf2(f32, f16, a); | 18 | return @inlineCall(extendXfYf2, f32, f16, a); |
| 15 | } | 19 | } |
| 16 | 20 | ||
| 17 | const CHAR_BIT = 8; | 21 | const CHAR_BIT = 8; |
| 18 | 22 | ||
| 19 | inline fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t { | 23 | fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t { |
| 24 | @setRuntimeSafety(builtin.is_test); | ||
| 25 | |||
| 20 | const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); | 26 | const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); |
| 21 | const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits); | 27 | const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits); |
| 22 | const srcSigBits = std.math.floatMantissaBits(src_t); | 28 | const srcSigBits = std.math.floatMantissaBits(src_t); |
std/special/compiler_rt/floatdidf.zig created+22| ... | @@ -0,0 +1,22 @@ | ||
| 1 | const builtin = @import("builtin"); | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | const twop52: f64 = 0x1.0p52; | ||
| 5 | const twop32: f64 = 0x1.0p32; | ||
| 6 | |||
| 7 | pub extern fn __floatdidf(a: i64) f64 { | ||
| 8 | @setRuntimeSafety(builtin.is_test); | ||
| 9 | |||
| 10 | if (a == 0) return 0; | ||
| 11 | |||
| 12 | var low = @bitCast(i64, twop52); | ||
| 13 | const high = @intToFloat(f64, @truncate(i32, a >> 32)) * twop32; | ||
| 14 | |||
| 15 | low |= @bitCast(i64, a & 0xFFFFFFFF); | ||
| 16 | |||
| 17 | return (high - twop52) + @bitCast(f64, low); | ||
| 18 | } | ||
| 19 | |||
| 20 | test "import floatdidf" { | ||
| 21 | _ = @import("floatdidf_test.zig"); | ||
| 22 | } | ||
std/special/compiler_rt/floatdidf_test.zig created+53| ... | @@ -0,0 +1,53 @@ | ||
| 1 | const __floatdidf = @import("floatdidf.zig").__floatdidf; | ||
| 2 | const testing = @import("std").testing; | ||
| 3 | |||
| 4 | fn test__floatdidf(a: i64, expected: f64) void { | ||
| 5 | const r = __floatdidf(a); | ||
| 6 | testing.expect(r == expected); | ||
| 7 | } | ||
| 8 | |||
| 9 | test "floatdidf" { | ||
| 10 | test__floatdidf(0, 0.0); | ||
| 11 | test__floatdidf(1, 1.0); | ||
| 12 | test__floatdidf(2, 2.0); | ||
| 13 | test__floatdidf(20, 20.0); | ||
| 14 | test__floatdidf(-1, -1.0); | ||
| 15 | test__floatdidf(-2, -2.0); | ||
| 16 | test__floatdidf(-20, -20.0); | ||
| 17 | test__floatdidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62); | ||
| 18 | test__floatdidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62); | ||
| 19 | test__floatdidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62); | ||
| 20 | test__floatdidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62); | ||
| 21 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000008000000000)), -0x1.FFFFFEp+62); | ||
| 22 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000800)), -0x1.FFFFFFFFFFFFEp+62); | ||
| 23 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000010000000000)), -0x1.FFFFFCp+62); | ||
| 24 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000001000)), -0x1.FFFFFFFFFFFFCp+62); | ||
| 25 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000000)), -0x1.000000p+63); | ||
| 26 | test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000001)), -0x1.000000p+63); | ||
| 27 | test__floatdidf(0x0007FB72E8000000, 0x1.FEDCBAp+50); | ||
| 28 | test__floatdidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50); | ||
| 29 | test__floatdidf(0x0007FB72EB000000, 0x1.FEDCBACp+50); | ||
| 30 | test__floatdidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50); | ||
| 31 | test__floatdidf(0x0007FB72EC000000, 0x1.FEDCBBp+50); | ||
| 32 | test__floatdidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50); | ||
| 33 | test__floatdidf(0x0007FB72E6000000, 0x1.FEDCB98p+50); | ||
| 34 | test__floatdidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50); | ||
| 35 | test__floatdidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50); | ||
| 36 | test__floatdidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50); | ||
| 37 | test__floatdidf(0x0007FB72E4000000, 0x1.FEDCB9p+50); | ||
| 38 | test__floatdidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57); | ||
| 39 | test__floatdidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57); | ||
| 40 | test__floatdidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57); | ||
| 41 | test__floatdidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57); | ||
| 42 | test__floatdidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57); | ||
| 43 | test__floatdidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57); | ||
| 44 | test__floatdidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57); | ||
| 45 | test__floatdidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57); | ||
| 46 | test__floatdidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57); | ||
| 47 | test__floatdidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57); | ||
| 48 | test__floatdidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57); | ||
| 49 | test__floatdidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57); | ||
| 50 | test__floatdidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57); | ||
| 51 | test__floatdidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57); | ||
| 52 | test__floatdidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57); | ||
| 53 | } | ||
std/special/compiler_rt/floatsiXf.zig created+109| ... | @@ -0,0 +1,109 @@ | ||
| 1 | const builtin = @import("builtin"); | ||
| 2 | const std = @import("std"); | ||
| 3 | const maxInt = std.math.maxInt; | ||
| 4 | |||
| 5 | fn floatsiXf(comptime T: type, a: i32) T { | ||
| 6 | @setRuntimeSafety(builtin.is_test); | ||
| 7 | |||
| 8 | const Z = @IntType(false, T.bit_count); | ||
| 9 | const S = @IntType(false, T.bit_count - @clz(Z(T.bit_count) - 1)); | ||
| 10 | |||
| 11 | if (a == 0) { | ||
| 12 | return T(0.0); | ||
| 13 | } | ||
| 14 | |||
| 15 | const significandBits = std.math.floatMantissaBits(T); | ||
| 16 | const exponentBits = std.math.floatExponentBits(T); | ||
| 17 | const exponentBias = ((1 << exponentBits - 1) - 1); | ||
| 18 | |||
| 19 | const implicitBit = Z(1) << significandBits; | ||
| 20 | const signBit = Z(1 << Z.bit_count - 1); | ||
| 21 | |||
| 22 | const sign = a >> 31; | ||
| 23 | // Take absolute value of a via abs(x) = (x^(x >> 31)) - (x >> 31). | ||
| 24 | const abs_a = (a ^ sign) -% sign; | ||
| 25 | // The exponent is the width of abs(a) | ||
| 26 | const exp = Z(31 - @clz(abs_a)); | ||
| 27 | |||
| 28 | const sign_bit = if (sign < 0) signBit else 0; | ||
| 29 | |||
| 30 | var mantissa: Z = undefined; | ||
| 31 | // Shift a into the significand field and clear the implicit bit. | ||
| 32 | if (exp <= significandBits) { | ||
| 33 | // No rounding needed | ||
| 34 | const shift = @intCast(S, significandBits - exp); | ||
| 35 | mantissa = @intCast(Z, @bitCast(u32, abs_a)) << shift ^ implicitBit; | ||
| 36 | } else { | ||
| 37 | const shift = @intCast(S, exp - significandBits); | ||
| 38 | // Round to the nearest number after truncation | ||
| 39 | mantissa = @intCast(Z, @bitCast(u32, abs_a)) >> shift ^ implicitBit; | ||
| 40 | // Align to the left and check if the truncated part is halfway over | ||
| 41 | const round = @bitCast(u32, abs_a) << @intCast(u5, 31 - shift); | ||
| 42 | mantissa += @boolToInt(round > 0x80000000); | ||
| 43 | // Tie to even | ||
| 44 | mantissa += mantissa & 1; | ||
| 45 | } | ||
| 46 | |||
| 47 | // Use the addition instead of a or since we may have a carry from the | ||
| 48 | // mantissa to the exponent | ||
| 49 | var result = mantissa; | ||
| 50 | result += (exp + exponentBias) << significandBits; | ||
| 51 | result += sign_bit; | ||
| 52 | |||
| 53 | return @bitCast(T, result); | ||
| 54 | } | ||
| 55 | |||
| 56 | pub extern fn __floatsisf(arg: i32) f32 { | ||
| 57 | @setRuntimeSafety(builtin.is_test); | ||
| 58 | return @inlineCall(floatsiXf, f32, arg); | ||
| 59 | } | ||
| 60 | |||
| 61 | pub extern fn __floatsidf(arg: i32) f64 { | ||
| 62 | @setRuntimeSafety(builtin.is_test); | ||
| 63 | return @inlineCall(floatsiXf, f64, arg); | ||
| 64 | } | ||
| 65 | |||
| 66 | pub extern fn __floatsitf(arg: i32) f128 { | ||
| 67 | @setRuntimeSafety(builtin.is_test); | ||
| 68 | return @inlineCall(floatsiXf, f128, arg); | ||
| 69 | } | ||
| 70 | |||
| 71 | fn test_one_floatsitf(a: i32, expected: u128) void { | ||
| 72 | const r = __floatsitf(a); | ||
| 73 | std.testing.expect(@bitCast(u128, r) == expected); | ||
| 74 | } | ||
| 75 | |||
| 76 | fn test_one_floatsidf(a: i32, expected: u64) void { | ||
| 77 | const r = __floatsidf(a); | ||
| 78 | std.testing.expect(@bitCast(u64, r) == expected); | ||
| 79 | } | ||
| 80 | |||
| 81 | fn test_one_floatsisf(a: i32, expected: u32) void { | ||
| 82 | const r = __floatsisf(a); | ||
| 83 | std.testing.expect(@bitCast(u32, r) == expected); | ||
| 84 | } | ||
| 85 | |||
| 86 | test "floatsidf" { | ||
| 87 | test_one_floatsidf(0, 0x0000000000000000); | ||
| 88 | test_one_floatsidf(1, 0x3ff0000000000000); | ||
| 89 | test_one_floatsidf(-1, 0xbff0000000000000); | ||
| 90 | test_one_floatsidf(0x7FFFFFFF, 0x41dfffffffc00000); | ||
| 91 | test_one_floatsidf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc1e0000000000000); | ||
| 92 | } | ||
| 93 | |||
| 94 | test "floatsisf" { | ||
| 95 | test_one_floatsisf(0, 0x00000000); | ||
| 96 | test_one_floatsisf(1, 0x3f800000); | ||
| 97 | test_one_floatsisf(-1, 0xbf800000); | ||
| 98 | test_one_floatsisf(0x7FFFFFFF, 0x4f000000); | ||
| 99 | test_one_floatsisf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xcf000000); | ||
| 100 | } | ||
| 101 | |||
| 102 | test "floatsitf" { | ||
| 103 | test_one_floatsitf(0, 0); | ||
| 104 | test_one_floatsitf(0x7FFFFFFF, 0x401dfffffffc00000000000000000000); | ||
| 105 | test_one_floatsitf(0x12345678, 0x401b2345678000000000000000000000); | ||
| 106 | test_one_floatsitf(-0x12345678, 0xc01b2345678000000000000000000000); | ||
| 107 | test_one_floatsitf(@bitCast(i32, @intCast(u32, 0xffffffff)), 0xbfff0000000000000000000000000000); | ||
| 108 | test_one_floatsitf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc01e0000000000000000000000000000); | ||
| 109 | } | ||
std/special/compiler_rt/floatundidf.zig created+24| ... | @@ -0,0 +1,24 @@ | ||
| 1 | const builtin = @import("builtin"); | ||
| 2 | const std = @import("std"); | ||
| 3 | |||
| 4 | const twop52: f64 = 0x1.0p52; | ||
| 5 | const twop84: f64 = 0x1.0p84; | ||
| 6 | const twop84_plus_twop52: f64 = 0x1.00000001p84; | ||
| 7 | |||
| 8 | pub extern fn __floatundidf(a: u64) f64 { | ||
| 9 | @setRuntimeSafety(builtin.is_test); | ||
| 10 | |||
| 11 | if (a == 0) return 0; | ||
| 12 | |||
| 13 | var high = @bitCast(u64, twop84); | ||
| 14 | var low = @bitCast(u64, twop52); | ||
| 15 | |||
| 16 | high |= a >> 32; | ||
| 17 | low |= a & 0xFFFFFFFF; | ||
| 18 | |||
| 19 | return (@bitCast(f64, high) - twop84_plus_twop52) + @bitCast(f64, low); | ||
| 20 | } | ||
| 21 | |||
| 22 | test "import floatundidf" { | ||
| 23 | _ = @import("floatundidf_test.zig"); | ||
| 24 | } | ||
std/special/compiler_rt/floatundidf_test.zig created+50| ... | @@ -0,0 +1,50 @@ | ||
| 1 | const __floatundidf = @import("floatundidf.zig").__floatundidf; | ||
| 2 | const testing = @import("std").testing; | ||
| 3 | |||
| 4 | fn test__floatundidf(a: u64, expected: f64) void { | ||
| 5 | const r = __floatundidf(a); | ||
| 6 | testing.expect(r == expected); | ||
| 7 | } | ||
| 8 | |||
| 9 | test "floatundidf" { | ||
| 10 | test__floatundidf(0, 0.0); | ||
| 11 | test__floatundidf(1, 1.0); | ||
| 12 | test__floatundidf(2, 2.0); | ||
| 13 | test__floatundidf(20, 20.0); | ||
| 14 | test__floatundidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62); | ||
| 15 | test__floatundidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62); | ||
| 16 | test__floatundidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62); | ||
| 17 | test__floatundidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62); | ||
| 18 | test__floatundidf(0x8000008000000000, 0x1.000001p+63); | ||
| 19 | test__floatundidf(0x8000000000000800, 0x1.0000000000001p+63); | ||
| 20 | test__floatundidf(0x8000010000000000, 0x1.000002p+63); | ||
| 21 | test__floatundidf(0x8000000000001000, 0x1.0000000000002p+63); | ||
| 22 | test__floatundidf(0x8000000000000000, 0x1p+63); | ||
| 23 | test__floatundidf(0x8000000000000001, 0x1p+63); | ||
| 24 | test__floatundidf(0x0007FB72E8000000, 0x1.FEDCBAp+50); | ||
| 25 | test__floatundidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50); | ||
| 26 | test__floatundidf(0x0007FB72EB000000, 0x1.FEDCBACp+50); | ||
| 27 | test__floatundidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50); | ||
| 28 | test__floatundidf(0x0007FB72EC000000, 0x1.FEDCBBp+50); | ||
| 29 | test__floatundidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50); | ||
| 30 | test__floatundidf(0x0007FB72E6000000, 0x1.FEDCB98p+50); | ||
| 31 | test__floatundidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50); | ||
| 32 | test__floatundidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50); | ||
| 33 | test__floatundidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50); | ||
| 34 | test__floatundidf(0x0007FB72E4000000, 0x1.FEDCB9p+50); | ||
| 35 | test__floatundidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57); | ||
| 36 | test__floatundidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57); | ||
| 37 | test__floatundidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57); | ||
| 38 | test__floatundidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57); | ||
| 39 | test__floatundidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57); | ||
| 40 | test__floatundidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57); | ||
| 41 | test__floatundidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57); | ||
| 42 | test__floatundidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57); | ||
| 43 | test__floatundidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57); | ||
| 44 | test__floatundidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57); | ||
| 45 | test__floatundidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57); | ||
| 46 | test__floatundidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57); | ||
| 47 | test__floatundidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57); | ||
| 48 | test__floatundidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57); | ||
| 49 | test__floatundidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57); | ||
| 50 | } | ||
std/special/compiler_rt/floatunsidf.zig created+33| ... | @@ -0,0 +1,33 @@ | ||
| 1 | const builtin = @import("builtin"); | ||
| 2 | const std = @import("std"); | ||
| 3 | const maxInt = std.math.maxInt; | ||
| 4 | |||
| 5 | const implicitBit = u64(1) << 52; | ||
| 6 | |||
| 7 | pub extern fn __floatunsidf(arg: u32) f64 { | ||
| 8 | @setRuntimeSafety(builtin.is_test); | ||
| 9 | |||
| 10 | if (arg == 0) return 0.0; | ||
| 11 | |||
| 12 | // The exponent is the width of abs(a) | ||
| 13 | const exp = u64(31) - @clz(arg); | ||
| 14 | // Shift a into the significand field and clear the implicit bit | ||
| 15 | const shift = @intCast(u6, 52 - exp); | ||
| 16 | const mant = u64(arg) << shift ^ implicitBit; | ||
| 17 | |||
| 18 | return @bitCast(f64, mant | (exp + 1023) << 52); | ||
| 19 | } | ||
| 20 | |||
| 21 | fn test_one_floatunsidf(a: u32, expected: u64) void { | ||
| 22 | const r = __floatunsidf(a); | ||
| 23 | std.testing.expect(@bitCast(u64, r) == expected); | ||
| 24 | } | ||
| 25 | |||
| 26 | test "floatsidf" { | ||
| 27 | // Test the produced bit pattern | ||
| 28 | test_one_floatunsidf(0, 0x0000000000000000); | ||
| 29 | test_one_floatunsidf(1, 0x3ff0000000000000); | ||
| 30 | test_one_floatunsidf(0x7FFFFFFF, 0x41dfffffffc00000); | ||
| 31 | test_one_floatunsidf(@intCast(u32, 0x80000000), 0x41e0000000000000); | ||
| 32 | test_one_floatunsidf(@intCast(u32, 0xFFFFFFFF), 0x41efffffffe00000); | ||
| 33 | } | ||
std/special/compiler_rt/truncXfYf2.zig+4| ... | @@ -16,6 +16,10 @@ pub extern fn __trunctfdf2(a: f128) f64 { | ... | @@ -16,6 +16,10 @@ pub extern fn __trunctfdf2(a: f128) f64 { |
| 16 | return truncXfYf2(f64, f128, a); | 16 | return truncXfYf2(f64, f128, a); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | pub extern fn __truncdfsf2(a: f64) f32 { | ||
| 20 | return truncXfYf2(f32, f64, a); | ||
| 21 | } | ||
| 22 | |||
| 19 | inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t { | 23 | inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t { |
| 20 | const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); | 24 | const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits); |
| 21 | const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits); | 25 | 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" { | ... | @@ -200,3 +200,40 @@ test "trunctfdf2" { |
| 200 | test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000); | 200 | test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000); |
| 201 | test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab); | 201 | test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab); |
| 202 | } | 202 | } |
| 203 | |||
| 204 | const __truncdfsf2 = @import("truncXfYf2.zig").__truncdfsf2; | ||
| 205 | |||
| 206 | fn 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 | |||
| 225 | test "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 | } |