XMath - Utility Functions for Mathematical Operations
Path: src/modbus/xmath.h
Revision History:
- Initial documentation
A lightweight utility header that provides essential mathematical operations for embedded systems. It includes a template-based clamp function and macros for range checking and normalized clamping.
REQUIREMENTS
Section titled “REQUIREMENTS”- C++17 compiler support
- No hardware-specific requirements
PROVIDES
Section titled “PROVIDES”clamp<T>()
- Template function for clamping values within specified boundsRANGE()
- Macro for checking if a value is within a specified rangeNCLAMP()
- Macro for normalizing a value within a range to [0.0, 1.0]
FEATURES
Section titled “FEATURES”- Type-generic clamping through template implementation
- Range checking for value validation
- Value normalization to a [0.0, 1.0] range
- Conditional compilation for C++ environments
- No standard library dependencies
DEPENDENCIES
Section titled “DEPENDENCIES”- None
BEHAVIOUR
Section titled “BEHAVIOUR”The XMath utilities provide deterministic mathematical operations:
PERFORMANCE
Section titled “PERFORMANCE”- Consider specialized implementations for common numeric types to avoid template instantiation overhead
- Evaluate potential for SIMD optimizations on compatible platforms
SECURITY
Section titled “SECURITY”- No known security concerns as the utilities perform pure mathematical operations without side effects
COMPLIANCE
Section titled “COMPLIANCE”- Ensure all mathematical operations maintain expected precision for safety-critical applications
- Verify behavior with edge cases (e.g., handling of NaN, Inf values)
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- Use
clamp<T>()
when working with specific types to leverage compile-time type checking - Prefer the NCLAMP macro when normalizing sensor readings or other input values to a standardized range
- Consider adding additional mathematical utilities as needed for specific application domains