Merge pull request #2452 from LemonBoy/more-more-builtins

More more builtins
This commit is contained in:
Andrew Kelley 2019-05-08 22:24:12 -04:00 committed by GitHub
commit 4b9e12be50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 419 additions and 28 deletions

View File

@ -677,9 +677,13 @@ set(ZIG_STD_FILES
"special/compiler_rt/fixunstfdi.zig"
"special/compiler_rt/fixunstfsi.zig"
"special/compiler_rt/fixunstfti.zig"
"special/compiler_rt/floatdidf.zig"
"special/compiler_rt/floatsiXf.zig"
"special/compiler_rt/floatunsidf.zig"
"special/compiler_rt/floattidf.zig"
"special/compiler_rt/floattisf.zig"
"special/compiler_rt/floattitf.zig"
"special/compiler_rt/floatundidf.zig"
"special/compiler_rt/floatunditf.zig"
"special/compiler_rt/floatunsitf.zig"
"special/compiler_rt/floatuntidf.zig"

View File

@ -62,6 +62,13 @@ comptime {
@export("__divsf3", @import("compiler_rt/divsf3.zig").__divsf3, linkage);
@export("__divdf3", @import("compiler_rt/divdf3.zig").__divdf3, linkage);
@export("__floatsidf", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage);
@export("__floatsisf", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage);
@export("__floatdidf", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage);
@export("__floatsitf", @import("compiler_rt/floatsiXf.zig").__floatsitf, linkage);
@export("__floatunsidf", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage);
@export("__floatundidf", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage);
@export("__floattitf", @import("compiler_rt/floattitf.zig").__floattitf, linkage);
@export("__floattidf", @import("compiler_rt/floattidf.zig").__floattidf, linkage);
@export("__floattisf", @import("compiler_rt/floattisf.zig").__floattisf, linkage);
@ -82,6 +89,10 @@ comptime {
@export("__trunctfdf2", @import("compiler_rt/truncXfYf2.zig").__trunctfdf2, linkage);
@export("__trunctfsf2", @import("compiler_rt/truncXfYf2.zig").__trunctfsf2, linkage);
@export("__truncdfsf2", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage);
@export("__extendsfdf2", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage);
@export("__fixunssfsi", @import("compiler_rt/fixunssfsi.zig").__fixunssfsi, linkage);
@export("__fixunssfdi", @import("compiler_rt/fixunssfdi.zig").__fixunssfdi, linkage);
@export("__fixunssfti", @import("compiler_rt/fixunssfti.zig").__fixunssfti, linkage);
@ -107,6 +118,7 @@ comptime {
@export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage);
@export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage);
@export("__divmoddi4", __divmoddi4, linkage);
@export("__divsi3", __divsi3, linkage);
@export("__divdi3", __divdi3, linkage);
@export("__udivsi3", __udivsi3, linkage);
@ -122,10 +134,15 @@ comptime {
@export("__negdf2", @import("compiler_rt/negXf2.zig").__negdf2, linkage);
if (is_arm_arch and !is_arm_64) {
@export("__aeabi_unwind_cpp_pr0", __aeabi_unwind_cpp_pr0, strong_linkage);
@export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage);
@export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage);
@export("__aeabi_ldivmod", __aeabi_ldivmod, linkage);
@export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);
@export("__aeabi_idiv", __divsi3, linkage);
@export("__aeabi_idivmod", __divmodsi4, linkage);
@export("__aeabi_idivmod", __aeabi_idivmod, linkage);
@export("__aeabi_uidiv", __udivsi3, linkage);
@export("__aeabi_uidivmod", __aeabi_uidivmod, linkage);
@ -149,6 +166,12 @@ comptime {
@export("__aeabi_memcmp4", __aeabi_memcmp, linkage);
@export("__aeabi_memcmp8", __aeabi_memcmp, linkage);
@export("__aeabi_f2d", @import("compiler_rt/extendXfYf2.zig").__extendsfdf2, linkage);
@export("__aeabi_i2d", @import("compiler_rt/floatsiXf.zig").__floatsidf, linkage);
@export("__aeabi_l2d", @import("compiler_rt/floatdidf.zig").__floatdidf, linkage);
@export("__aeabi_ui2d", @import("compiler_rt/floatunsidf.zig").__floatunsidf, linkage);
@export("__aeabi_ul2d", @import("compiler_rt/floatundidf.zig").__floatundidf, linkage);
@export("__aeabi_fneg", @import("compiler_rt/negXf2.zig").__negsf2, linkage);
@export("__aeabi_dneg", @import("compiler_rt/negXf2.zig").__negdf2, linkage);
@ -168,6 +191,9 @@ comptime {
@export("__aeabi_h2f", @import("compiler_rt/extendXfYf2.zig").__extendhfsf2, linkage);
@export("__aeabi_f2h", @import("compiler_rt/truncXfYf2.zig").__truncsfhf2, linkage);
@export("__aeabi_i2f", @import("compiler_rt/floatsiXf.zig").__floatsisf, linkage);
@export("__aeabi_d2f", @import("compiler_rt/truncXfYf2.zig").__truncdfsf2, linkage);
@export("__aeabi_fadd", @import("compiler_rt/addXf3.zig").__addsf3, linkage);
@export("__aeabi_dadd", @import("compiler_rt/addXf3.zig").__adddf3, linkage);
@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
}
}
extern fn __aeabi_unwind_cpp_pr0() void {
unreachable;
}
extern fn __aeabi_unwind_cpp_pr1() void {
unreachable;
}
extern fn __aeabi_unwind_cpp_pr2() void {
unreachable;
}
extern fn __divmoddi4(a: i64, b: i64, rem: *i64) i64 {
@setRuntimeSafety(is_test);
const d = __divdi3(a, b);
rem.* = a -% (d *% b);
return d;
}
extern fn __divdi3(a: i64, b: i64) i64 {
@setRuntimeSafety(is_test);
@ -290,14 +334,35 @@ extern fn __umoddi3(a: u64, b: u64) u64 {
return r;
}
const AeabiUlDivModResult = extern struct {
quot: u64,
rem: u64,
};
extern fn __aeabi_uldivmod(numerator: u64, denominator: u64) AeabiUlDivModResult {
extern fn __aeabi_uidivmod(n: u32, d: u32) extern struct{q: u32, r: u32} {
@setRuntimeSafety(is_test);
var result: AeabiUlDivModResult = undefined;
result.quot = __udivmoddi4(numerator, denominator, &result.rem);
var result: @typeOf(__aeabi_uidivmod).ReturnType = undefined;
result.q = __udivmodsi4(n, d, &result.r);
return result;
}
extern fn __aeabi_uldivmod(n: u64, d: u64) extern struct{q: u64, r: u64} {
@setRuntimeSafety(is_test);
var result: @typeOf(__aeabi_uldivmod).ReturnType = undefined;
result.q = __udivmoddi4(n, d, &result.r);
return result;
}
extern fn __aeabi_idivmod(n: i32, d: i32) extern struct{q: i32, r: i32} {
@setRuntimeSafety(is_test);
var result: @typeOf(__aeabi_idivmod).ReturnType = undefined;
result.q = __divmodsi4(n, d, &result.r);
return result;
}
extern fn __aeabi_ldivmod(n: i64, d: i64) extern struct{q: i64, r:i64} {
@setRuntimeSafety(is_test);
var result: @typeOf(__aeabi_ldivmod).ReturnType = undefined;
result.q = __divmoddi4(n, d, &result.r);
return result;
}
@ -391,22 +456,6 @@ test "usesThumb1" {
//etc.
}
nakedcc fn __aeabi_uidivmod() void {
@setRuntimeSafety(false);
asm volatile (
\\ push { lr }
\\ sub sp, sp, #4
\\ mov r2, sp
\\ bl __udivmodsi4
\\ ldr r1, [sp]
\\ add sp, sp, #4
\\ pop { pc }
:
:
: "r2", "r1"
);
}
nakedcc fn __aeabi_memcpy() noreturn {
@setRuntimeSafety(false);
if (use_thumb_1) {

View File

@ -2,21 +2,27 @@ const std = @import("std");
const builtin = @import("builtin");
const is_test = builtin.is_test;
pub extern fn __extendsfdf2(a: f32) f64 {
return @inlineCall(extendXfYf2, f64, f32, @bitCast(u32, a));
}
pub extern fn __extenddftf2(a: f64) f128 {
return extendXfYf2(f128, f64, @bitCast(u64, a));
return @inlineCall(extendXfYf2, f128, f64, @bitCast(u64, a));
}
pub extern fn __extendsftf2(a: f32) f128 {
return extendXfYf2(f128, f32, @bitCast(u32, a));
return @inlineCall(extendXfYf2, f128, f32, @bitCast(u32, a));
}
pub extern fn __extendhfsf2(a: u16) f32 {
return extendXfYf2(f32, f16, a);
return @inlineCall(extendXfYf2, f32, f16, a);
}
const CHAR_BIT = 8;
inline fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t {
fn extendXfYf2(comptime dst_t: type, comptime src_t: type, a: @IntType(false, @typeInfo(src_t).Float.bits)) dst_t {
@setRuntimeSafety(builtin.is_test);
const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits);
const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits);
const srcSigBits = std.math.floatMantissaBits(src_t);

View File

@ -0,0 +1,22 @@
const builtin = @import("builtin");
const std = @import("std");
const twop52: f64 = 0x1.0p52;
const twop32: f64 = 0x1.0p32;
pub extern fn __floatdidf(a: i64) f64 {
@setRuntimeSafety(builtin.is_test);
if (a == 0) return 0;
var low = @bitCast(i64, twop52);
const high = @intToFloat(f64, @truncate(i32, a >> 32)) * twop32;
low |= @bitCast(i64, a & 0xFFFFFFFF);
return (high - twop52) + @bitCast(f64, low);
}
test "import floatdidf" {
_ = @import("floatdidf_test.zig");
}

View File

@ -0,0 +1,53 @@
const __floatdidf = @import("floatdidf.zig").__floatdidf;
const testing = @import("std").testing;
fn test__floatdidf(a: i64, expected: f64) void {
const r = __floatdidf(a);
testing.expect(r == expected);
}
test "floatdidf" {
test__floatdidf(0, 0.0);
test__floatdidf(1, 1.0);
test__floatdidf(2, 2.0);
test__floatdidf(20, 20.0);
test__floatdidf(-1, -1.0);
test__floatdidf(-2, -2.0);
test__floatdidf(-20, -20.0);
test__floatdidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
test__floatdidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62);
test__floatdidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
test__floatdidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000008000000000)), -0x1.FFFFFEp+62);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000800)), -0x1.FFFFFFFFFFFFEp+62);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000010000000000)), -0x1.FFFFFCp+62);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000001000)), -0x1.FFFFFFFFFFFFCp+62);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000000)), -0x1.000000p+63);
test__floatdidf(@bitCast(i64, @intCast(u64, 0x8000000000000001)), -0x1.000000p+63);
test__floatdidf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
test__floatdidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50);
test__floatdidf(0x0007FB72EB000000, 0x1.FEDCBACp+50);
test__floatdidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50);
test__floatdidf(0x0007FB72EC000000, 0x1.FEDCBBp+50);
test__floatdidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50);
test__floatdidf(0x0007FB72E6000000, 0x1.FEDCB98p+50);
test__floatdidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50);
test__floatdidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50);
test__floatdidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50);
test__floatdidf(0x0007FB72E4000000, 0x1.FEDCB9p+50);
test__floatdidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57);
test__floatdidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57);
test__floatdidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57);
test__floatdidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57);
test__floatdidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57);
test__floatdidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57);
}

