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 @@
11const builtin = @import("builtin");
22const std = @import("std");
33
4const Variant = enum(u2) {
5 la32r = 0b00,
6 la32 = 0b01,
7 la64 = 0b10,
8 reserved = 0b11,
9};
10
411inline fn bit(input: u32, offset: u5) bool {
512 return (input >> offset) & 1 != 0;
613}
......@@ -19,10 +26,16 @@ pub fn detectNativeCpuAndFeatures(
1926 _ = os;
2027 _ = query;
2128
29 const variant: Variant = @enumFromInt(cpucfg(1) & 0b11);
30
2231 var cpu: std.Target.Cpu = .{
2332 .arch = arch,
24 .model = switch (cpucfg(0) & 0xf000) {
25 else => return null,
33 .model = switch (cpucfg(0) & 0b1111000000000000) {
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 },
2639 0xc000 => &std.Target.loongarch.cpu.la464,
2740 0xd000 => &std.Target.loongarch.cpu.la664,
2841 },
......@@ -31,6 +44,8 @@ pub fn detectNativeCpuAndFeatures(
3144
3245 cpu.features.addFeatureSet(cpu.model.features);
3346
47 setFeature(&cpu, .@"32s", variant != .la32r);
48
3449 const cfg2 = cpucfg(2);
3550 const cfg3 = cpucfg(3);
3651