authorgravatar for bartimaeusnek@noreply.codeberg.orgbartimaeusnek <bartimaeusnek@noreply.codeberg.org> 2026-01-22 19:18:47+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-01-22 19:18:47+01:00
log37288e53ae4e617a56fa79d5b718f3b66957d4f5
tree133c099e48dc172441fc191cf9f703f96d77105c
parentc8edfbe1d580fe3bfa0f95d0ec3a930cedce8711

std.zig.system.loongarch: implement individual cpu feature bit tests (#30915)

closes #30902 Co-authored-by: bartimaeusnek <33183715+bartimaeusnek@users.noreply.github.com> Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30915 Reviewed-by: Alex Rønne Petersen <alex@alexrp.com> Co-authored-by: bartimaeusnek <bartimaeusnek@noreply.codeberg.org> Co-committed-by: bartimaeusnek <bartimaeusnek@noreply.codeberg.org>

1 files changed, 35 insertions(+), 3 deletions(-)

lib/std/zig/system/loongarch.zig+35-3
......@@ -1,6 +1,16 @@
11const builtin = @import("builtin");
22const std = @import("std");
33
4inline fn bit(input: u32, offset: u5) bool {
5 return (input >> offset) & 1 != 0;
6}
7
8fn setFeature(cpu: *std.Target.Cpu, feature: std.Target.loongarch.Feature, enabled: bool) void {
9 const idx = @as(std.Target.Cpu.Feature.Set.Index, @intFromEnum(feature));
10
11 if (enabled) cpu.features.addFeature(idx) else cpu.features.removeFeature(idx);
12}
13
414pub fn detectNativeCpuAndFeatures(
515 arch: std.Target.Cpu.Arch,
616 os: std.Target.Os,
......@@ -9,9 +19,6 @@ pub fn detectNativeCpuAndFeatures(
919 _ = os;
1020 _ = query;
1121
12 // Clearly this code could do better in the future by actually querying specific CPU features
13 // with the cpucfg instruction like on x86. But with the small number of well-known LoongArch
14 // models that exist at the moment, simply checking the PRID is plenty.
1522 var cpu: std.Target.Cpu = .{
1623 .arch = arch,
1724 .model = switch (cpucfg(0) & 0xf000) {
......@@ -23,6 +30,31 @@ pub fn detectNativeCpuAndFeatures(
2330 };
2431
2532 cpu.features.addFeatureSet(cpu.model.features);
33
34 const cfg1 = cpucfg(1);
35 const cfg2 = cpucfg(2);
36 const cfg3 = cpucfg(3);
37
38 setFeature(&cpu, .ual, bit(cfg1, 20));
39
40 const has_fpu = bit(cfg2, 0);
41 setFeature(&cpu, .f, has_fpu and bit(cfg2, 1));
42 setFeature(&cpu, .d, has_fpu and bit(cfg2, 2));
43
44 setFeature(&cpu, .lsx, bit(cfg2, 6));
45 setFeature(&cpu, .lasx, bit(cfg2, 7));
46 setFeature(&cpu, .lvz, bit(cfg2, 10));
47
48 setFeature(&cpu, .lbt, bit(cfg2, 18) and bit(cfg2, 19) and bit(cfg2, 20));
49
50 setFeature(&cpu, .frecipe, bit(cfg2, 25));
51 setFeature(&cpu, .div32, bit(cfg2, 26));
52 setFeature(&cpu, .lam_bh, bit(cfg2, 27));
53 setFeature(&cpu, .lamcas, bit(cfg2, 28));
54 setFeature(&cpu, .scq, bit(cfg2, 30));
55
56 setFeature(&cpu, .ld_seq_sa, bit(cfg3, 23));
57
2658 cpu.features.populateDependencies(cpu.arch.allFeaturesList());
2759
2860 return cpu;