View File

@ -0,0 +1,109 @@
const builtin = @import("builtin");
const std = @import("std");
const maxInt = std.math.maxInt;
fn floatsiXf(comptime T: type, a: i32) T {
@setRuntimeSafety(builtin.is_test);
const Z = @IntType(false, T.bit_count);
const S = @IntType(false, T.bit_count - @clz(Z(T.bit_count) - 1));
if (a == 0) {
return T(0.0);
}
const significandBits = std.math.floatMantissaBits(T);
const exponentBits = std.math.floatExponentBits(T);
const exponentBias = ((1 << exponentBits - 1) - 1);
const implicitBit = Z(1) << significandBits;
const signBit = Z(1 << Z.bit_count - 1);
const sign = a >> 31;
// Take absolute value of a via abs(x) = (x^(x >> 31)) - (x >> 31).
const abs_a = (a ^ sign) -% sign;
// The exponent is the width of abs(a)
const exp = Z(31 - @clz(abs_a));
const sign_bit = if (sign < 0) signBit else 0;
var mantissa: Z = undefined;
// Shift a into the significand field and clear the implicit bit.
if (exp <= significandBits) {
// No rounding needed
const shift = @intCast(S, significandBits - exp);
mantissa = @intCast(Z, @bitCast(u32, abs_a)) << shift ^ implicitBit;
} else {
const shift = @intCast(S, exp - significandBits);
// Round to the nearest number after truncation
mantissa = @intCast(Z, @bitCast(u32, abs_a)) >> shift ^ implicitBit;
// Align to the left and check if the truncated part is halfway over
const round = @bitCast(u32, abs_a) << @intCast(u5, 31 - shift);
mantissa += @boolToInt(round > 0x80000000);
// Tie to even
mantissa += mantissa & 1;
}
// Use the addition instead of a or since we may have a carry from the
// mantissa to the exponent
var result = mantissa;
result += (exp + exponentBias) << significandBits;
result += sign_bit;
return @bitCast(T, result);
}
pub extern fn __floatsisf(arg: i32) f32 {
@setRuntimeSafety(builtin.is_test);
return @inlineCall(floatsiXf, f32, arg);
}
pub extern fn __floatsidf(arg: i32) f64 {
@setRuntimeSafety(builtin.is_test);
return @inlineCall(floatsiXf, f64, arg);
}
pub extern fn __floatsitf(arg: i32) f128 {
@setRuntimeSafety(builtin.is_test);
return @inlineCall(floatsiXf, f128, arg);
}
fn test_one_floatsitf(a: i32, expected: u128) void {
const r = __floatsitf(a);
std.testing.expect(@bitCast(u128, r) == expected);
}
fn test_one_floatsidf(a: i32, expected: u64) void {
const r = __floatsidf(a);
std.testing.expect(@bitCast(u64, r) == expected);
}
fn test_one_floatsisf(a: i32, expected: u32) void {
const r = __floatsisf(a);
std.testing.expect(@bitCast(u32, r) == expected);
}
test "floatsidf" {
test_one_floatsidf(0, 0x0000000000000000);
test_one_floatsidf(1, 0x3ff0000000000000);
test_one_floatsidf(-1, 0xbff0000000000000);
test_one_floatsidf(0x7FFFFFFF, 0x41dfffffffc00000);
test_one_floatsidf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc1e0000000000000);
}
test "floatsisf" {
test_one_floatsisf(0, 0x00000000);
test_one_floatsisf(1, 0x3f800000);
test_one_floatsisf(-1, 0xbf800000);
test_one_floatsisf(0x7FFFFFFF, 0x4f000000);
test_one_floatsisf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xcf000000);
}
test "floatsitf" {
test_one_floatsitf(0, 0);
test_one_floatsitf(0x7FFFFFFF, 0x401dfffffffc00000000000000000000);
test_one_floatsitf(0x12345678, 0x401b2345678000000000000000000000);
test_one_floatsitf(-0x12345678, 0xc01b2345678000000000000000000000);
test_one_floatsitf(@bitCast(i32, @intCast(u32, 0xffffffff)), 0xbfff0000000000000000000000000000);
test_one_floatsitf(@bitCast(i32, @intCast(u32, 0x80000000)), 0xc01e0000000000000000000000000000);
}

