|
SBgl 0.1.0
A graphics framework in C99
|
The math library provides Data-Oriented Design (DOD) workflows. The library is contained within a single header file (sbgl_math.h) and utilizes static inline functions for compiler inlining.
sbgl_Vec3, sbgl_Vec4, sbgl_Mat4, sbgl_Quat) are 16-byte aligned. This allows the compiler to utilize SSE/AVX instructions when processing arrays of math types.sbgl_Vec3 is explicitly padded to 16 bytes. While this increases memory footprint by 4 bytes per vector, it ensures that 3D data is 128-bit aligned. This is a deliberate trade-off to enable SIMD loads, ensure cache line alignment in arrays, and maintain direct compatibility with GPU buffer layouts (std140/std430).unions, providing access to individual components (x, y, z, w) or raw arrays (v[4]).| Type | Alignment | Description |
|---|---|---|
| sbgl_Vec2 | Default | 2D vector (float). |
| sbgl_Vec3 | 16-byte | 3D vector, padded to 16 bytes. |
| sbgl_Vec4 | 16-byte | 4D vector. |
| sbgl_Quat | 16-byte | Quaternion. |
| sbgl_Mat4 | 16-byte | 4x4 matrix, column-major. |
sbgl_InvSqrt(): Computes 1.0f / sqrtf(x) using bit-level manipulation and two Newton-Raphson iterations. This implementation is inspired by the Approximate Inverse Square Root algorithm historically used in Quake III Arena.The library provides static inline constructor functions to initialize types and ensure correct padding.
sbgl_Vec3Add(), sbgl_Vec3Sub(), sbgl_Vec3Mul().sbgl_Vec3Dot(), sbgl_Vec3Cross(), sbgl_Vec3Length(), sbgl_Vec3Normalize().sbgl_Mat4Identity(), sbgl_Mat4Perspective(), sbgl_Mat4Orthographic(), sbgl_Mat4LookAt().sbgl_Mat4Translate(), sbgl_Mat4Rotate(), sbgl_Mat4Scale().sbgl_Mat4Mul(), sbgl_Mat4MulVec4().sbgl_QuatIdentity(), sbgl_QuatMul(), sbgl_QuatFromAxisAngle().sbgl_QuatToMat4().While the library technically supports brace-enclosed initialization, it is strongly recommended to use the provided constructor functions (e.g., sbgl_Vec3Set()) for all data initialization.
Types like sbgl_Vec3 are defined as a union with an internal anonymous struct that includes a _pad field for SIMD alignment.
Using raw brace initialization (e.g., { 1.0f, 2.0f, 3.0f }) often skips the _pad field, triggering compiler warnings or errors when -Wmissing-field-initializers is enabled. Explicitly using sbgl_Vec3Set(1.0f, 2.0f, 3.0f) ensures every field—including padding—is correctly initialized and satisfies strict compilation requirements.
If brace initialization must be used (e.g., for static global data), a double-brace syntax {{ ... }} is required due to the nested nature of the union.
{} initialize the union.{} initialize the anonymous struct._pad, should be specified to avoid warnings.The library provides static inline definitions, allowing the compiler to optimize math operations directly into the calling code. The aligned memory layout is intended to facilitate data streaming into SIMD registers during batch processing.