From 773a555397684e602fc2a645f221babdfb0a5042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 6 Jul 2026 06:06:41 +0200 Subject: [PATCH] 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. --- lib/std/zig/system/loongarch.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/std/zig/system/loongarch.zig b/lib/std/zig/system/loongarch.zig index c5930340dea96e2aad563e82335030e0b38b61c6..eae8ac31df8f77c5acc66d31752d31ad6bbe3108 100644 --- a/lib/std/zig/system/loongarch.zig +++ b/lib/std/zig/system/loongarch.zig @@ -1,6 +1,13 @@ const builtin = @import("builtin"); const std = @import("std"); +const Variant = enum(u2) { + la32r = 0b00, + la32 = 0b01, + la64 = 0b10, + reserved = 0b11, +}; + inline fn bit(input: u32, offset: u5) bool { return (input >> offset) & 1 != 0; } @@ -19,10 +26,16 @@ pub fn detectNativeCpuAndFeatures( _ = os; _ = query; + const variant: Variant = @enumFromInt(cpucfg(1) & 0b11); + var cpu: std.Target.Cpu = .{ .arch = arch, - .model = switch (cpucfg(0) & 0xf000) { - else => return null, + .model = switch (cpucfg(0) & 0b1111000000000000) { + else => switch (variant) { + .la32r, .la32 => &std.Target.loongarch.cpu.generic_la32, + .la64 => &std.Target.loongarch.cpu.generic_la64, + .reserved => unreachable, + }, 0xc000 => &std.Target.loongarch.cpu.la464, 0xd000 => &std.Target.loongarch.cpu.la664, }, @@ -31,6 +44,8 @@ pub fn detectNativeCpuAndFeatures( cpu.features.addFeatureSet(cpu.model.features); + setFeature(&cpu, .@"32s", variant != .la32r); + const cfg2 = cpucfg(2); const cfg3 = cpucfg(3); -- 2.54.0