Skip to content

Add alignment, SM_INLINE, OpenMP and GLFW example support#1

Open
joneqdaniel wants to merge 1 commit into
slendidev:masterfrom
joneqdaniel:master
Open

Add alignment, SM_INLINE, OpenMP and GLFW example support#1
joneqdaniel wants to merge 1 commit into
slendidev:masterfrom
joneqdaniel:master

Conversation

@joneqdaniel

@joneqdaniel joneqdaniel commented May 15, 2026

Copy link
Copy Markdown
  • explicitly force inline using SM_INLINE with (always_inline,flatten,nothrow) inline
  • align Vec to N * alignof(T) if N==std::bit_ceil(N) otherwise use default alignof(T)
  • strip binary if CMAKE_BUILD_TYPE != Debug
  • add OpenMP support
  • add SSE compiler option on AMD64
  • add GNUInstallDirs and FindPkgConfig for non MSVC
  • add test case for alignof(Vec3) and alignof(Vec4)
  • add AVec3 and AVec3d
  • add sincos constexpr
  • add position() and direction() to Vec with N = 3
  • add GLFW example support
  • add GLUT example support

@joneqdaniel joneqdaniel force-pushed the master branch 5 times, most recently from 2c38922 to a5391b0 Compare May 16, 2026 20:31
@joneqdaniel joneqdaniel changed the title Add alignment, SM_INLINE and OpenMP CMake support Add alignment, SM_INLINE, OpenMP and GLFW example support May 16, 2026
@joneqdaniel joneqdaniel force-pushed the master branch 6 times, most recently from c806259 to 53dae22 Compare May 17, 2026 22:25

@slendidev slendidev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots and lots of constexpr was removed. What is up with that? There's also other issues, and I don't think that there needs to be an alignment parameter, especially as a template parameter. I also don't see any tangible benefits from it.

Comment thread CMakeLists.txt
Comment on lines +24 to +64
if(NOT CMAKE_GENERATOR MATCHES "^Visual Studio")
if( NOT CMAKE_BUILD_TYPE STREQUAL Debug )
add_compile_options(-O3)
add_link_options(-Wl,--gc-sections)
add_link_options(-Wl,--print-gc-sections)
add_link_options(-Wl,-s)
else()
add_compile_options(-O0 -g)
endif()
else()
if( NOT CMAKE_BUILD_TYPE STREQUAL Debug )
add_compile_options(/O2)
else()
add_compile_options(/Od /Zi)
endif()
endif()

if (NOT CMAKE_GENERATOR MATCHES "^Visual Studio")
if ( NOT CMAKE_CROSS_COMPILING )
add_compile_options(-march=native)
if( CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86_64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "AMD64" )
add_compile_options(-mfpmath=sse)
endif()
endif()
add_compile_options(-fopenmp)
add_compile_options(-fopenmp-simd)
add_compile_options(-ftree-vectorize)
add_compile_options(-Wno-gnu-alignof-expression)
add_compile_options(-D_FILE_OFFSET_BITS=64)
add_compile_options(-fdata-sections)
add_compile_options(-ffunction-sections)
add_compile_options(-fpermissive)
add_compile_options(-Wno-array-bounds)
add_compile_options(-Wno-unused-parameter)
add_compile_options(-Wno-pedantic)
add_compile_options(-Wno-narrowing)
add_compile_options(-Wno-attributes)
else()
add_compile_options(/openmp:experimental)
endif()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add indentation.

Comment thread src/modules/smath.cppm

export using ::smath::Vec2;
export using ::smath::Vec3;
export using ::smath::AVec3

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's the semicolon?

Comment thread include/smath/smath.hpp
Comment on lines +58 to +60
template<std::size_t N, typename T = float, std::size_t A = ((N == std::bit_ceil<std::size_t>(N)) ? N : 1) * alignof(T), std::size_t N_POW2 = std::bit_ceil<std::size_t>(N)>
requires std::is_arithmetic_v<T>
struct Vec;
struct alignas(A) Vec;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think A should be a template parameter, complicates API and causes type mismatches. If we truly want aligned storage this should be an internal implementation detail and not part of the public type signature.

