Repository: opencv/opencv
File: modules/dnn/src/layers/is_inf_layer.cpp (C++)

Code before:
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Copyright (C) 2025, BigVision LLC, all rights reserved.
// Third party copyrights are property of their respective owners.

#include "../precomp.hpp"
#include "layers_common.hpp"
#include "opencv2/core/fast_math.hpp"  // for cvIsInf

namespace cv {
namespace dnn {

template <typename T>
static inline void computeIsInfMask(const T* src, uchar* dst, const size_t count, const bool detectPositive, const bool detectNegative)
{
    for (size_t i = 0; i < count; ++i)
    {
        const T v = src[i];
        const bool pos = cvIsInf(v) && (v > 0) && detectPositive;
        const bool neg = cvIsInf(v) && (v < 0) && detectNegative;
        dst[i] = static_cast<uchar>(pos || neg);
    }
}

class IsInfLayerImpl CV_FINAL : public IsInfLayer
{
    bool detect_pos = true, detect_neg = true;

public:
    IsInfLayerImpl(const LayerParams& params)
    {
        setParamsFrom(params);
        detect_pos = params.get<bool>("detect_positive", true);
        detect_neg = params.get<bool>("detect_negative", true);
    }

    bool supportBackend(int backendId) CV_OVERRIDE
    {
        return backendId == DNN_BACKEND_OPENCV;
    }

    bool getMemoryShapes(const std::vector<MatShape>& inputs, int,
                         std::vector<MatShape>& outputs,
                         std::vector<MatShape>&) const CV_OVERRIDE

Review comment (refactor):
I would split this into different loops:

```
if (detectPositive && detectNegative) {
   for (...) ... // just check for cvIsInf()
} else if (detectPositive) {
   for (...) ... // cvIsInf(v) && v > 0
} else if (detectNegative) {
   for (...) ... // cvIsInf(v) && v < 0
} else {
   // report error?
}
```


Code after:
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Copyright (C) 2025, BigVision LLC, all rights reserved.
// Third party copyrights are property of their respective owners.

#include "../precomp.hpp"
#include "layers_common.hpp"
#include "opencv2/core/fast_math.hpp"  // for cvIsInf

namespace cv {
namespace dnn {

/*
    IsInf layer, as defined in ONNX specification:
    https://onnx.ai/onnx/operators/onnx__IsInf.html

    Opset's 10 to 20 are covered.
*/

template <typename T, typename WT = T>
static inline void computeIsInfMask(const T* src, uchar* dst, const size_t count, const bool detectPositive, const bool detectNegative)
{
    if (detectPositive && detectNegative)
    {
        parallel_for_(Range(0, (int)count), [&](const Range& r){
            for (int i = r.start; i < r.end; ++i)
            {
                WT v = (WT)src[i];
                dst[i] = static_cast<uchar>(cvIsInf(v));
            }
        });
    }
    else if (detectPositive)
    {
        parallel_for_(Range(0, (int)count), [&](const Range& r){
            for (int i = r.start; i < r.end; ++i)
            {
                WT v = (WT)src[i];
                dst[i] = static_cast<uchar>(cvIsInf(v) && (v > 0));
            }
        });
    }
    else if (detectNegative)
    {