authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-07-06 06:06:41+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-07-06 23:27:58+02:00
log773a555397684e602fc2a645f221babdfb0a5042
tree52f02a114c80edc6a883942dbe3ead4c233e91c1
parent3e6b2c6b03eacd311de2579b52f5ed9b83e57a02
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.zig.system.loongarch: use generic CPU model if we can't determine the model

In principle, there's no reason why we shouldn't be able to proceed with CPU feature detection just because we can't determine the exact CPU model. This is relevant when new silicon appears in the wild and Zig hasn't yet been updated to recognize it.

1 files changed, 17 insertions(+), 2 deletions(-)

lib/std/zig/system/loongarch.zig+17-2
...@@ -1,6 +1,13 @@...@@ -1,6 +1,13 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
33
4const Variant = enum(u2) {
5 la32r = 0b00,
6 la32 = 0b01,
7 la64 = 0b10,
8 reserved = 0b11,
9};
10
4inline fn bit(input: u32, offset: u5) bool {11inline fn bit(input: u32, offset: u5) bool {
5 return (input >> offset) & 1 != 0;12 return (input >> offset) & 1 != 0;
6}13}
...@@ -19,10 +26,16 @@ pub fn detectNativeCpuAndFeatures(...@@ -19,10 +26,16 @@ pub fn detectNativeCpuAndFeatures(
19 _ = os;26 _ = os;
20 _ = query;27 _ = query;
2128
29 const variant: Variant = @enumFromInt(cpucfg(1) & 0b11);
30
22 var cpu: std.Target.Cpu = .{31 var cpu: std.Target.Cpu = .{
23 .arch = arch,32 .arch = arch,
24 .model = switch (cpucfg(0) & 0xf000) {33 .model = switch (cpucfg(0) & 0b1111000000000000) {
25 else => return null,34 else => switch (variant) {
35 .la32r, .la32 => &std.Target.loongarch.cpu.generic_la32,
36 .la64 => &std.Target.loongarch.cpu.generic_la64,
37 .reserved => unreachable,
38 },
26 0xc000 => &std.Target.loongarch.cpu.la464,39 0xc000 => &std.Target.loongarch.cpu.la464,
27 0xd000 => &std.Target.loongarch.cpu.la664,40 0xd000 => &std.Target.loongarch.cpu.la664,
28 },41 },
...@@ -31,6 +44,8 @@ pub fn detectNativeCpuAndFeatures(...@@ -31,6 +44,8 @@ pub fn detectNativeCpuAndFeatures(
3144
32 cpu.features.addFeatureSet(cpu.model.features);45 cpu.features.addFeatureSet(cpu.model.features);
3346
47 setFeature(&cpu, .@"32s", variant != .la32r);
48
34 const cfg2 = cpucfg(2);49 const cfg2 = cpucfg(2);
35 const cfg3 = cpucfg(3);50 const cfg3 = cpucfg(3);
3651