From ade77a5e0fa8f753769c7aa3323cafdac054d14c Mon Sep 17 00:00:00 2001 From: Jan Wassenberg Date: Thu, 9 Jan 2025 07:24:04 -0800 Subject: [PATCH] fix emu128 reduction with infinities. thanks @yohanchatelain, fixes #2434 PiperOrigin-RevId: 713663844 --- hwy/ops/emu128-inl.h | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/hwy/ops/emu128-inl.h b/hwy/ops/emu128-inl.h index 7a361de011..a530a8f9f6 100644 --- a/hwy/ops/emu128-inl.h +++ b/hwy/ops/emu128-inl.h @@ -2916,9 +2916,30 @@ HWY_API T ReduceSum(D d, VFromD v) { } return sum; } + +namespace detail { +template , HWY_IF_FLOAT_OR_SPECIAL(T)> +T InitReduceMin(D d) { + return GetLane(Inf(d)); +} +template , HWY_IF_NOT_FLOAT_NOR_SPECIAL(T)> +T InitReduceMin(D d) { + return HighestValue(); +} + +template , HWY_IF_FLOAT_OR_SPECIAL(T)> +T InitReduceMax(D d) { + return -GetLane(Inf(d)); +} +template , HWY_IF_NOT_FLOAT_NOR_SPECIAL(T)> +T InitReduceMax(D d) { + return LowestValue(); +} +} // namespace detail + template , HWY_IF_REDUCE_D(D)> HWY_API T ReduceMin(D d, VFromD v) { - T min = HighestValue(); + T min = detail::InitReduceMin(d); for (size_t i = 0; i < MaxLanes(d); ++i) { min = HWY_MIN(min, v.raw[i]); } @@ -2926,7 +2947,7 @@ HWY_API T ReduceMin(D d, VFromD v) { } template , HWY_IF_REDUCE_D(D)> HWY_API T ReduceMax(D d, VFromD v) { - T max = LowestValue(); + T max = detail::InitReduceMax(d); for (size_t i = 0; i < MaxLanes(d); ++i) { max = HWY_MAX(max, v.raw[i]); }