我想链接并使用 cglm C 库。我正在使用 Zig 0.7.1 和 Zig 0.8.0 (master) 在没有 msvc 的 Windows 上工作(因此针对 gnu C ABI),没有任何运气。
我已经能够从 Zig build.zig 构建 CGLM 静态库,但没有运气从 Zig 程序链接它,因为报告了 CGLM SIMD 内在优化错误。
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
// cglm
const cglmLibBasePath = "vendor/cglm/";
const cglmFiles = &[_][]const u8{
cglmLibBasePath ++ "src/euler.c",
cglmLibBasePath ++ "src/affine.c",
cglmLibBasePath ++ "src/io.c",
cglmLibBasePath ++ "src/quat.c",
cglmLibBasePath ++ "src/cam.c",
cglmLibBasePath ++ "src/vec2.c",
cglmLibBasePath ++ "src/vec3.c",
cglmLibBasePath ++ "src/vec4.c",
cglmLibBasePath ++ "src/mat2.c",
cglmLibBasePath ++ "src/mat3.c",
cglmLibBasePath ++ "src/mat4.c",
cglmLibBasePath ++ "src/plane.c",
cglmLibBasePath ++ "src/frustum.c",
cglmLibBasePath ++ "src/box.c",
cglmLibBasePath ++ "src/project.c",
cglmLibBasePath ++ "src/sphere.c",
cglmLibBasePath ++ "src/ease.c",
cglmLibBasePath ++ "src/curve.c",
cglmLibBasePath ++ "src/bezier.c",
cglmLibBasePath ++ "src/ray.c",
cglmLibBasePath ++ "src/affine2d.c",
};
const cglmLib = b.addStaticLibrary("cglm", null);
cglmLib.setBuildMode(mode);
cglmLib.setTarget(target);
cglmLib.defineCMacro("CGLM_STATIC");
cglmLib.defineCMacro("WIN32");
cglmLib.addIncludeDir(cglmLibBasePath ++ "src/");
for (cglmFiles) |cglmFile| {
cglmLib.addCSourceFile(cglmFile, &[_][]const u8 {
"-std=c11",
"-Wall",
"-Werror",
"-O3",
});
}
cglmLib.linkLibC();
cglmLib.install();
const exe = b.addExecutable("app-zig-cglm", "src/main.zig");
exe.setBuildMode(mode);
exe.setTarget(target);
// cglm
exe.linkLibrary(cglmLib);
exe.addSystemIncludeDir(cglmLibBasePath ++ "include/");
// C and win32
exe.linkLibC();
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
当我尝试使用链接的生成库构建/运行 exe 时,会报告以下错误(在 Zig 版本 0.7.1 和 0.8.0 master 中类似)Ex 来自 0.7.1 Zig 版本。
alagt@LAPTOP-HS5L5VEH MINGW64 /c/Dev/Projects/test-bed/app-zig-cglm (master)
$ zig build -Dtarget=x86_64-windows-gnu
.\zig-cache\o\1bc9e6dc93c2ab6590b8006381a9000e\cimport.zig:4255:26: error: unable to translate function
pub const glm_mul_sse2 = @compileError("unable to translate function"); // C:\Dev\Projects\test-bed\app-zig-cglm\vendor\cglm\include\cglm/simd/sse2/affine.h:17:1
^
.\zig-cache\o\1bc9e6dc93c2ab6590b8006381a9000e\cimport.zig:4264:5: note: referenced here
glm_mul_sse2(m1, m2, dest);
^
.\zig-cache\o\1bc9e6dc93c2ab6590b8006381a9000e\cimport.zig:657:20: error: unable to resolve typedef child type
pub const __m128 = @compileError("unable to resolve typedef child type"); // C:\Dev\Tools\zig-0.7.1\lib\include\xmmintrin.h:17:15
^
.\zig-cache\o\1bc9e6dc93c2ab6590b8006381a9000e\cimport.zig:1255:48: note: referenced here
pub fn _mm_store_ps(arg___p: [*c]f32, arg___a: __m128) callconv(.C) void {
^
.\zig-cache\o\1bc9e6dc93c2ab6590b8006381a9000e\cimport.zig:3557:5: note: referenced here
_mm_store_ps(&dest[@intCast(c_uint, @as(c_int, 0))], _mm_load_ps(&mat[@intCast(c_uint, @as(c_int, 0))]));
^
app-zig-cglm...The following command exited with error code 1:
C:\Dev\Tools\zig-0.7.1\zig.exe build-exe C:\Dev\Projects\test-bed\app-zig-cglm\src\main.zig C:\Dev\Projects\test-bed\app-zig-cglm\zig-cache\o\dfacf78e7514990119de31b967d43202\cglm.lib --library c --cache-dir C:\Dev\Projects\test-bed\app-zig-cglm\zig-cache --global-cache-dir C:\Users\alagt\AppData\Local\zig --name app-zig-cglm -target x86_64-windows-gnu -isystem C:\Dev\Projects\test-bed\app-zig-cglm\vendor\cglm\include --enable-cache
error: the following build command failed with exit code 1:
C:\Dev\Projects\test-bed\app-zig-cglm\zig-cache\o\64a99c599e98cac00a19cffa57c71a68\build.exe C:\Dev\Tools\zig-0.7.1\zig.exe C:\Dev\Projects\test-bed\app-zig-cglm C:\Dev\Projects\test-bed\app-zig-cglm\zig-cache C:\Users\alagt\AppData\Local\zig -Dtarget=x86_64-windows-gnu
avxintrin.h 中的无法解析行如下
typedef float __m128 __attribute__((__vector_size__(16), __aligned__(16)));
我想知道使用内在函数的 C 库现在是否可以工作,如果不能,我想从 build.zig 禁用内在函数特性 sse/avx/... 以便可以在没有 SIMD 的情况下构建和链接 CGLM优化。
编辑(从 build.zig 禁用 SIMD 功能)
我在 lib/zig/std/build.zig 上进行了一些研究,以了解如何禁用 SIMD 功能,最后我能够在 build.zig 上使用以下代码禁用
var target = b.standardTargetOptions(.{});
target.cpu_features_sub = x86.featureSet(&[_]x86.Feature{
x86.Feature.avx,
x86.Feature.avx2,
x86.Feature.avx512bf16,
x86.Feature.avx512bitalg,
x86.Feature.avx512bw,
x86.Feature.avx512cd,
x86.Feature.avx512dq,
x86.Feature.avx512er,
x86.Feature.avx512f,
x86.Feature.avx512ifma,
x86.Feature.avx512pf,
x86.Feature.avx512vbmi,
x86.Feature.avx512vbmi2,
x86.Feature.avx512vl,
x86.Feature.avx512vnni,
x86.Feature.avx512vp2intersect,
x86.Feature.avx512vpopcntdq,
x86.Feature.sse,
x86.Feature.sse_unaligned_mem,
x86.Feature.sse2,
x86.Feature.sse3,
x86.Feature.sse4_1,
x86.Feature.sse4_2,
x86.Feature.sse4a,
x86.Feature.ssse3,
});
状态:
现在我能够构建(没有 SIMD 优化,这是不理想的)并从 zig 代码链接 CGLM 库,但真正令人担忧的问题出现了,因为调用标准 C tanf 函数的代码正在计算真正错误的结果。
按照为 glm_perspective 函数生成的 zig 代码。
pub fn glm_perspective(arg_fovy: f32, arg_aspect: f32, arg_nearVal: f32, arg_farVal: f32, arg_dest: [*c]vec4) callconv(.C) void {
var fovy = arg_fovy;
var aspect = arg_aspect;
var nearVal = arg_nearVal;
var farVal = arg_farVal;
var dest = arg_dest;
var f: f32 = undefined;
var @"fn": f32 = undefined;
glm_mat4_zero(dest);
f = (1 / tanf((fovy * 0.5)));
@"fn" = (1 / (nearVal - farVal));
dest[@intCast(c_uint, @as(c_int, 0))][@intCast(c_uint, @as(c_int, 0))] = (f / aspect);
dest[@intCast(c_uint, @as(c_int, 1))][@intCast(c_uint, @as(c_int, 1))] = f;
dest[@intCast(c_uint, @as(c_int, 2))][@intCast(c_uint, @as(c_int, 2))] = ((nearVal + farVal) * @"fn");
dest[@intCast(c_uint, @as(c_int, 2))][@intCast(c_uint, @as(c_int, 3))] = -1;
dest[@intCast(c_uint, @as(c_int, 3))][@intCast(c_uint, @as(c_int, 2))] = (((2 * nearVal) * farVal) * @"fn");
}
执行 f = (1 / tanf((fovy * 0.5))); 的结果 arg_fovy = 0.785398185(45 度弧度)的行返回 134217728。这是完全错误的。正确的结果应该是大约。2.4142134。
VSCode cppvsdbg 不允许我进入 tanf 以了解 tanf 实现可能有什么问题。
编辑(使用 Zig 编译器构建和编译最小 C 代码):
我使用数学 tanf 编译了一个 min C 案例,并使用 Zig 构建器机制构建。
构建.zig
const Builder = @import("std").build.Builder;
const x86 = @import("std").Target.x86;
pub fn build(b: *Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
var target = b.standardTargetOptions(.{});
target.cpu_features_sub = x86.featureSet(&[_]x86.Feature{
x86.Feature.avx,
x86.Feature.avx2,
x86.Feature.avx512bf16,
x86.Feature.avx512bitalg,
x86.Feature.avx512bw,
x86.Feature.avx512cd,
x86.Feature.avx512dq,
x86.Feature.avx512er,
x86.Feature.avx512f,
x86.Feature.avx512ifma,
x86.Feature.avx512pf,
x86.Feature.avx512vbmi,
x86.Feature.avx512vbmi2,
x86.Feature.avx512vl,
x86.Feature.avx512vnni,
x86.Feature.avx512vp2intersect,
x86.Feature.avx512vpopcntdq,
x86.Feature.sse,
x86.Feature.sse_unaligned_mem,
x86.Feature.sse2,
x86.Feature.sse3,
x86.Feature.sse4_1,
x86.Feature.sse4_2,
x86.Feature.sse4a,
x86.Feature.ssse3,
});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exeC = b.addExecutable("app-c", "src/main.c");
exeC.setBuildMode(mode);
exeC.setTarget(target);
// C and win32
exeC.linkLibC();
exeC.install();
const run_cmd = exeC.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
src/main.c
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv) {
float fovDeg = (argc == 1 ? 45.0f : 50.0f);
while (1) {
float fovRad = fovDeg * (M_PI / 180.0f);
float tanFov = tanf(fovRad);
fprintf(stdout, "fovDeg=%f, fovRad=%f, tanFov=%f\n", fovDeg, fovRad, tanFov);
}
return 0;
}
如果我在没有 SIMD 排除的情况下构建,或者只是将目标设置为本机,则代码会以预期的结果正确执行。
$ zig build -Dtarget=native-windows-gnu 运行 -- 1
fovDeg=50.000000, fovRad=0.872665, tanFov=1.191754
如果我使用 SIMD 排除项构建并执行它,则 tanf 返回 0
$ zig build -Dtarget=x86_64-windows-gnu 运行 -- 1
fovDeg=50.000000, fovRad=0.872665, tanFov=0.000000
当前状态
在我看来,这
- zig mingw64 或 msvcrt 不支持基本数学函数,并且在禁用 x86_64 架构 SIMD 功能时会出现浮点错误。
- Zig mingw64 不支持 SIMD C 代码。
- Zig 无法以可行的方式编译CGLM。
如果有人能带来一些启示,以防我遗漏了什么,将不胜感激。
提前致谢。