fix(hex8): bridge nonfinite vector sums before error arithmetic

This commit is contained in:
KOKO\Mimi
2026-10-14 23:14:21 +09:00
parent 96772651d8
commit 0c29a9f8d8
2 changed files with 97 additions and 14 deletions
+32 -14
View File
@@ -124,9 +124,14 @@ PairPacket Store(VectorPair value) {
return {{{high[0], low[0]}, {high[1], low[1]},
{high[2], low[2]}, {high[3], low[3]}}};
}
/// @brief Retains the original per-lane subtraction/addition association and gates.
VectorPair TwoSum(__m256d left, __m256d right) {
/// @brief Retains finite-sum error association; false requests original graph.
/// @note False leaves output unreadable and preserves the necessary sum flags.
/// Prefix flags still require optimized scalar-oracle/codegen validation.
bool TwoSum(__m256d left, __m256d right, VectorPair* output) {
const auto sum = _mm256_add_pd(left, right);
// Do not speculate Inf-Inf error arithmetic beyond a failed first sum.
// The caller retains original inputs for its existing baseline bridge.
if (_mm256_movemask_pd(Finite(sum)) != 15) return false;
const auto bridge = _mm256_sub_pd(sum, left);
const auto restored = _mm256_sub_pd(sum, bridge);
const auto error = _mm256_add_pd(_mm256_sub_pd(left, restored),
@@ -134,8 +139,9 @@ VectorPair TwoSum(__m256d left, __m256d right) {
const auto valid = _mm256_and_pd(_mm256_and_pd(Finite(sum), Finite(bridge)),
_mm256_and_pd(Finite(restored), Finite(error)));
const auto exact_zero = _mm256_and_pd(Zero(sum), Zero(error));
return InvalidOutside({Select(exact_zero, _mm256_setzero_pd(), sum),
Select(exact_zero, _mm256_setzero_pd(), error)}, valid);
*output = InvalidOutside({Select(exact_zero, _mm256_setzero_pd(), sum),
Select(exact_zero, _mm256_setzero_pd(), error)}, valid);
return true;
}
/// @brief Routes exceptional packets to baseline checked Add in lane order.
VectorPair ScalarAdd(VectorPair left, VectorPair right, bool pair_shortcuts) {
@@ -167,10 +173,15 @@ VectorPair Add(VectorPair left, VectorPair right, bool pair_shortcuts) {
if (left_zero != 0 || right_zero != 0)
return ScalarAdd(left, right, true);
}
const auto head = TwoSum(left.high, right.high);
const auto tail = TwoSum(left.low, right.low);
const auto middle = TwoSum(head.low, tail.high);
const auto joined = TwoSum(head.high, middle.high);
VectorPair head;
VectorPair tail;
VectorPair middle;
VectorPair joined;
if (!TwoSum(left.high, right.high, &head) ||
!TwoSum(left.low, right.low, &tail) ||
!TwoSum(head.low, tail.high, &middle) ||
!TwoSum(head.high, middle.high, &joined))
return ScalarAdd(left, right, pair_shortcuts);
const auto remainder = _mm256_add_pd(
_mm256_add_pd(joined.low, middle.low), tail.low);
const auto valid = _mm256_and_pd(Finite(remainder), _mm256_and_pd(
@@ -179,9 +190,11 @@ VectorPair Add(VectorPair left, VectorPair right, bool pair_shortcuts) {
_mm256_and_pd(Finite(middle.high), Finite(middle.low)),
_mm256_and_pd(Finite(joined.high), Finite(joined.low))))));
// Original Add returns before its last TwoSum when a prior finite gate fails.
return InvalidOutside(TwoSum(
Select(valid, joined.high, _mm256_setzero_pd()),
Select(valid, remainder, _mm256_setzero_pd())), valid);
VectorPair result;
if (!TwoSum(Select(valid, joined.high, _mm256_setzero_pd()),
Select(valid, remainder, _mm256_setzero_pd()), &result))
return ScalarAdd(left, right, pair_shortcuts);
return InvalidOutside(result, valid);
}
/// @brief Checks finite/zero/tail premises before products; neutralizes rejected
/// lanes before the explicit residual FMA and final TwoSum.
@@ -211,9 +224,14 @@ VectorPair Product(__m256d left, __m256d right) {
const auto error = _mm256_fmsub_pd(Select(product_valid, left, one),
Select(product_valid, right, one), Select(product_valid, product, one));
const auto valid = _mm256_and_pd(product_valid, Finite(error));
return InvalidOutside(TwoSum(
Select(valid, product, _mm256_setzero_pd()),
Select(valid, error, _mm256_setzero_pd())), valid);
VectorPair result;
if (!TwoSum(Select(valid, product, _mm256_setzero_pd()),
Select(valid, error, _mm256_setzero_pd()), &result)) {
PairPacket scalar;
ScalarPacketTwoProduct(a, b, &scalar);
return Load(scalar);
}
return InvalidOutside(result, valid);
}
/// @brief Uses uniform low-zero branches with original four-product association;
/// mixed or invalid packets preserve the baseline checked path.