View File

@ -0,0 +1,24 @@
const builtin = @import("builtin");
const std = @import("std");
const twop52: f64 = 0x1.0p52;
const twop84: f64 = 0x1.0p84;
const twop84_plus_twop52: f64 = 0x1.00000001p84;
pub extern fn __floatundidf(a: u64) f64 {
@setRuntimeSafety(builtin.is_test);
if (a == 0) return 0;
var high = @bitCast(u64, twop84);
var low = @bitCast(u64, twop52);
high |= a >> 32;
low |= a & 0xFFFFFFFF;
return (@bitCast(f64, high) - twop84_plus_twop52) + @bitCast(f64, low);
}
test "import floatundidf" {
_ = @import("floatundidf_test.zig");
}

View File

@ -0,0 +1,50 @@
const __floatundidf = @import("floatundidf.zig").__floatundidf;
const testing = @import("std").testing;
fn test__floatundidf(a: u64, expected: f64) void {
const r = __floatundidf(a);
testing.expect(r == expected);
}
test "floatundidf" {
test__floatundidf(0, 0.0);
test__floatundidf(1, 1.0);
test__floatundidf(2, 2.0);
test__floatundidf(20, 20.0);
test__floatundidf(0x7FFFFF8000000000, 0x1.FFFFFEp+62);
test__floatundidf(0x7FFFFFFFFFFFF800, 0x1.FFFFFFFFFFFFEp+62);
test__floatundidf(0x7FFFFF0000000000, 0x1.FFFFFCp+62);
test__floatundidf(0x7FFFFFFFFFFFF000, 0x1.FFFFFFFFFFFFCp+62);
test__floatundidf(0x8000008000000000, 0x1.000001p+63);
test__floatundidf(0x8000000000000800, 0x1.0000000000001p+63);
test__floatundidf(0x8000010000000000, 0x1.000002p+63);
test__floatundidf(0x8000000000001000, 0x1.0000000000002p+63);
test__floatundidf(0x8000000000000000, 0x1p+63);
test__floatundidf(0x8000000000000001, 0x1p+63);
test__floatundidf(0x0007FB72E8000000, 0x1.FEDCBAp+50);
test__floatundidf(0x0007FB72EA000000, 0x1.FEDCBA8p+50);
test__floatundidf(0x0007FB72EB000000, 0x1.FEDCBACp+50);
test__floatundidf(0x0007FB72EBFFFFFF, 0x1.FEDCBAFFFFFFCp+50);
test__floatundidf(0x0007FB72EC000000, 0x1.FEDCBBp+50);
test__floatundidf(0x0007FB72E8000001, 0x1.FEDCBA0000004p+50);
test__floatundidf(0x0007FB72E6000000, 0x1.FEDCB98p+50);
test__floatundidf(0x0007FB72E7000000, 0x1.FEDCB9Cp+50);
test__floatundidf(0x0007FB72E7FFFFFF, 0x1.FEDCB9FFFFFFCp+50);
test__floatundidf(0x0007FB72E4000001, 0x1.FEDCB90000004p+50);
test__floatundidf(0x0007FB72E4000000, 0x1.FEDCB9p+50);
test__floatundidf(0x023479FD0E092DC0, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DA1, 0x1.1A3CFE870496Dp+57);
test__floatundidf(0x023479FD0E092DB0, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DB8, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DB6, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DBF, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DC1, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DC7, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DC8, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DCF, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DD0, 0x1.1A3CFE870496Ep+57);
test__floatundidf(0x023479FD0E092DD1, 0x1.1A3CFE870496Fp+57);
test__floatundidf(0x023479FD0E092DD8, 0x1.1A3CFE870496Fp+57);
test__floatundidf(0x023479FD0E092DDF, 0x1.1A3CFE870496Fp+57);
test__floatundidf(0x023479FD0E092DE0, 0x1.1A3CFE870496Fp+57);
}

