From 45cf4710c476d30ff31333590b3ec85339033016 Mon Sep 17 00:00:00 2001 From: Valeri Date: Fri, 1 Dec 2023 04:19:47 +0300 Subject: [PATCH 1/5] Use the appropriate version of alignof --- include/cglm/types.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/include/cglm/types.h b/include/cglm/types.h index bafebe6..65391cd 100644 --- a/include/cglm/types.h +++ b/include/cglm/types.h @@ -8,6 +8,10 @@ #ifndef cglm_types_h #define cglm_types_h +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) +# include +#endif + #if defined(_MSC_VER) /* do not use alignment for older visual studio versions */ # if _MSC_VER < 1913 /* Visual Studio 2017 version 15.6 */ @@ -57,8 +61,16 @@ # define CGLM_ASSUME_ALIGNED(expr, alignment) (expr) #endif -#define CGLM_CASTPTR_ASSUME_ALIGNED(expr, type) \ - ((type*)CGLM_ASSUME_ALIGNED((expr), __alignof__(type))) +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) +# define CGLM_CASTPTR_ASSUME_ALIGNED(expr, type) \ + ((type*)CGLM_ASSUME_ALIGNED((expr), alignof(type))) +#elif defined(_MSC_VER) +# define CGLM_CASTPTR_ASSUME_ALIGNED(expr, type) \ + ((type*)CGLM_ASSUME_ALIGNED((expr), __alignof(type))) +#else +# define CGLM_CASTPTR_ASSUME_ALIGNED(expr, type) \ + ((type*)CGLM_ASSUME_ALIGNED((expr), __alignof__(type))) +#endif typedef int ivec2[2]; typedef int ivec3[3]; From 1fdc1c86757f50438434528ecddd62e41940e41d Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Fri, 1 Dec 2023 11:18:48 +0300 Subject: [PATCH 2/5] struct: remove _vec_inv from struct function lists --- include/cglm/struct/vec3.h | 1 - include/cglm/struct/vec4.h | 1 - 2 files changed, 2 deletions(-) diff --git a/include/cglm/struct/vec3.h b/include/cglm/struct/vec3.h index 163b2be..0b9713a 100644 --- a/include/cglm/struct/vec3.h +++ b/include/cglm/struct/vec3.h @@ -43,7 +43,6 @@ CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest); CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v); CGLM_INLINE vec3s glms_vec3_negate(vec3s v); - CGLM_INLINE vec3s glms_vec3_inv(vec3s v); CGLM_INLINE vec3s glms_vec3_normalize(vec3s v); CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b); CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b); diff --git a/include/cglm/struct/vec4.h b/include/cglm/struct/vec4.h index 53ec3fb..1a6b359 100644 --- a/include/cglm/struct/vec4.h +++ b/include/cglm/struct/vec4.h @@ -42,7 +42,6 @@ CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest); CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest); CGLM_INLINE vec4s glms_vec4_negate(vec4s v); - CGLM_INLINE vec4s glms_vec4_inv(vec4s v); CGLM_INLINE vec4s glms_vec4_normalize(vec4s v); CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b); CGLM_INLINE float glms_vec4_distance2(vec4s a, vec4s b); From 0b2006dd4760e516c62cdcb64eac92add5f55ee3 Mon Sep 17 00:00:00 2001 From: Valeri Date: Sat, 2 Dec 2023 01:44:54 +0300 Subject: [PATCH 3/5] meson: don't build by default if used in a subproject --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index de6988e..0bc40a6 100644 --- a/meson.build +++ b/meson.build @@ -95,6 +95,7 @@ cglm_dep = declare_dependency( dependencies : cglm_deps, compile_args : cglm_args, include_directories : cglm_inc, + build_by_default: not meson.is_subproject(), version : meson.project_version() ) From 009405adcd14875c8b076eac4459f06e24f3db05 Mon Sep 17 00:00:00 2001 From: myfreeer Date: Sat, 2 Dec 2023 09:54:49 +0800 Subject: [PATCH 4/5] wasm: prefer pmin/pmax According to [emscripten](https://emscripten.org/docs/porting/simd.html) and [v8](https://github.com/v8/v8/blob/b6520eda5eafc3b007a5641b37136dfc9d92f63d/src/compiler/backend/x64/code-generator-x64.cc#L2661-L2699), `[f32x4|f64x2].[min|max]` compiles to much more instructions than `[f32x4|f64x2].[pmin|pmax]`. It is defined in [spec](https://github.com/WebAssembly/spec/blob/main/proposals/simd/SIMD.md#floating-point-min-and-max) that the difference between pmin/pmax and min/max is NaN-propagating behavior, and the equivalent to the x86 `_mm_min_ps`/`_mm_max_ps` is pmin/pmax in [v8](https://github.com/v8/v8/blob/b6520eda5eafc3b007a5641b37136dfc9d92f63d/src/compiler/backend/x64/code-generator-x64.cc#L2740-L2747). This should make functions with min/max faster on webassembly, and align with the existing behavior with x86 sse. --- include/cglm/vec4.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/cglm/vec4.h b/include/cglm/vec4.h index 5c9ff6e..ae99ff6 100644 --- a/include/cglm/vec4.h +++ b/include/cglm/vec4.h @@ -649,7 +649,7 @@ glm_vec4_maxadd(vec4 a, vec4 b, vec4 dest) { #if defined(__wasm__) && defined(__wasm_simd128__) glmm_store(dest, wasm_f32x4_add( glmm_load(dest), - wasm_f32x4_max(glmm_load(a), glmm_load(b)))); + wasm_f32x4_pmax(glmm_load(a), glmm_load(b)))); #elif defined( __SSE__ ) || defined( __SSE2__ ) glmm_store(dest, _mm_add_ps(glmm_load(dest), _mm_max_ps(glmm_load(a), @@ -681,7 +681,7 @@ glm_vec4_minadd(vec4 a, vec4 b, vec4 dest) { #if defined(__wasm__) && defined(__wasm_simd128__) glmm_store(dest, wasm_f32x4_add( glmm_load(dest), - wasm_f32x4_min(glmm_load(a), glmm_load(b)))); + wasm_f32x4_pmin(glmm_load(a), glmm_load(b)))); #elif defined( __SSE__ ) || defined( __SSE2__ ) glmm_store(dest, _mm_add_ps(glmm_load(dest), _mm_min_ps(glmm_load(a), @@ -854,7 +854,7 @@ CGLM_INLINE void glm_vec4_maxv(vec4 a, vec4 b, vec4 dest) { #if defined(__wasm__) && defined(__wasm_simd128__) - glmm_store(dest, wasm_f32x4_max(glmm_load(a), glmm_load(b))); + glmm_store(dest, wasm_f32x4_pmax(glmm_load(a), glmm_load(b))); #elif defined( __SSE__ ) || defined( __SSE2__ ) glmm_store(dest, _mm_max_ps(glmm_load(a), glmm_load(b))); #elif defined(CGLM_NEON_FP) @@ -878,7 +878,7 @@ CGLM_INLINE void glm_vec4_minv(vec4 a, vec4 b, vec4 dest) { #if defined(__wasm__) && defined(__wasm_simd128__) - glmm_store(dest, wasm_f32x4_min(glmm_load(a), glmm_load(b))); + glmm_store(dest, wasm_f32x4_pmin(glmm_load(a), glmm_load(b))); #elif defined( __SSE__ ) || defined( __SSE2__ ) glmm_store(dest, _mm_min_ps(glmm_load(a), glmm_load(b))); #elif defined(CGLM_NEON_FP) @@ -902,8 +902,8 @@ CGLM_INLINE void glm_vec4_clamp(vec4 v, float minVal, float maxVal) { #if defined(__wasm__) && defined(__wasm_simd128__) - glmm_store(v, wasm_f32x4_min( - wasm_f32x4_max(glmm_load(v), wasm_f32x4_splat(minVal)), + glmm_store(v, wasm_f32x4_pmin( + wasm_f32x4_pmax(glmm_load(v), wasm_f32x4_splat(minVal)), wasm_f32x4_splat(maxVal))); #elif defined( __SSE__ ) || defined( __SSE2__ ) glmm_store(v, _mm_min_ps(_mm_max_ps(glmm_load(v), _mm_set1_ps(minVal)), From 24e417107b6f4ea2f9d86b67b1547df82e92b819 Mon Sep 17 00:00:00 2001 From: myfreeer Date: Sat, 2 Dec 2023 11:08:33 +0800 Subject: [PATCH 5/5] ci: initial support of meson and emscripten It seems that meson only supports emscripten as a compiler to wasm at present, wasi-sdk support is not completed yet, so this only adds ci build scripts for emscripten. References: * * --- .github/workflows/meson-wasm.yml | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/meson-wasm.yml diff --git a/.github/workflows/meson-wasm.yml b/.github/workflows/meson-wasm.yml new file mode 100644 index 0000000..dac2c67 --- /dev/null +++ b/.github/workflows/meson-wasm.yml @@ -0,0 +1,79 @@ +name: Meson WebAssembly + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + wasmtime_version: v7.0.0 + wasmer_version: v3.1.1 + +jobs: + build_emsdk: + strategy: + matrix: + BUILD_TYPE: [debug, debugoptimized, release, minsize] + C_FLAGS: ['', '-msimd128', '-msse -msse2 -msimd128', '-msse -msse2 -msse3 -msse4 -msimd128'] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup emsdk + uses: mymindstorm/setup-emsdk@v13 + + - name: Verify emsdk + run: emcc -v + + - name: Creating cross file + run: | + cat << EOF > ${{github.workspace}}/meson_cross_emsdk.txt + [binaries] + c = '`which emcc`' + cpp = '`which em++`' + ar = '`which emar`' + + [built-in options] + + c_args = ['-Wno-unused-parameter'] + c_link_args = ['-s', 'STANDALONE_WASM'] + cpp_args = ['-Wno-unused-parameter'] + cpp_link_args = ['-s', 'STANDALONE_WASM'] + + [host_machine] + + system = 'emscripten' + cpu_family = 'wasm32' + cpu = 'wasm32' + endian = 'little' + + EOF + cat ${{github.workspace}}/meson_cross_emsdk.txt + + - uses: actions/setup-python@v4 + + - name: Install meson + run: | + sudo python3 -m pip install meson ninja + + - name: Build with meson + run: | + meson setup build -Dbuildtype=${{matrix.BUILD_TYPE}} --cross-file ${{github.workspace}}/meson_cross_emsdk.txt --default-library=static -Dbuild_tests=true + meson test -C build + + - name: Test with wasmtime + run: | + cd ${{github.workspace}} + ls -lh ${{github.workspace}}/build/ + wget --no-verbose https://github.com/bytecodealliance/wasmtime/releases/download/${{env.wasmtime_version}}/wasmtime-${{env.wasmtime_version}}-x86_64-linux.tar.xz + tar xf wasmtime-${{env.wasmtime_version}}-x86_64-linux.tar.xz + ./wasmtime-${{env.wasmtime_version}}-x86_64-linux/wasmtime run --wasm-features simd ${{github.workspace}}/build/tests.wasm + + - name: Test with wasmer + run: | + cd ${{github.workspace}} + mkdir wasmer + cd wasmer + wget --no-verbose https://github.com/wasmerio/wasmer/releases/download/${{env.wasmer_version}}/wasmer-linux-amd64.tar.gz + tar xf wasmer-linux-amd64.tar.gz + ./bin/wasmer run --enable-simd ${{github.workspace}}/build/tests.wasm