authorgravatar for justin.b.alexander1@gmail.comvegecode <justin.b.alexander1@gmail.com> 2019-04-17 13:13:03-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-24 21:15:39-04:00
log01168e15775a6fb7dcac086785de476a9ffc3fe2
treec967392d1c23ced6bf3075ca0b586d3e5ad40733
parentad994c96423dd1fcf81d3069aa2d39e345a0aeba

compiler-rt: add aeabi_dcmp, comparedf2


5 files changed, 348 insertions(+), 0 deletions(-)

CMakeLists.txt+2
......@@ -633,10 +633,12 @@ set(ZIG_STD_FILES
633633 "special/builtin.zig"
634634 "special/compiler_rt.zig"
635635 "special/compiler_rt/arm/aeabi_fcmp.zig"
636 "special/compiler_rt/arm/aeabi_dcmp.zig"
636637 "special/compiler_rt/addXf3.zig"
637638 "special/compiler_rt/aulldiv.zig"
638639 "special/compiler_rt/aullrem.zig"
639640 "special/compiler_rt/comparetf2.zig"
641 "special/compiler_rt/comparedf2.zig"
640642 "special/compiler_rt/comparesf2.zig"
641643 "special/compiler_rt/divsf3.zig"
642644 "special/compiler_rt/divdf3.zig"
std/special/compiler_rt.zig+15
......@@ -6,26 +6,33 @@ comptime {
66 const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong;
77
88 @export("__lesf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage);
9 @export("__ledf2", @import("compiler_rt/comparedf2.zig").__ledf2, linkage);
910 @export("__letf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
1011
1112 @export("__gesf2", @import("compiler_rt/comparesf2.zig").__gesf2, linkage);
13 @export("__gedf2", @import("compiler_rt/comparedf2.zig").__gedf2, linkage);
1214 @export("__getf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage);
1315
1416 if (!is_test) {
1517 // only create these aliases when not testing
1618 @export("__cmpsf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage);
19 @export("__cmpdf2", @import("compiler_rt/comparedf2.zig").__ledf2, linkage);
1720 @export("__cmptf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
1821
1922 @export("__eqsf2", @import("compiler_rt/comparesf2.zig").__eqsf2, linkage);
23 @export("__eqdf2", @import("compiler_rt/comparedf2.zig").__eqdf2, linkage);
2024 @export("__eqtf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
2125
2226 @export("__ltsf2", @import("compiler_rt/comparesf2.zig").__ltsf2, linkage);
27 @export("__ltdf2", @import("compiler_rt/comparedf2.zig").__ltdf2, linkage);
2328 @export("__lttf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
2429
2530 @export("__nesf2", @import("compiler_rt/comparesf2.zig").__nesf2, linkage);
31 @export("__nedf2", @import("compiler_rt/comparedf2.zig").__nedf2, linkage);
2632 @export("__netf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
2733
2834 @export("__gtsf2", @import("compiler_rt/comparesf2.zig").__gtsf2, linkage);
35 @export("__gtdf2", @import("compiler_rt/comparedf2.zig").__gtdf2, linkage);
2936 @export("__gttf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage);
3037
3138 @export("__gnu_h2f_ieee", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage);
......@@ -33,6 +40,7 @@ comptime {
3340 }
3441
3542 @export("__unordsf2", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage);
43 @export("__unorddf2", @import("compiler_rt/comparedf2.zig").__unorddf2, linkage);
3644 @export("__unordtf2", @import("compiler_rt/comparetf2.zig").__unordtf2, linkage);
3745
3846 @export("__addsf3", @import("compiler_rt/addXf3.zig").__addsf3, linkage);
......@@ -165,6 +173,13 @@ comptime {
165173 @export("__aeabi_fcmpge", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpge, linkage);
166174 @export("__aeabi_fcmpgt", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpgt, linkage);
167175 @export("__aeabi_fcmpun", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage);
176
177 @export("__aeabi_dcmpeq", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpeq, linkage);
178 @export("__aeabi_dcmplt", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmplt, linkage);
179 @export("__aeabi_dcmple", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmple, linkage);
180 @export("__aeabi_dcmpge", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpge, linkage);
181 @export("__aeabi_dcmpgt", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpgt, linkage);
182 @export("__aeabi_dcmpun", @import("compiler_rt/comparedf2.zig").__unorddf2, linkage);
168183 }
169184 if (builtin.os == builtin.Os.windows) {
170185 switch (builtin.arch) {
std/special/compiler_rt/arm/aeabi_dcmp.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_dcmp.S
4
5const compiler_rt_armhf_target = false; // TODO
6
7const ConditionalOperator = enum {
8 Eq,
9 Lt,
10 Le,
11 Ge,
12 Gt,
13};
14
15pub nakedcc fn __aeabi_dcmpeq() noreturn {
16 @setRuntimeSafety(false);
17 aeabi_dcmp(.Eq);
18 unreachable;
19}
20
21pub nakedcc fn __aeabi_dcmplt() noreturn {
22 @setRuntimeSafety(false);
23 aeabi_dcmp(.Lt);
24 unreachable;
25}
26
27pub nakedcc fn __aeabi_dcmple() noreturn {
28 @setRuntimeSafety(false);
29 aeabi_dcmp(.Le);
30 unreachable;
31}
32
33pub nakedcc fn __aeabi_dcmpge() noreturn {
34 @setRuntimeSafety(false);
35 aeabi_dcmp(.Ge);
36 unreachable;
37}
38
39pub nakedcc fn __aeabi_dcmpgt() noreturn {
40 @setRuntimeSafety(false);
41 aeabi_dcmp(.Gt);
42 unreachable;
43}
44
45inline fn convert_dcmp_args_to_df2_args() void {
46 asm volatile (
47 \\ vmov d0, r0, r1
48 \\ vmov d1, r2, r3
49 );
50}
51
52inline fn aeabi_dcmp(comptime cond: ConditionalOperator) void {
53 @setRuntimeSafety(false);
54 asm volatile (
55 \\ push { r4, lr }
56 );
57
58 if (compiler_rt_armhf_target) {
59 convert_dcmp_args_to_df2_args();
60 }
61
62 switch (cond) {
63 .Eq => asm volatile (
64 \\ bl __eqdf2
65 \\ cmp r0, #0
66 \\ beq 1f
67 \\ movs r0, #0
68 \\ pop { r4, pc }
69 \\ 1:
70 ),
71 .Lt => asm volatile (
72 \\ bl __ltdf2
73 \\ cmp r0, #0
74 \\ blt 1f
75 \\ movs r0, #0
76 \\ pop { r4, pc }
77 \\ 1:
78 ),
79 .Le => asm volatile (
80 \\ bl __ledf2
81 \\ cmp r0, #0
82 \\ ble 1f
83 \\ movs r0, #0
84 \\ pop { r4, pc }
85 \\ 1:
86 ),
87 .Ge => asm volatile (
88 \\ bl __ltdf2
89 \\ cmp r0, #0
90 \\ blt 1f
91 \\ movs r0, #0
92 \\ pop { r4, pc }
93 \\ 1:
94 ),
95 .Gt => asm volatile (
96 \\ bl __gtdf2
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/comparedf2.zig created+122
......@@ -0,0 +1,122 @@
1// Ported from:
2//
3// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparedf2.c
4
5const std = @import("std");
6const builtin = @import("builtin");
7const is_test = builtin.is_test;
8
9const fp_t = f64;
10const rep_t = u64;
11const srep_t = i64;
12
13const typeWidth = rep_t.bit_count;
14const significandBits = std.math.floatMantissaBits(fp_t);
15const exponentBits = std.math.floatExponentBits(fp_t);
16const signBit = (rep_t(1) << (significandBits + exponentBits));
17const absMask = signBit - 1;
18const implicitBit = rep_t(1) << significandBits;
19const significandMask = implicitBit - 1;
20const exponentMask = absMask ^ significandMask;
21const 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
25const LE_LESS = c_int(-1);
26const LE_EQUAL = c_int(0);
27const LE_GREATER = c_int(1);
28const LE_UNORDERED = c_int(1);
29
30pub extern fn __ledf2(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
68const GE_LESS = c_int(-1);
69const GE_EQUAL = c_int(0);
70const GE_GREATER = c_int(1);
71const GE_UNORDERED = c_int(-1); // Note: different from LE_UNORDERED
72
73pub extern fn __gedf2(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
97pub extern fn __unorddf2(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
104pub extern fn __eqdf2(a: fp_t, b: fp_t) c_int {
105 return __ledf2(a, b);
106}
107
108pub extern fn __ltdf2(a: fp_t, b: fp_t) c_int {
109 return __ledf2(a, b);
110}
111
112pub extern fn __nedf2(a: fp_t, b: fp_t) c_int {
113 return __ledf2(a, b);
114}
115
116pub extern fn __gtdf2(a: fp_t, b: fp_t) c_int {
117 return __gedf2(a, b);
118}
119
120test "import comparedf2" {
121 _ = @import("comparedf2_test.zig");
122}
std/special/compiler_rt/comparedf2_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/comparedf2_test.c
4
5const std = @import("std");
6const builtin = @import("builtin");
7const is_test = builtin.is_test;
8
9const comparedf2 = @import("comparedf2.zig");
10
11const TestVector = struct {
12 a: f64,
13 b: f64,
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
23fn test__cmpdf2(vector: TestVector) bool {
24 if (comparedf2.__eqdf2(vector.a, vector.b) != vector.eqReference) {
25 return false;
26 }
27 if (comparedf2.__gedf2(vector.a, vector.b) != vector.geReference) {
28 return false;
29 }
30 if (comparedf2.__gtdf2(vector.a, vector.b) != vector.gtReference) {
31 return false;
32 }
33 if (comparedf2.__ledf2(vector.a, vector.b) != vector.leReference) {
34 return false;
35 }
36 if (comparedf2.__ltdf2(vector.a, vector.b) != vector.ltReference) {
37 return false;
38 }
39 if (comparedf2.__nedf2(vector.a, vector.b) != vector.neReference) {
40 return false;
41 }
42 if (comparedf2.__unorddf2(vector.a, vector.b) != vector.unReference) {
43 return false;
44 }
45 return true;
46}
47
48const arguments = []f64{
49 std.math.nan(f64),
50 -std.math.inf(f64),
51 -0x1.fffffffffffffp1023,
52 -0x1.0000000000001p0 - 0x1.0000000000000p0,
53 -0x1.fffffffffffffp-1,
54 -0x1.0000000000000p-1022,
55 -0x0.fffffffffffffp-1022,
56 -0x0.0000000000001p-1022,
57 -0.0,
58 0.0,
59 0x0.0000000000001p-1022,
60 0x0.fffffffffffffp-1022,
61 0x1.0000000000000p-1022,
62 0x1.fffffffffffffp-1,
63 0x1.0000000000000p0,
64 0x1.0000000000001p0,
65 0x1.fffffffffffffp1023,
66 std.math.inf(f64),
67};
68
69fn generateVector(comptime a: f64, comptime b: f64) 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
86const 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
97test "compare f64" {
98 for (test_vectors) |vector, i| {
99 std.testing.expect(test__cmpdf2(vector));
100 }
101}