Comment thread include/smath/smath.hpp
Comment on lines +52 to +54
template <class S>
constexpr SM_INLINE std::pair<S,S> sincos(S arg) { return { std::sin(arg), std::cos(arg) }; }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inside the smath namespace alongside the other math functions.

Comment thread include/smath/smath.hpp
#define SM_INLINE __attribute__((always_inline,flatten,nothrow)) inline
#define SM_CONST __attribute__((const))
#endif

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use C++ style attribute syntax. Also I don't think __flatten is available on MSVC.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it should be consistently named with the rest of the macros, aka SMATH_* and not SM_*.

Comment thread include/smath/smath.hpp
Comment on lines +759 to 762
template<std::size_t R, std::size_t C, typename T = float, std::size_t A = std::bit_ceil<std::size_t>(alignof(Vec<R,T>) * C)>
requires std::is_arithmetic_v<T>
struct Mat;
struct alignas(A) Mat;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue with the addition of A in Vec

Comment thread CMakeLists.txt
Comment on lines +48 to +61
add_compile_options(-fopenmp)
add_compile_options(-fopenmp-simd)
add_compile_options(-ftree-vectorize)
add_compile_options(-Wno-gnu-alignof-expression)
add_compile_options(-D_FILE_OFFSET_BITS=64)
add_compile_options(-fdata-sections)
add_compile_options(-ffunction-sections)
add_compile_options(-fpermissive)
add_compile_options(-Wno-array-bounds)
add_compile_options(-Wno-unused-parameter)
add_compile_options(-Wno-pedantic)
add_compile_options(-Wno-narrowing)
add_compile_options(-Wno-attributes)
else()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the compiler instead, not the cmake generator. MSVC can be used with other generators so that would end up with those flags being passed to MSVC.

Comment thread CMakeLists.txt
endif()
endif()

if (NOT CMAKE_GENERATOR MATCHES "^Visual Studio")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated check.

