| author | |
| committer | |
| log | bb25f212b3630eeaac6a76575f21875d7d94e1a4 |
| tree | 0358f86009a463135e84bbf5575203a630422f03 |
| parent | 22f5e5fd3e7f4cf15c38c2b3be0b0e357854026a |
5 files changed, 350 insertions(+), 0 deletions(-)
CMakeLists.txt+2| ... | @@ -636,10 +636,12 @@ set(ZIG_STD_FILES | ... | @@ -636,10 +636,12 @@ set(ZIG_STD_FILES |
| 636 | "special/build_runner.zig" | 636 | "special/build_runner.zig" |
| 637 | "special/builtin.zig" | 637 | "special/builtin.zig" |
| 638 | "special/compiler_rt.zig" | 638 | "special/compiler_rt.zig" |
| 639 | "special/compiler_rt/arm/aeabi_fcmp.zig" | ||
| 639 | "special/compiler_rt/addXf3.zig" | 640 | "special/compiler_rt/addXf3.zig" |
| 640 | "special/compiler_rt/aulldiv.zig" | 641 | "special/compiler_rt/aulldiv.zig" |
| 641 | "special/compiler_rt/aullrem.zig" | 642 | "special/compiler_rt/aullrem.zig" |
| 642 | "special/compiler_rt/comparetf2.zig" | 643 | "special/compiler_rt/comparetf2.zig" |
| 644 | "special/compiler_rt/comparesf2.zig" | ||
| 643 | "special/compiler_rt/divsf3.zig" | 645 | "special/compiler_rt/divsf3.zig" |
| 644 | "special/compiler_rt/divdf3.zig" | 646 | "special/compiler_rt/divdf3.zig" |
| 645 | "special/compiler_rt/divti3.zig" | 647 | "special/compiler_rt/divti3.zig" |
std/special/compiler_rt.zig+21| ... | @@ -5,20 +5,34 @@ comptime { | ... | @@ -5,20 +5,34 @@ comptime { |
| 5 | const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak; | 5 | const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak; |
| 6 | const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong; | 6 | const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong; |
| 7 | 7 | ||
| 8 | @export("__lesf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage); | ||
| 8 | @export("__letf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); | 9 | @export("__letf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); |
| 10 | |||
| 11 | @export("__gesf2", @import("compiler_rt/comparesf2.zig").__gesf2, linkage); | ||
| 9 | @export("__getf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage); | 12 | @export("__getf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage); |
| 10 | 13 | ||
| 11 | if (!is_test) { | 14 | if (!is_test) { |
| 12 | // only create these aliases when not testing | 15 | // only create these aliases when not testing |
| 16 | @export("__cmpsf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage); | ||
| 13 | @export("__cmptf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); | 17 | @export("__cmptf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); |
| 18 | |||
| 19 | @export("__eqsf2", @import("compiler_rt/comparesf2.zig").__eqsf2, linkage); | ||
| 14 | @export("__eqtf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); | 20 | @export("__eqtf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); |
| 21 | |||
| 22 | @export("__ltsf2", @import("compiler_rt/comparesf2.zig").__ltsf2, linkage); | ||
| 15 | @export("__lttf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); | 23 | @export("__lttf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); |
| 24 | |||
| 25 | @export("__nesf2", @import("compiler_rt/comparesf2.zig").__nesf2, linkage); | ||
| 16 | @export("__netf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); | 26 | @export("__netf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage); |
| 27 | |||
| 28 | @export("__gtsf2", @import("compiler_rt/comparesf2.zig").__gtsf2, linkage); | ||
| 17 | @export("__gttf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage); | 29 | @export("__gttf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage); |
| 30 | |||
| 18 | @export("__gnu_h2f_ieee", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage); | 31 | @export("__gnu_h2f_ieee", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage); |
| 19 | @export("__gnu_f2h_ieee", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); | 32 | @export("__gnu_f2h_ieee", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage); |
| 20 | } | 33 | } |
| 21 | 34 | ||
| 35 | @export("__unordsf2", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage); | ||
| 22 | @export("__unordtf2", @import("compiler_rt/comparetf2.zig").__unordtf2, linkage); | 36 | @export("__unordtf2", @import("compiler_rt/comparetf2.zig").__unordtf2, linkage); |
| 23 | 37 | ||
| 24 | @export("__addsf3", @import("compiler_rt/addXf3.zig").__addsf3, linkage); | 38 | @export("__addsf3", @import("compiler_rt/addXf3.zig").__addsf3, linkage); |
| ... | @@ -144,6 +158,13 @@ comptime { | ... | @@ -144,6 +158,13 @@ comptime { |
| 144 | 158 | ||
| 145 | @export("__aeabi_fdiv", @import("compiler_rt/divsf3.zig").__divsf3, linkage); | 159 | @export("__aeabi_fdiv", @import("compiler_rt/divsf3.zig").__divsf3, linkage); |
| 146 | @export("__aeabi_ddiv", @import("compiler_rt/divdf3.zig").__divdf3, linkage); | 160 | @export("__aeabi_ddiv", @import("compiler_rt/divdf3.zig").__divdf3, linkage); |
| 161 | |||
| 162 | @export("__aeabi_fcmpeq", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpeq, linkage); | ||
| 163 | @export("__aeabi_fcmplt", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmplt, linkage); | ||
| 164 | @export("__aeabi_fcmple", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmple, linkage); | ||
| 165 | @export("__aeabi_fcmpge", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpge, linkage); | ||
| 166 | @export("__aeabi_fcmpgt", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpgt, linkage); | ||
| 167 | @export("__aeabi_fcmpun", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage); | ||
| 147 | } | 168 | } |
| 148 | if (builtin.os == builtin.Os.windows) { | 169 | if (builtin.os == builtin.Os.windows) { |
| 149 | switch (builtin.arch) { | 170 | switch (builtin.arch) { |
std/special/compiler_rt/arm/aeabi_fcmp.zig created+108| ... | @@ -0,0 +1,108 @@ | ||
| 1 | // Ported from: | ||
| 2 | // | ||
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/arm/aeabi_fcmp.S | ||
| 4 | |||
| 5 | const compiler_rt_armhf_target = false; // TODO | ||
| 6 | |||
| 7 | const ConditionalOperator = enum { | ||
| 8 | Eq, | ||
| 9 | Lt, | ||
| 10 | Le, | ||
| 11 | Ge, | ||
| 12 | Gt, | ||
| 13 | }; | ||
| 14 | |||
| 15 | pub nakedcc fn __aeabi_fcmpeq() noreturn { | ||
| 16 | @setRuntimeSafety(false); | ||
| 17 | aeabi_fcmp(.Eq); | ||
| 18 | unreachable; | ||
| 19 | } | ||
| 20 | |||
| 21 | pub nakedcc fn __aeabi_fcmplt() noreturn { | ||
| 22 | @setRuntimeSafety(false); | ||
| 23 | aeabi_fcmp(.Lt); | ||
| 24 | unreachable; | ||
| 25 | } | ||
| 26 | |||
| 27 | pub nakedcc fn __aeabi_fcmple() noreturn { | ||
| 28 | @setRuntimeSafety(false); | ||
| 29 | aeabi_fcmp(.Le); | ||
| 30 | unreachable; | ||
| 31 | } | ||
| 32 | |||
| 33 | pub nakedcc fn __aeabi_fcmpge() noreturn { | ||
| 34 | @setRuntimeSafety(false); | ||
| 35 | aeabi_fcmp(.Ge); | ||
| 36 | unreachable; | ||
| 37 | } | ||
| 38 | |||
| 39 | pub nakedcc fn __aeabi_fcmpgt() noreturn { | ||
| 40 | @setRuntimeSafety(false); | ||
| 41 | aeabi_fcmp(.Gt); | ||
| 42 | unreachable; | ||
| 43 | } | ||
| 44 | |||
| 45 | inline fn convert_fcmp_args_to_sf2_args() void { | ||
| 46 | asm volatile ( | ||
| 47 | \\ vmov s0, r0 | ||
| 48 | \\ vmov s1, r1 | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | inline fn aeabi_fcmp(comptime cond: ConditionalOperator) void { | ||
| 53 | @setRuntimeSafety(false); | ||
| 54 | asm volatile ( | ||
| 55 | \\ push { r4, lr } | ||
| 56 | ); | ||
| 57 | |||
| 58 | if (compiler_rt_armhf_target) { | ||
| 59 | convert_fcmp_args_to_sf2_args(); | ||
| 60 | } | ||
| 61 | |||
| 62 | switch (cond) { | ||
| 63 | .Eq => asm volatile ( | ||
| 64 | \\ bl __eqsf2 | ||
| 65 | \\ cmp r0, #0 | ||
| 66 | \\ beq 1f | ||
| 67 | \\ movs r0, #0 | ||
| 68 | \\ pop { r4, pc } | ||
| 69 | \\ 1: | ||
| 70 | ), | ||
| 71 | .Lt => asm volatile ( | ||
| 72 | \\ bl __ltsf2 | ||
| 73 | \\ cmp r0, #0 | ||
| 74 | \\ blt 1f | ||
| 75 | \\ movs r0, #0 | ||
| 76 | \\ pop { r4, pc } | ||
| 77 | \\ 1: | ||
| 78 | ), | ||
| 79 | .Le => asm volatile ( | ||
| 80 | \\ bl __lesf2 | ||
| 81 | \\ cmp r0, #0 | ||
| 82 | \\ ble 1f | ||
| 83 | \\ movs r0, #0 | ||
| 84 | \\ pop { r4, pc } | ||
| 85 | \\ 1: | ||
| 86 | ), | ||
| 87 | .Ge => asm volatile ( | ||
| 88 | \\ bl __ltsf2 | ||
| 89 | \\ cmp r0, #0 | ||
| 90 | \\ blt 1f | ||
| 91 | \\ movs r0, #0 | ||
| 92 | \\ pop { r4, pc } | ||
| 93 | \\ 1: | ||
| 94 | ), | ||
| 95 | .Gt => asm volatile ( | ||
| 96 | \\ bl __gtsf2 | ||
| 97 | \\ cmp r0, #0 | ||
| 98 | \\ bgt 1f | ||
| 99 | \\ movs r0, #0 | ||
| 100 | \\ pop { r4, pc } | ||
| 101 | \\ 1: | ||
| 102 | ), | ||
| 103 | } | ||
| 104 | asm volatile ( | ||
| 105 | \\ movs r0, #1 | ||
| 106 | \\ pop { r4, pc } | ||
| 107 | ); | ||
| 108 | } | ||
std/special/compiler_rt/comparesf2.zig created+118| ... | @@ -0,0 +1,118 @@ | ||
| 1 | // Ported from: | ||
| 2 | // | ||
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparesf2.c | ||
| 4 | |||
| 5 | const std = @import("std"); | ||
| 6 | const builtin = @import("builtin"); | ||
| 7 | const is_test = builtin.is_test; | ||
| 8 | |||
| 9 | const fp_t = f32; | ||
| 10 | const rep_t = u32; | ||
| 11 | const srep_t = i32; | ||
| 12 | |||
| 13 | const typeWidth = rep_t.bit_count; | ||
| 14 | const significandBits = std.math.floatMantissaBits(fp_t); | ||
| 15 | const exponentBits = std.math.floatExponentBits(fp_t); | ||
| 16 | const signBit = (rep_t(1) << (significandBits + exponentBits)); | ||
| 17 | const absMask = signBit - 1; | ||
| 18 | const implicitBit = rep_t(1) << significandBits; | ||
| 19 | const significandMask = implicitBit - 1; | ||
| 20 | const exponentMask = absMask ^ significandMask; | ||
| 21 | const infRep = @bitCast(rep_t, std.math.inf(fp_t)); | ||
| 22 | |||
| 23 | // TODO https://github.com/ziglang/zig/issues/641 | ||
| 24 | // and then make the return types of some of these functions the enum instead of c_int | ||
| 25 | const LE_LESS = c_int(-1); | ||
| 26 | const LE_EQUAL = c_int(0); | ||
| 27 | const LE_GREATER = c_int(1); | ||
| 28 | const LE_UNORDERED = c_int(1); | ||
| 29 | |||
| 30 | pub extern fn __lesf2(a: fp_t, b: fp_t) c_int { | ||
| 31 | @setRuntimeSafety(is_test); | ||
| 32 | const aInt: srep_t = @bitCast(srep_t, a); | ||
| 33 | const bInt: srep_t = @bitCast(srep_t, b); | ||
| 34 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | ||
| 35 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | ||
| 36 | |||
| 37 | // If either a or b is NaN, they are unordered. | ||
| 38 | if (aAbs > infRep or bAbs > infRep) return LE_UNORDERED; | ||
| 39 | |||
| 40 | // If a and b are both zeros, they are equal. | ||
| 41 | if ((aAbs | bAbs) == 0) return LE_EQUAL; | ||
| 42 | |||
| 43 | // If at least one of a and b is positive, we get the same result comparing | ||
| 44 | // a and b as signed integers as we would with a fp_ting-point compare. | ||
| 45 | if ((aInt & bInt) >= 0) { | ||
| 46 | if (aInt < bInt) { | ||
| 47 | return LE_LESS; | ||
| 48 | } else if (aInt == bInt) { | ||
| 49 | return LE_EQUAL; | ||
| 50 | } else return LE_GREATER; | ||
| 51 | } | ||
| 52 | |||
| 53 | // Otherwise, both are negative, so we need to flip the sense of the | ||
| 54 | // comparison to get the correct result. (This assumes a twos- or ones- | ||
| 55 | // complement integer representation; if integers are represented in a | ||
| 56 | // sign-magnitude representation, then this flip is incorrect). | ||
| 57 | else { | ||
| 58 | if (aInt > bInt) { | ||
| 59 | return LE_LESS; | ||
| 60 | } else if (aInt == bInt) { | ||
| 61 | return LE_EQUAL; | ||
| 62 | } else return LE_GREATER; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | // TODO https://github.com/ziglang/zig/issues/641 | ||
| 67 | // and then make the return types of some of these functions the enum instead of c_int | ||
| 68 | const GE_LESS = c_int(-1); | ||
| 69 | const GE_EQUAL = c_int(0); | ||
| 70 | const GE_GREATER = c_int(1); | ||
| 71 | const GE_UNORDERED = c_int(-1); // Note: different from LE_UNORDERED | ||
| 72 | |||
| 73 | pub extern fn __gesf2(a: fp_t, b: fp_t) c_int { | ||
| 74 | @setRuntimeSafety(is_test); | ||
| 75 | const aInt: srep_t = @bitCast(srep_t, a); | ||
| 76 | const bInt: srep_t = @bitCast(srep_t, b); | ||
| 77 | const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask; | ||
| 78 | const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask; | ||
| 79 | |||
| 80 | if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; | ||
| 81 | if ((aAbs | bAbs) == 0) return GE_EQUAL; | ||
| 82 | if ((aInt & bInt) >= 0) { | ||
| 83 | if (aInt < bInt) { | ||
| 84 | return GE_LESS; | ||
| 85 | } else if (aInt == bInt) { | ||
| 86 | return GE_EQUAL; | ||
| 87 | } else return GE_GREATER; | ||
| 88 | } else { | ||
| 89 | if (aInt > bInt) { | ||
| 90 | return GE_LESS; | ||
| 91 | } else if (aInt == bInt) { | ||
| 92 | return GE_EQUAL; | ||
| 93 | } else return GE_GREATER; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | pub extern fn __unordsf2(a: fp_t, b: fp_t) c_int { | ||
| 98 | @setRuntimeSafety(is_test); | ||
| 99 | const aAbs: rep_t = @bitCast(rep_t, a) & absMask; | ||
| 100 | const bAbs: rep_t = @bitCast(rep_t, b) & absMask; | ||
| 101 | return @boolToInt(aAbs > infRep or bAbs > infRep); | ||
| 102 | } | ||
| 103 | |||
| 104 | pub extern fn __eqsf2(a: fp_t, b: fp_t) c_int { | ||
| 105 | return __lesf2(a, b); | ||
| 106 | } | ||
| 107 | |||
| 108 | pub extern fn __ltsf2(a: fp_t, b: fp_t) c_int { | ||
| 109 | return __lesf2(a, b); | ||
| 110 | } | ||
| 111 | |||
| 112 | pub extern fn __nesf2(a: fp_t, b: fp_t) c_int { | ||
| 113 | return __lesf2(a, b); | ||
| 114 | } | ||
| 115 | |||
| 116 | pub extern fn __gtsf2(a: fp_t, b: fp_t) c_int { | ||
| 117 | return __gesf2(a, b); | ||
| 118 | } | ||
std/special/compiler_rt/comparesf2_test.zig created+101| ... | @@ -0,0 +1,101 @@ | ||
| 1 | // Ported from: | ||
| 2 | // | ||
| 3 | // https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/test/builtins/Unit/comparesf2_test.c | ||
| 4 | |||
| 5 | const std = @import("std"); | ||
| 6 | const builtin = @import("builtin"); | ||
| 7 | const is_test = builtin.is_test; | ||
| 8 | |||
| 9 | const comparesf2 = @import("comparesf2.zig"); | ||
| 10 | |||
| 11 | const TestVector = struct { | ||
| 12 | a: f32, | ||
| 13 | b: f32, | ||
| 14 | eqReference: c_int, | ||
| 15 | geReference: c_int, | ||
| 16 | gtReference: c_int, | ||
| 17 | leReference: c_int, | ||
| 18 | ltReference: c_int, | ||
| 19 | neReference: c_int, | ||
| 20 | unReference: c_int, | ||
| 21 | }; | ||
| 22 | |||
| 23 | fn test__cmpsf2(vector: TestVector) bool { | ||
| 24 | if (comparesf2.__eqsf2(vector.a, vector.b) != vector.eqReference) { | ||
| 25 | return false; | ||
| 26 | } | ||
| 27 | if (comparesf2.__gesf2(vector.a, vector.b) != vector.geReference) { | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 | if (comparesf2.__gtsf2(vector.a, vector.b) != vector.gtReference) { | ||
| 31 | return false; | ||
| 32 | } | ||
| 33 | if (comparesf2.__lesf2(vector.a, vector.b) != vector.leReference) { | ||
| 34 | return false; | ||
| 35 | } | ||
| 36 | if (comparesf2.__ltsf2(vector.a, vector.b) != vector.ltReference) { | ||
| 37 | return false; | ||
| 38 | } | ||
| 39 | if (comparesf2.__nesf2(vector.a, vector.b) != vector.neReference) { | ||
| 40 | return false; | ||
| 41 | } | ||
| 42 | if (comparesf2.__unordsf2(vector.a, vector.b) != vector.unReference) { | ||
| 43 | return false; | ||
| 44 | } | ||
| 45 | return true; | ||
| 46 | } | ||
| 47 | |||
| 48 | const arguments = []f32{ | ||
| 49 | std.math.nan(f32), | ||
| 50 | -std.math.inf(f32), | ||
| 51 | -0x1.fffffep127, | ||
| 52 | -0x1.000002p0 - 0x1.000000p0, | ||
| 53 | -0x1.fffffep-1, | ||
| 54 | -0x1.000000p-126, | ||
| 55 | -0x0.fffffep-126, | ||
| 56 | -0x0.000002p-126, | ||
| 57 | -0.0, | ||
| 58 | 0.0, | ||
| 59 | 0x0.000002p-126, | ||
| 60 | 0x0.fffffep-126, | ||
| 61 | 0x1.000000p-126, | ||
| 62 | 0x1.fffffep-1, | ||
| 63 | 0x1.000000p0, | ||
| 64 | 0x1.000002p0, | ||
| 65 | 0x1.fffffep127, | ||
| 66 | std.math.inf(f32), | ||
| 67 | }; | ||
| 68 | |||
| 69 | fn generateVector(comptime a: f32, comptime b: f32) TestVector { | ||
| 70 | const leResult = if (a < b) -1 else if (a == b) 0 else 1; | ||
| 71 | const geResult = if (a > b) 1 else if (a == b) 0 else -1; | ||
| 72 | const unResult = if (a != a or b != b) 1 else 0; | ||
| 73 | return TestVector{ | ||
| 74 | .a = a, | ||
| 75 | .b = b, | ||
| 76 | .eqReference = leResult, | ||
| 77 | .geReference = geResult, | ||
| 78 | .gtReference = geResult, | ||
| 79 | .leReference = leResult, | ||
| 80 | .ltReference = leResult, | ||
| 81 | .neReference = leResult, | ||
| 82 | .unReference = unResult, | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | |||
| 86 | const test_vectors = init: { | ||
| 87 | @setEvalBranchQuota(10000); | ||
| 88 | var vectors: [arguments.len * arguments.len]TestVector = undefined; | ||
| 89 | for (arguments[0..]) |arg_i, i| { | ||
| 90 | for (arguments[0..]) |arg_j, j| { | ||
| 91 | vectors[(i * arguments.len) + j] = generateVector(arg_i, arg_j); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | break :init vectors; | ||
| 95 | }; | ||
| 96 | |||
| 97 | test "compare f32" { | ||
| 98 | for (test_vectors) |vector, i| { | ||
| 99 | std.testing.expect(test__cmpsf2(vector)); | ||
| 100 | } | ||
| 101 | } | ||