View File

@ -0,0 +1,33 @@
const builtin = @import("builtin");
const std = @import("std");
const maxInt = std.math.maxInt;
const implicitBit = u64(1) << 52;
pub extern fn __floatunsidf(arg: u32) f64 {
@setRuntimeSafety(builtin.is_test);
if (arg == 0) return 0.0;
// The exponent is the width of abs(a)
const exp = u64(31) - @clz(arg);
// Shift a into the significand field and clear the implicit bit
const shift = @intCast(u6, 52 - exp);
const mant = u64(arg) << shift ^ implicitBit;
return @bitCast(f64, mant | (exp + 1023) << 52);
}
fn test_one_floatunsidf(a: u32, expected: u64) void {
const r = __floatunsidf(a);
std.testing.expect(@bitCast(u64, r) == expected);
}
test "floatsidf" {
// Test the produced bit pattern
test_one_floatunsidf(0, 0x0000000000000000);
test_one_floatunsidf(1, 0x3ff0000000000000);
test_one_floatunsidf(0x7FFFFFFF, 0x41dfffffffc00000);
test_one_floatunsidf(@intCast(u32, 0x80000000), 0x41e0000000000000);
test_one_floatunsidf(@intCast(u32, 0xFFFFFFFF), 0x41efffffffe00000);
}