Comment thread CMakeLists.txt
Comment on lines +142 to +163
if(SMATH_BUILD_GLFW_EXAMPLES)
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
file(GLOB GLFW_EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/glfw/*.cpp")
foreach(EXAMPLE_FILE ${GLFW_EXAMPLE_SOURCES})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE)
add_executable("glfw_${EXAMPLE_NAME}" ${EXAMPLE_FILE})
target_link_libraries("glfw_${EXAMPLE_NAME}" PUBLIC smath::smath glfw OpenGL OpenGL::GLU)
endforeach()
endif()

if(SMATH_BUILD_GLUT_EXAMPLES)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
file(GLOB GLUT_EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/glut/*.cpp")
foreach(EXAMPLE_FILE ${GLUT_EXAMPLE_SOURCES})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE)
add_executable("glut_${EXAMPLE_NAME}" ${EXAMPLE_FILE})
target_link_libraries("glut_${EXAMPLE_NAME}" PUBLIC smath::smath OpenGL OpenGL::GLU GLUT::GLUT)
endforeach()
endif()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this vibe coded? There's no examples/glfw or examples/glut!

Comment thread include/smath/smath.hpp
Comment on lines +1348 to 1354
res[0, 0] = 2 / (right - left);
res[1, 1] = 2 / (top - bottom);
res[2, 2] = -2 / (far - near);
res[0, 3] = -(right + left) / (right - left);
res[1, 3] = -(top + bottom) / (top - bottom);
res[2, 3] = -(far + near) / (far - near);
res[2, 3] = -(far + near) / (far - near);
res[3, 3] = 1;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated formatting changes. Run clang-format.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands smath with alignment-focused vector types and additional build/example support, aiming to enable more aggressive inlining/SIMD and add optional GLFW/GLUT example builds.

Changes:

  • Adds aligned Vec variants (AVec3, AVec3d) and alignment-related unit tests.
  • Introduces SM_INLINE, adds OpenMP SIMD pragmas across vector/matrix operations, and adds position()/direction() helpers for 3D vectors.
  • Updates CMake to add OpenMP-related flags and adds optional GLFW/GLUT example targets.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.

File Description
include/smath/smath.hpp Adds alignment/inlining macros, aligned vector aliases, OpenMP SIMD pragmas, and new Vec helpers.
src/modules/smath.cppm Re-exports new public API symbols from the module interface.
tests/vec.cpp Adds/updates tests to validate alignment behavior and aligned cross-product results.
CMakeLists.txt Adds OpenMP flags and optional GLFW/GLUT example build targets.
Comments suppressed due to low confidence (1)

include/smath/smath.hpp:214

  • The scalar-fill constructor is no longer constexpr, but there are still constexpr operators (e.g., operator-(T, Vec const&)) that call Vec(s). A constexpr function that unconditionally calls a non-constexpr constructor/function is ill-formed and can break compilation.
	explicit Vec(T const &s) noexcept
	{
		for (auto &v : *this)
			v = s;
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/modules/smath.cppm
Comment on lines 13 to 16
export using ::smath::Vec2;
export using ::smath::Vec3;
export using ::smath::AVec3
export using ::smath::Vec4;
Comment thread include/smath/smath.hpp
Comment on lines +44 to +50
#ifdef _MSC_VER
#define SM_INLINE __force_inline __flatten __declspec(nothrow) inline
#define SM_CONST __declspec(noalias)
#else
#define SM_INLINE __attribute__((always_inline,flatten,nothrow)) inline
#define SM_CONST __attribute__((const))
#endif
Comment thread include/smath/smath.hpp
Comment on lines 25 to 35
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <format>
#include <numbers>
#include <optional>
#include <type_traits>
#include <bit>

Comment thread include/smath/smath.hpp
Comment on lines 685 to 688
using Vec4 = Vec<4>;
/** @brief 3D aligned float vector alias. */
using AVec3 = Vec<3, float, alignof(Vec4)>;

Comment thread include/smath/smath.hpp
Comment on lines 481 to +486
requires(N == 3)
/** @brief 3D cross product. */
constexpr auto cross(Vec const &r) const noexcept -> Vec
constexpr SM_INLINE auto cross(Vec const &r) const noexcept -> Vec<3,U,alignof(U) * 4>
{
return {
(*this)[1] * r[2] - (*this)[2] * r[1],
(*this)[2] * r[0] - (*this)[0] * r[2],
(*this)[0] * r[1] - (*this)[1] * r[0],
};
return (Vec<3,U,alignof(U) * 4>)swizzle<"yzx">((*this)) * (Vec<3,U,alignof(U) * 4>)swizzle<"zxy">(r) -
(Vec<3,U,alignof(U) * 4>)swizzle<"zyx">((*this)) * (Vec<3,U,alignof(U) * 4>)swizzle<"yzx">(r);
Comment thread CMakeLists.txt
Comment on lines +20 to +22
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE )
endif()
Comment thread CMakeLists.txt
Comment on lines +41 to +63
if (NOT CMAKE_GENERATOR MATCHES "^Visual Studio")
if ( NOT CMAKE_CROSS_COMPILING )
add_compile_options(-march=native)
if( CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86_64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "AMD64" )
add_compile_options(-mfpmath=sse)
endif()
endif()
add_compile_options(-fopenmp)
add_compile_options(-fopenmp-simd)
add_compile_options(-ftree-vectorize)
add_compile_options(-Wno-gnu-alignof-expression)
add_compile_options(-D_FILE_OFFSET_BITS=64)
add_compile_options(-fdata-sections)
add_compile_options(-ffunction-sections)
add_compile_options(-fpermissive)
add_compile_options(-Wno-array-bounds)
add_compile_options(-Wno-unused-parameter)
add_compile_options(-Wno-pedantic)
add_compile_options(-Wno-narrowing)
add_compile_options(-Wno-attributes)
else()
add_compile_options(/openmp:experimental)
endif()
Comment thread CMakeLists.txt
Comment on lines +148 to +150
add_executable("glfw_${EXAMPLE_NAME}" ${EXAMPLE_FILE})
target_link_libraries("glfw_${EXAMPLE_NAME}" PUBLIC smath::smath glfw OpenGL OpenGL::GLU)
endforeach()
Comment thread CMakeLists.txt
Comment on lines +159 to +161
add_executable("glut_${EXAMPLE_NAME}" ${EXAMPLE_FILE})
target_link_libraries("glut_${EXAMPLE_NAME}" PUBLIC smath::smath OpenGL OpenGL::GLU GLUT::GLUT)
endforeach()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants