Expand description
§ggmath
A math library for games and graphics with support for generics and SIMD.
The library features:
- Vectors:
Vec2<T>,Vec3<T>,Vec4<T>. - Square Matrices (todo):
Mat2<T>,Mat3<T>,Mat4<T>. - Quaternion (todo):
Quat<T>. - Affine Transformations (todo):
Affine2<T>,Affine3<T>. - Masks (todo):
Mask2<T>,Mask3<T>,Mask4<T>.
For appropriate scalars, these types are aligned for SIMD to improve performance. The library also features unaligned types:
- Vectors:
Vec2U<T>,Vec3U<T>,Vec4U<T>. - Square Matrices (todo):
Mat2U<T>,Mat3U<T>,Mat4U<T>. - Quaternion (todo):
QuatU<T>. - Affine Transformations (todo):
Affine2U<T>,Affine3U<T>. - Masks (todo):
Mask2U<T>,Mask3U<T>,Mask4U<T>.
Unaligned types have the same functionality as aligned types, but are not aligned for SIMD meaning they take less memory but have slower operations.
All types are type aliases to these generic structs:
Vector<N, T, A>.Matrix<N, T, A>(todo).Quaternion<T, A>(todo).Affine<N, T, A>(todo).Mask<N, T, A>(todo).
Where:
Nis the length (2, 3, or 4).Tis the scalar type.Ais eitherAlignedorUnaligned.
Generic structs are used to implement functionality for all lengths and or both alignments without duplicating code or using macros.
§Development Status
ggmath is not mature yet but is under active development.
Feature List:
- Vectors
- Square Matrices
- Quaternion
- Affine Transformations
- Masks
- Sufficient Float-Vector functionality
- Sufficient Int-Vector functionality
- Sufficient Matrix functionality
- Sufficient Quaternion functionality
- Sufficient Affine functionality
Crate Support:
Performance:
-
f32SSE2 optimizations -
f32SSE4.2+ optimizations -
f64AVX+ optimizations -
i32u32SSE2+ optimizations -
i8u8boolSSE2+ optimizations forMat4<T> -
i16u16AVX2+ optimizations forMat4<T> -
f32NEON optimizations -
i32u32NEON optimizations -
i8u8boolNEON optimizations forMat4<T> -
f32WASM optimizations -
i32u32WASM optimizations -
i8u8boolWASM optimizations forMat4<T>
§Comparison to glam
glam is more mature than ggmath. This comparison assumes ggmath has
reached the maturity of glam. If you need any of the features missing
from ggmath, you should use glam.
The primary difference between the two crates is that ggmath uses generics and
glam intentionally doesn’t. You should pick a crate based on these pros and
cons:
§Code Duplication
Generics eliminate code duplication by enabling code that is generic over scalar type, vector length, and alignment.
This is primarily useful for writing math functions that need to work for
multiple scalar types, all lengths, or both SIMD and no SIMD. For example, a
cross platform deterministic implementation of sin would need to be duplicated
multiple times when using glam, but will only need to be written once using
ggmath.
§Custom Scalar Types
Generics make it possible to define custom scalar types.
This is primarily useful for either fixed point numbers (e.g., support for the
fixed crate), or SoA storage (Struct of Arrays) (e.g., Vec3<f32x4> using the
wide crate).
§Compile Times
To properly support generics, ggmath internally uses many language tricks that
unfortunately add work for the compiler not only when compiling ggmath, but
also when compiling math code that uses it.
If you need the fastest compile times possible you should use glam.
§Complexity
Generics make ggmath harder to understand in advance scenarios.
While the API is mostly similar (Vec3<f32> vs Vec3A), ggmath has some
advanced features that are harder to understand than anything in glam (e.g.,
the full generic form Vector<N, T, A>).
§Usage
Add this to your Cargo.toml:
[dependencies]
ggmath = "0.15.1"For no_std support, enable the libm feature:
[dependencies]
ggmath = { version = "0.15.1", features = ["libm"] }For no_std without libm, disable default features:
[dependencies]
ggmath = { version = "0.15.1", default-features = false }Without std or libm, the crate compiles but all float functionality that
relies on a backend is disabled.
§Optional Features
-
std(default feature): Usesstdas the backend for float functionality. -
assertions: Enables assertions in release mode. Assertions are panics that catch invalid input and are enabled by default in debug mode. -
no-assertions: Disables assertions in debug mode. Library crates should not directly enableassertionsorno-assertionsand should leave the decision to binary crates. -
bytemuck: Implementsbytemucktraits for allggmathtypes. -
libm: Useslibmas the backend for float functionality. This makes the crateno_stdeven if thestdfeature isn’t disabled. -
mint: Implements conversions betweenggmathandminttypes. -
serde: ImplementsSerializeandDeserializefor allggmathtypes.
§Attribution
The design of ggmath is heavily influenced by glam, as it serves the same
purpose as glam but with generics.
Most optimizations in ggmath are taken directly from glam and wide.
§License
Licensed under either Apache License Version 2.0 or MIT license at your option.
Modules§
- constants
- Traits for scalar constants like
ZERO,ONEandNAN.
Macros§
- vec2
- Constructs a 2-dimensional vector from the provided arguments.
- vec3
- Constructs a 3-dimensional vector from the provided arguments.
- vec4
- Constructs a 4-dimensional vector from the provided arguments.
Structs§
- Aligned
- Marker type indicating SIMD-aligned math types.
- Length
- Marker type representing the length of a math type.
- Unaligned
- Marker type indicating non SIMD-aligned math types.
- Vector
- A generic vector type.
Traits§
- Alignment
- Marker trait controlling SIMD alignment for math types.
- Float
Ext - An extension trait for float primitives.
- Scalar
- A trait for types that can be stored in vectors.
- Scalar
Backend - A trait to control the implementation of math types.
- Scalar
Default - A default implementation for
ScalarBackend. - Supported
Length - Marker trait restricting the length of math types.