View File

@ -16,6 +16,10 @@ pub extern fn __trunctfdf2(a: f128) f64 {
return truncXfYf2(f64, f128, a);
}
pub extern fn __truncdfsf2(a: f64) f32 {
return truncXfYf2(f32, f64, a);
}
inline fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t {
const src_rep_t = @IntType(false, @typeInfo(src_t).Float.bits);
const dst_rep_t = @IntType(false, @typeInfo(dst_t).Float.bits);

View File

@ -200,3 +200,40 @@ test "trunctfdf2" {
test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000);
test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab);
}
const __truncdfsf2 = @import("truncXfYf2.zig").__truncdfsf2;
fn test__truncdfsf2(a: f64, expected: u32) void {
const x = __truncdfsf2(a);
const rep = @bitCast(u32, x);
if (rep == expected) {
return;
}
// test other possible NaN representation(signal NaN)
else if (expected == 0x7fc00000) {
if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
return;
}
}
@import("std").debug.warn("got 0x{x} wanted 0x{x}\n", rep, expected);
@panic("__trunctfsf2 test failure");
}
test "truncdfsf2" {
// nan & qnan
test__truncdfsf2(@bitCast(f64, u64(0x7ff8000000000000)), 0x7fc00000);
test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000001)), 0x7fc00000);
// inf
test__truncdfsf2(@bitCast(f64, u64(0x7ff0000000000000)), 0x7f800000);
test__truncdfsf2(@bitCast(f64, u64(0xfff0000000000000)), 0xff800000);
test__truncdfsf2(0.0, 0x0);
test__truncdfsf2(1.0, 0x3f800000);
test__truncdfsf2(-1.0, 0xbf800000);
// huge number becomes inf
test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);
}