Skip to content

Commit f0e9ec3

Browse files
committed
std.Target: Add FloatAbi.hard32 and isSoft()/isHard() functions.
We need the extra ABI to represent the 3 float ABIs on RISC-V and LoongArch: * ilp32/lp64 (soft float) * ilp32f/lp64f (hard float for f32) * ilp32d/lp64d (hard float for f32 and f64)
1 parent 2876513 commit f0e9ec3

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

lib/compiler_rt/common.zig

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?
8383
/// here in compiler-rt.
8484
pub fn F16T(comptime OtherType: type) type {
8585
return switch (builtin.cpu.arch) {
86-
.arm, .armeb, .thumb, .thumbeb => if (std.Target.arm.featureSetHas(builtin.cpu.features, .has_v8))
87-
switch (builtin.abi.floatAbi()) {
88-
.soft => u16,
89-
.hard => f16,
90-
}
91-
else
92-
u16,
86+
.arm, .armeb, .thumb, .thumbeb,
87+
=> if (std.Target.arm.featureSetHas(builtin.cpu.features, .has_v8) and builtin.abi.isHardFloat()) f16 else u16,
9388
.aarch64, .aarch64_be, .aarch64_32 => f16,
9489
.riscv64 => if (builtin.zig_backend == .stage1) u16 else f16,
9590
.x86, .x86_64 => if (builtin.target.isDarwin()) switch (OtherType) {

lib/std/Target.zig

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,14 @@ pub const Abi = enum {
755755
else => .soft,
756756
};
757757
}
758+
759+
pub inline fn isSoftFloat(abi: Abi) bool {
760+
return abi.floatAbi().isSoft();
761+
}
762+
763+
pub inline fn isHardFloat(abi: Abi) bool {
764+
return abi.floatAbi().isHard();
765+
}
758766
};
759767

760768
pub const ObjectFormat = enum {
@@ -1645,15 +1653,41 @@ pub inline fn isSpirV(target: Target) bool {
16451653
return target.cpu.arch.isSpirV();
16461654
}
16471655

1656+
/// Most architectures only make the distinction between soft float and hard float, and assume that
1657+
/// hard float implies that both `f32` and `f64` have hardware support, while soft float implies
1658+
/// that both are emulated. We use `.soft` and `.hard` for these cases.
1659+
///
1660+
/// Some architectures, however, have three floating point ABIs: Soft float, hard float for just
1661+
/// `f32`, and hard float for both `f32` and `f64`. Here, hard float for both is usually the norm,
1662+
/// with hard float for just `f32` being more common in embedded scenarios. This is the case for
1663+
/// RISC-V and LoongArch, for example. We use `.hard32` to refer to the case where only `f32` has
1664+
/// hardware support.
16481665
pub const FloatAbi = enum {
1649-
hard,
16501666
soft,
1667+
hard,
1668+
hard32,
1669+
1670+
pub inline fn isSoft(abi: FloatAbi) bool {
1671+
return abi == .soft;
1672+
}
1673+
1674+
pub inline fn isHard(abi: FloatAbi) bool {
1675+
return !abi.isSoft();
1676+
}
16511677
};
16521678

1653-
pub inline fn getFloatAbi(target: Target) FloatAbi {
1679+
pub inline fn floatAbi(target: Target) FloatAbi {
16541680
return target.abi.floatAbi();
16551681
}
16561682

1683+
pub inline fn isSoftFloat(target: Target) bool {
1684+
return target.floatAbi().isSoft();
1685+
}
1686+
1687+
pub inline fn isHardFloat(target: Target) bool {
1688+
return target.floatAbi().isHard();
1689+
}
1690+
16571691
pub inline fn hasDynamicLinker(target: Target) bool {
16581692
if (target.cpu.arch.isWasm()) {
16591693
return false;
@@ -1733,7 +1767,7 @@ pub const DynamicLinker = struct {
17331767
.thumbeb => .armeb,
17341768
else => cpu.arch,
17351769
}),
1736-
if (cpu.arch.isArmOrThumb() and abi.floatAbi() == .hard) "hf" else "",
1770+
if (cpu.arch.isArmOrThumb() and abi.isHardFloat()) "hf" else "",
17371771
}) catch unreachable else switch (os_tag) {
17381772
.freebsd => init("/libexec/ld-elf.so.1"),
17391773
.netbsd => init("/libexec/ld.elf_so"),
@@ -1754,10 +1788,7 @@ pub const DynamicLinker = struct {
17541788
.armeb,
17551789
.thumb,
17561790
.thumbeb,
1757-
=> initFmt("/lib/ld-linux{s}.so.3", .{switch (abi.floatAbi()) {
1758-
.hard => "-armhf",
1759-
else => "",
1760-
}}) catch unreachable,
1791+
=> initFmt("/lib/ld-linux{s}.so.3", .{if (abi.isHardFloat()) "-armhf" else ""}) catch unreachable,
17611792

17621793
.mips,
17631794
.mipsel,

src/libunwind.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
131131
if (!comp.config.any_non_single_threaded) {
132132
try cflags.append("-D_LIBUNWIND_HAS_NO_THREADS");
133133
}
134-
if (target.cpu.arch.isARM() and target.abi.floatAbi() == .hard) {
134+
if (target.cpu.arch.isARM() and target.isHardFloat()) {
135135
try cflags.append("-DCOMPILER_RT_ARMHF_TARGET");
136136
}
137137
try cflags.append("-Wno-bitwise-conditional-parentheses");

0 commit comments

Comments
 (0)