From 897df18573c951b49fb6421a9e1b711cabfeda67 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 16 Apr 2022 01:55:41 +0200 Subject: [PATCH] stage2: fix @mulAdd on aarch64 Darwin According to Apple docs, the long double type is a double precision IEEE754 binary floating-point type, which makes it identical to the double type. This behavior contrasts to the standard specification, in which a long double is a quad-precision, IEEE754 binary, floating-point type. Thus, we need to take this into account when using the compiler intrinsics so that we select the correct function version for FloatMulAdd. --- lib/std/target.zig | 11 ++++++++++- src/stage1/target.cpp | 10 +++++++++- src/type.zig | 3 +-- test/behavior/cast.zig | 23 ++++++++++------------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/std/target.zig b/lib/std/target.zig index 45a9c985b550e91ac045b0cf329d7abdd829cfc4..95fbd7e4e7db188d8f9aa67d28979dac7febea1a 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1718,8 +1718,17 @@ pub const Target = struct { } return switch (F) { f128 => switch (target.cpu.arch) { + .aarch64 => { + // According to Apple's official guide: + // > The long double type is a double precision IEEE754 binary floating-point type, + // > which makes it identical to the double type. This behavior contrasts to the + // > standard specification, in which a long double is a quad-precision, IEEE754 + // > binary, floating-point type. + // https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms + return !target.isDarwin(); + }, + .riscv64, - .aarch64, .aarch64_be, .aarch64_32, .s390x, diff --git a/src/stage1/target.cpp b/src/stage1/target.cpp index 51aac6c07201d0c181d1b404f06d4ba401ce0c05..7ff300891103e830d0fa667ae9c57f8c93ac2493 100644 --- a/src/stage1/target.cpp +++ b/src/stage1/target.cpp @@ -1008,8 +1008,16 @@ bool target_long_double_is_f128(const ZigTarget *target) { return false; } switch (target->arch) { - case ZigLLVM_riscv64: case ZigLLVM_aarch64: + // According to Apple's official guide: + // > The long double type is a double precision IEEE754 binary floating-point type, + // > which makes it identical to the double type. This behavior contrasts to the + // > standard specification, in which a long double is a quad-precision, IEEE754 + // > binary, floating-point type. + // https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms + return !target_os_is_darwin(target->os); + + case ZigLLVM_riscv64: case ZigLLVM_aarch64_be: case ZigLLVM_aarch64_32: case ZigLLVM_systemz: diff --git a/src/type.zig b/src/type.zig index b087f7ab23cce9d15e7097323dde72fe7952ce4b..72cf2cd5340308efc42ecab6b8638fe719c42e93 100644 --- a/src/type.zig +++ b/src/type.zig @@ -6155,7 +6155,6 @@ pub const CType = enum { }, .linux, - .macos, .freebsd, .netbsd, .dragonfly, @@ -6198,7 +6197,7 @@ pub const CType = enum { .longlong, .ulonglong, .longdouble => return 64, }, - .ios, .tvos, .watchos => switch (self) { + .macos, .ios, .tvos, .watchos => switch (self) { .short, .ushort => return 16, .int, .uint => return 32, .long, .ulong, .longlong, .ulonglong => return 64, diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 8793e7cb19af2a9f3229c9559f198a4312623b95..a1bd41e7b02e6c96c1556bd5a2414664a7bd4a48 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -79,19 +79,16 @@ test "comptime_int @intToFloat" { try expect(result == 1234.0); } - if (!((builtin.zig_backend == .stage2_aarch64 or builtin.zig_backend == .stage2_x86_64) and builtin.os.tag == .macos)) { - // TODO investigate why this traps on x86_64-macos and aarch64-macos - { - const result = @intToFloat(f128, 1234); - try expect(@TypeOf(result) == f128); - try expect(result == 1234.0); - } - // big comptime_int (> 64 bits) to f128 conversion - { - const result = @intToFloat(f128, 0x1_0000_0000_0000_0000); - try expect(@TypeOf(result) == f128); - try expect(result == 0x1_0000_0000_0000_0000.0); - } + { + const result = @intToFloat(f128, 1234); + try expect(@TypeOf(result) == f128); + try expect(result == 1234.0); + } + // big comptime_int (> 64 bits) to f128 conversion + { + const result = @intToFloat(f128, 0x1_0000_0000_0000_0000); + try expect(@TypeOf(result) == f128); + try expect(result == 0x1_0000_0000_0000_0000.0); } } -- 2.54.0