perf(hex8): elide exact zero paired cross products
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../math/high_low_arithmetic.h"
|
||||
#include "hex8_geometry.h"
|
||||
#include "hex8_pair_arithmetic.h"
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
@@ -18,7 +19,7 @@ HighLow operator-(HighLow a, HighLow b) {
|
||||
return math_internal::Subtract(a, b);
|
||||
}
|
||||
HighLow operator*(HighLow a, HighLow b) {
|
||||
return math_internal::Multiply(a, b);
|
||||
return hex8_internal::MultiplyPair(a, b);
|
||||
}
|
||||
HighLow operator/(HighLow a, HighLow b) {
|
||||
return math_internal::Divide(a, b);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef FESA_ELEMENTS_HEX8_PAIR_ARITHMETIC_H_
|
||||
#define FESA_ELEMENTS_HEX8_PAIR_ARITHMETIC_H_
|
||||
|
||||
#include "../math/high_low_arithmetic.h"
|
||||
|
||||
namespace fesa::hex8_internal {
|
||||
/// @brief Omits exact-zero cross products without changing nonzero association.
|
||||
/// @note A retained zero addition performs the original canonicalization and
|
||||
/// invalid-result check; its repeated application is idempotent.
|
||||
inline math_internal::HighLow MultiplyPair(math_internal::HighLow left,
|
||||
math_internal::HighLow right) {
|
||||
using namespace math_internal;
|
||||
if (!IsNormalized(left) || !IsNormalized(right)) return InvalidHighLow();
|
||||
if (left.low != 0 && right.low != 0) return Multiply(left, right);
|
||||
auto product = TwoProduct(left.high, right.high);
|
||||
if (right.low != 0) {
|
||||
product = Add(product, TwoProduct(left.high, right.low));
|
||||
} else if (left.low != 0) {
|
||||
// The original zero addition precedes the nonzero low-high term.
|
||||
product = Add(product, HighLow{});
|
||||
}
|
||||
if (left.low != 0)
|
||||
product = Add(product, TwoProduct(left.low, right.high));
|
||||
return Add(product, HighLow{});
|
||||
}
|
||||
} // namespace fesa::hex8_internal
|
||||
|
||||
#endif // FESA_ELEMENTS_HEX8_PAIR_ARITHMETIC_H_
|
||||
Reference in New Issue
Block a user