add compiler_rt functions for f128

* __letf2
 * __cmptf2
 * __getf2
 * __unordtf2
 * __eqtf2
 * __lttf2
 * __netf2
 * __gttf2
This commit is contained in:
Andrew Kelley 2017-08-17 19:07:23 -04:00
parent 0d117bb0a9
commit e63d864c1e
5 changed files with 148 additions and 9 deletions

View File

@ -311,6 +311,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG
install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_file_template.zig" DESTINATION "${ZIG_STD_DEST}/special")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/comparetf2.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixuint.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunsdfdi.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/fixunsdfsi.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")

View File

@ -0,0 +1,120 @@
// TODO https://github.com/zig-lang/zig/issues/305
// 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);
const rep_t = u128;
const srep_t = i128;
const typeWidth = rep_t.bit_count;
const significandBits = 112;
const exponentBits = (typeWidth - significandBits - 1);
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 = exponentMask;
export fn __letf2(a: f128, b: f128) -> c_int {
const aInt = @bitCast(rep_t, a);
const bInt = @bitCast(rep_t, b);
const aAbs: rep_t = aInt & absMask;
const bAbs: 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 floating-point compare.
return if ((aInt & bInt) >= 0) {
if (aInt < bInt) {
LE_LESS
} else if (aInt == bInt) {
LE_EQUAL
} else {
LE_GREATER
}
} else {
// 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).
if (aInt > bInt) {
LE_LESS
} else if (aInt == bInt) {
LE_EQUAL
} else {
LE_GREATER
}
};
}
// Alias for libgcc compatibility
// TODO https://github.com/zig-lang/zig/issues/420
export fn __cmptf2(a: f128, b: f128) -> c_int { __letf2(a, b) }
// TODO https://github.com/zig-lang/zig/issues/305
// 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
export fn __getf2(a: f128, b: f128) -> c_int {
const aInt = @bitCast(srep_t, a);
const bInt = @bitCast(srep_t, b);
const aAbs = @bitCast(rep_t, aInt) & absMask;
const bAbs = @bitCast(rep_t, bInt) & absMask;
if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
if ((aAbs | bAbs) == 0) return GE_EQUAL;
return if ((aInt & bInt) >= 0) {
if (aInt < bInt) {
GE_LESS
} else if (aInt == bInt) {
GE_EQUAL
} else {
GE_GREATER
}
} else {
if (aInt > bInt) {
GE_LESS
} else if (aInt == bInt) {
GE_EQUAL
} else {
GE_GREATER
}
};
}
export fn __unordtf2(a: f128, b: f128) -> c_int {
const aAbs = @bitCast(rep_t, a) & absMask;
const bAbs = @bitCast(rep_t, b) & absMask;
return c_int(aAbs > infRep or bAbs > infRep);
}
// The following are alternative names for the preceding routines.
export fn __eqtf2(a: f128, b: f128) -> c_int {
return __letf2(a, b);
}
export fn __lttf2(a: f128, b: f128) -> c_int {
return __letf2(a, b);
}
export fn __netf2(a: f128, b: f128) -> c_int {
return __letf2(a, b);
}
export fn __gttf2(a: f128, b: f128) -> c_int {
return __getf2(a, b);
}

View File

@ -36,14 +36,15 @@ test "fixunstfdi" {
test__fixunstfdi(-0x1.FFFFFFFFFFFFFp+62, 0);
test__fixunstfdi(-0x1.FFFFFFFFFFFFEp+62, 0);
test__fixunstfdi(0x1.FFFFFFFFFFFFFFFEp+63, 0xFFFFFFFFFFFFFFFF);
test__fixunstfdi(0x1.0000000000000002p+63, 0x8000000000000001);
test__fixunstfdi(0x1.0000000000000000p+63, 0x8000000000000000);
test__fixunstfdi(0x1.FFFFFFFFFFFFFFFCp+62, 0x7FFFFFFFFFFFFFFF);
test__fixunstfdi(0x1.FFFFFFFFFFFFFFF8p+62, 0x7FFFFFFFFFFFFFFE);
test__fixunstfdi(0x1.p+64, 0xFFFFFFFFFFFFFFFF);
// TODO enable these tests when we can parse f128 float literals
//test__fixunstfdi(0x1.FFFFFFFFFFFFFFFEp+63, 0xFFFFFFFFFFFFFFFF);
//test__fixunstfdi(0x1.0000000000000002p+63, 0x8000000000000001);
//test__fixunstfdi(0x1.0000000000000000p+63, 0x8000000000000000);
//test__fixunstfdi(0x1.FFFFFFFFFFFFFFFCp+62, 0x7FFFFFFFFFFFFFFF);
//test__fixunstfdi(0x1.FFFFFFFFFFFFFFF8p+62, 0x7FFFFFFFFFFFFFFE);
//test__fixunstfdi(0x1.p+64, 0xFFFFFFFFFFFFFFFF);
test__fixunstfdi(-0x1.0000000000000000p+63, 0);
test__fixunstfdi(-0x1.FFFFFFFFFFFFFFFCp+62, 0);
test__fixunstfdi(-0x1.FFFFFFFFFFFFFFF8p+62, 0);
//test__fixunstfdi(-0x1.0000000000000000p+63, 0);
//test__fixunstfdi(-0x1.FFFFFFFFFFFFFFFCp+62, 0);
//test__fixunstfdi(-0x1.FFFFFFFFFFFFFFF8p+62, 0);
}

View File

@ -1,5 +1,6 @@
// Find all the exported functions.
comptime {
_ = @import("comparetf2.zig");
_ = @import("fixunsdfdi.zig");
_ = @import("fixunsdfsi.zig");
_ = @import("fixunsdfti.zig");

View File

@ -312,3 +312,19 @@ test "big number multiplication" {
232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
}
}
test "f128" {
test_f128();
comptime test_f128();
}
fn make_f128(x: f128) -> f128 { x }
fn test_f128() {
assert(@sizeOf(f128) == 16);
assert(make_f128(1.0) == 1.0);
assert(make_f128(1.0) != 1.1);
assert(make_f128(1.0) > 0.9);
assert(make_f128(1.0) >= 0.9);
assert(make_f128(1.0) >= 1.0);
}