Commit 01168e1577
Changed files (5)
std
special
std/special/compiler_rt/arm/aeabi_dcmp.zig
@@ -0,0 +1,108 @@
+// Ported from:
+//
+// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/arm/aeabi_dcmp.S
+
+const compiler_rt_armhf_target = false; // TODO
+
+const ConditionalOperator = enum {
+ Eq,
+ Lt,
+ Le,
+ Ge,
+ Gt,
+};
+
+pub nakedcc fn __aeabi_dcmpeq() noreturn {
+ @setRuntimeSafety(false);
+ aeabi_dcmp(.Eq);
+ unreachable;
+}
+
+pub nakedcc fn __aeabi_dcmplt() noreturn {
+ @setRuntimeSafety(false);
+ aeabi_dcmp(.Lt);
+ unreachable;
+}
+
+pub nakedcc fn __aeabi_dcmple() noreturn {
+ @setRuntimeSafety(false);
+ aeabi_dcmp(.Le);
+ unreachable;
+}
+
+pub nakedcc fn __aeabi_dcmpge() noreturn {
+ @setRuntimeSafety(false);
+ aeabi_dcmp(.Ge);
+ unreachable;
+}
+
+pub nakedcc fn __aeabi_dcmpgt() noreturn {
+ @setRuntimeSafety(false);
+ aeabi_dcmp(.Gt);
+ unreachable;
+}
+
+inline fn convert_dcmp_args_to_df2_args() void {
+ asm volatile (
+ \\ vmov d0, r0, r1
+ \\ vmov d1, r2, r3
+ );
+}
+
+inline fn aeabi_dcmp(comptime cond: ConditionalOperator) void {
+ @setRuntimeSafety(false);
+ asm volatile (
+ \\ push { r4, lr }
+ );
+
+ if (compiler_rt_armhf_target) {
+ convert_dcmp_args_to_df2_args();
+ }
+
+ switch (cond) {
+ .Eq => asm volatile (
+ \\ bl __eqdf2
+ \\ cmp r0, #0
+ \\ beq 1f
+ \\ movs r0, #0
+ \\ pop { r4, pc }
+ \\ 1:
+ ),
+ .Lt => asm volatile (
+ \\ bl __ltdf2
+ \\ cmp r0, #0
+ \\ blt 1f
+ \\ movs r0, #0
+ \\ pop { r4, pc }
+ \\ 1:
+ ),
+ .Le => asm volatile (
+ \\ bl __ledf2
+ \\ cmp r0, #0
+ \\ ble 1f
+ \\ movs r0, #0
+ \\ pop { r4, pc }
+ \\ 1:
+ ),
+ .Ge => asm volatile (
+ \\ bl __ltdf2
+ \\ cmp r0, #0
+ \\ blt 1f
+ \\ movs r0, #0
+ \\ pop { r4, pc }
+ \\ 1:
+ ),
+ .Gt => asm volatile (
+ \\ bl __gtdf2
+ \\ cmp r0, #0
+ \\ bgt 1f
+ \\ movs r0, #0
+ \\ pop { r4, pc }
+ \\ 1:
+ ),
+ }
+ asm volatile (
+ \\ movs r0, #1
+ \\ pop { r4, pc }
+ );
+}
std/special/compiler_rt/comparedf2.zig
@@ -0,0 +1,122 @@
+// Ported from:
+//
+// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/lib/builtins/comparedf2.c
+
+const std = @import("std");
+const builtin = @import("builtin");
+const is_test = builtin.is_test;
+
+const fp_t = f64;
+const rep_t = u64;
+const srep_t = i64;
+
+const typeWidth = rep_t.bit_count;
+const significandBits = std.math.floatMantissaBits(fp_t);
+const exponentBits = std.math.floatExponentBits(fp_t);
+const signBit = (rep_t(1) << (significandBits + exponentBits));
+const absMask = signBit - 1;
+const implicitBit = rep_t(1) << significandBits;
+const significandMask = implicitBit - 1;
+const exponentMask = absMask ^ significandMask;
+const infRep = @bitCast(rep_t, std.math.inf(fp_t));
+
+// TODO https://github.com/ziglang/zig/issues/641
+// and then make the return types of some of these functions the enum instead of c_int
+const LE_LESS = c_int(-1);
+const LE_EQUAL = c_int(0);
+const LE_GREATER = c_int(1);
+const LE_UNORDERED = c_int(1);
+
+pub extern fn __ledf2(a: fp_t, b: fp_t) c_int {
+ @setRuntimeSafety(is_test);
+ const aInt: srep_t = @bitCast(srep_t, a);
+ const bInt: srep_t = @bitCast(srep_t, b);
+ const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask;
+ const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask;
+
+ // If either a or b is NaN, they are unordered.
+ if (aAbs > infRep or bAbs > infRep) return LE_UNORDERED;
+
+ // If a and b are both zeros, they are equal.
+ if ((aAbs | bAbs) == 0) return LE_EQUAL;
+
+ // If at least one of a and b is positive, we get the same result comparing
+ // a and b as signed integers as we would with a fp_ting-point compare.
+ if ((aInt & bInt) >= 0) {
+ if (aInt < bInt) {
+ return LE_LESS;
+ } else if (aInt == bInt) {
+ return LE_EQUAL;
+ } else return LE_GREATER;
+ }
+
+ // Otherwise, both are negative, so we need to flip the sense of the
+ // comparison to get the correct result. (This assumes a twos- or ones-
+ // complement integer representation; if integers are represented in a
+ // sign-magnitude representation, then this flip is incorrect).
+ else {
+ if (aInt > bInt) {
+ return LE_LESS;
+ } else if (aInt == bInt) {
+ return LE_EQUAL;
+ } else return LE_GREATER;
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/641
+// and then make the return types of some of these functions the enum instead of c_int
+const GE_LESS = c_int(-1);
+const GE_EQUAL = c_int(0);
+const GE_GREATER = c_int(1);
+const GE_UNORDERED = c_int(-1); // Note: different from LE_UNORDERED
+
+pub extern fn __gedf2(a: fp_t, b: fp_t) c_int {
+ @setRuntimeSafety(is_test);
+ const aInt: srep_t = @bitCast(srep_t, a);
+ const bInt: srep_t = @bitCast(srep_t, b);
+ const aAbs: rep_t = @bitCast(rep_t, aInt) & absMask;
+ const bAbs: rep_t = @bitCast(rep_t, bInt) & absMask;
+
+ if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
+ if ((aAbs | bAbs) == 0) return GE_EQUAL;
+ if ((aInt & bInt) >= 0) {
+ if (aInt < bInt) {
+ return GE_LESS;
+ } else if (aInt == bInt) {
+ return GE_EQUAL;
+ } else return GE_GREATER;
+ } else {
+ if (aInt > bInt) {
+ return GE_LESS;
+ } else if (aInt == bInt) {
+ return GE_EQUAL;
+ } else return GE_GREATER;
+ }
+}
+
+pub extern fn __unorddf2(a: fp_t, b: fp_t) c_int {
+ @setRuntimeSafety(is_test);
+ const aAbs: rep_t = @bitCast(rep_t, a) & absMask;
+ const bAbs: rep_t = @bitCast(rep_t, b) & absMask;
+ return @boolToInt(aAbs > infRep or bAbs > infRep);
+}
+
+pub extern fn __eqdf2(a: fp_t, b: fp_t) c_int {
+ return __ledf2(a, b);
+}
+
+pub extern fn __ltdf2(a: fp_t, b: fp_t) c_int {
+ return __ledf2(a, b);
+}
+
+pub extern fn __nedf2(a: fp_t, b: fp_t) c_int {
+ return __ledf2(a, b);
+}
+
+pub extern fn __gtdf2(a: fp_t, b: fp_t) c_int {
+ return __gedf2(a, b);
+}
+
+test "import comparedf2" {
+ _ = @import("comparedf2_test.zig");
+}
std/special/compiler_rt/comparedf2_test.zig
@@ -0,0 +1,101 @@
+// Ported from:
+//
+// https://github.com/llvm/llvm-project/commit/d674d96bc56c0f377879d01c9d8dfdaaa7859cdb/compiler-rt/test/builtins/Unit/comparedf2_test.c
+
+const std = @import("std");
+const builtin = @import("builtin");
+const is_test = builtin.is_test;
+
+const comparedf2 = @import("comparedf2.zig");
+
+const TestVector = struct {
+ a: f64,
+ b: f64,
+ eqReference: c_int,
+ geReference: c_int,
+ gtReference: c_int,
+ leReference: c_int,
+ ltReference: c_int,
+ neReference: c_int,
+ unReference: c_int,
+};
+
+fn test__cmpdf2(vector: TestVector) bool {
+ if (comparedf2.__eqdf2(vector.a, vector.b) != vector.eqReference) {
+ return false;
+ }
+ if (comparedf2.__gedf2(vector.a, vector.b) != vector.geReference) {
+ return false;
+ }
+ if (comparedf2.__gtdf2(vector.a, vector.b) != vector.gtReference) {
+ return false;
+ }
+ if (comparedf2.__ledf2(vector.a, vector.b) != vector.leReference) {
+ return false;
+ }
+ if (comparedf2.__ltdf2(vector.a, vector.b) != vector.ltReference) {
+ return false;
+ }
+ if (comparedf2.__nedf2(vector.a, vector.b) != vector.neReference) {
+ return false;
+ }
+ if (comparedf2.__unorddf2(vector.a, vector.b) != vector.unReference) {
+ return false;
+ }
+ return true;
+}
+
+const arguments = []f64{
+ std.math.nan(f64),
+ -std.math.inf(f64),
+ -0x1.fffffffffffffp1023,
+ -0x1.0000000000001p0 - 0x1.0000000000000p0,
+ -0x1.fffffffffffffp-1,
+ -0x1.0000000000000p-1022,
+ -0x0.fffffffffffffp-1022,
+ -0x0.0000000000001p-1022,
+ -0.0,
+ 0.0,
+ 0x0.0000000000001p-1022,
+ 0x0.fffffffffffffp-1022,
+ 0x1.0000000000000p-1022,
+ 0x1.fffffffffffffp-1,
+ 0x1.0000000000000p0,
+ 0x1.0000000000001p0,
+ 0x1.fffffffffffffp1023,
+ std.math.inf(f64),
+};
+
+fn generateVector(comptime a: f64, comptime b: f64) TestVector {
+ const leResult = if (a < b) -1 else if (a == b) 0 else 1;
+ const geResult = if (a > b) 1 else if (a == b) 0 else -1;
+ const unResult = if (a != a or b != b) 1 else 0;
+ return TestVector{
+ .a = a,
+ .b = b,
+ .eqReference = leResult,
+ .geReference = geResult,
+ .gtReference = geResult,
+ .leReference = leResult,
+ .ltReference = leResult,
+ .neReference = leResult,
+ .unReference = unResult,
+ };
+}
+
+const test_vectors = init: {
+ @setEvalBranchQuota(10000);
+ var vectors: [arguments.len * arguments.len]TestVector = undefined;
+ for (arguments[0..]) |arg_i, i| {
+ for (arguments[0..]) |arg_j, j| {
+ vectors[(i * arguments.len) + j] = generateVector(arg_i, arg_j);
+ }
+ }
+ break :init vectors;
+};
+
+test "compare f64" {
+ for (test_vectors) |vector, i| {
+ std.testing.expect(test__cmpdf2(vector));
+ }
+}
std/special/compiler_rt.zig
@@ -6,26 +6,33 @@ comptime {
const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong;
@export("__lesf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage);
+ @export("__ledf2", @import("compiler_rt/comparedf2.zig").__ledf2, linkage);
@export("__letf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
@export("__gesf2", @import("compiler_rt/comparesf2.zig").__gesf2, linkage);
+ @export("__gedf2", @import("compiler_rt/comparedf2.zig").__gedf2, linkage);
@export("__getf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage);
if (!is_test) {
// only create these aliases when not testing
@export("__cmpsf2", @import("compiler_rt/comparesf2.zig").__lesf2, linkage);
+ @export("__cmpdf2", @import("compiler_rt/comparedf2.zig").__ledf2, linkage);
@export("__cmptf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
@export("__eqsf2", @import("compiler_rt/comparesf2.zig").__eqsf2, linkage);
+ @export("__eqdf2", @import("compiler_rt/comparedf2.zig").__eqdf2, linkage);
@export("__eqtf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
@export("__ltsf2", @import("compiler_rt/comparesf2.zig").__ltsf2, linkage);
+ @export("__ltdf2", @import("compiler_rt/comparedf2.zig").__ltdf2, linkage);
@export("__lttf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
@export("__nesf2", @import("compiler_rt/comparesf2.zig").__nesf2, linkage);
+ @export("__nedf2", @import("compiler_rt/comparedf2.zig").__nedf2, linkage);
@export("__netf2", @import("compiler_rt/comparetf2.zig").__letf2, linkage);
@export("__gtsf2", @import("compiler_rt/comparesf2.zig").__gtsf2, linkage);
+ @export("__gtdf2", @import("compiler_rt/comparedf2.zig").__gtdf2, linkage);
@export("__gttf2", @import("compiler_rt/comparetf2.zig").__getf2, linkage);
@export("__gnu_h2f_ieee", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage);
@@ -33,6 +40,7 @@ comptime {
}
@export("__unordsf2", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage);
+ @export("__unorddf2", @import("compiler_rt/comparedf2.zig").__unorddf2, linkage);
@export("__unordtf2", @import("compiler_rt/comparetf2.zig").__unordtf2, linkage);
@export("__addsf3", @import("compiler_rt/addXf3.zig").__addsf3, linkage);
@@ -165,6 +173,13 @@ comptime {
@export("__aeabi_fcmpge", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpge, linkage);
@export("__aeabi_fcmpgt", @import("compiler_rt/arm/aeabi_fcmp.zig").__aeabi_fcmpgt, linkage);
@export("__aeabi_fcmpun", @import("compiler_rt/comparesf2.zig").__unordsf2, linkage);
+
+ @export("__aeabi_dcmpeq", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpeq, linkage);
+ @export("__aeabi_dcmplt", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmplt, linkage);
+ @export("__aeabi_dcmple", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmple, linkage);
+ @export("__aeabi_dcmpge", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpge, linkage);
+ @export("__aeabi_dcmpgt", @import("compiler_rt/arm/aeabi_dcmp.zig").__aeabi_dcmpgt, linkage);
+ @export("__aeabi_dcmpun", @import("compiler_rt/comparedf2.zig").__unorddf2, linkage);
}
if (builtin.os == builtin.Os.windows) {
switch (builtin.arch) {
CMakeLists.txt
@@ -633,10 +633,12 @@ set(ZIG_STD_FILES
"special/builtin.zig"
"special/compiler_rt.zig"
"special/compiler_rt/arm/aeabi_fcmp.zig"
+ "special/compiler_rt/arm/aeabi_dcmp.zig"
"special/compiler_rt/addXf3.zig"
"special/compiler_rt/aulldiv.zig"
"special/compiler_rt/aullrem.zig"
"special/compiler_rt/comparetf2.zig"
+ "special/compiler_rt/comparedf2.zig"
"special/compiler_rt/comparesf2.zig"
"special/compiler_rt/divsf3.zig"
"special/compiler_rt/divdf3.zig"