Skip to content

normL1 does not give consistent results with different compilers and optimization levels #27080

@fengyuentau

Description

@fengyuentau

System Information

Latest 5.x.

Detailed description

I was working on normL1 on 32s input data and found that the existing kernel

template<typename _Tp, typename _AccTp> static inline
_AccTp normL1(const _Tp* a, const _Tp* b, int n)
{
_AccTp s = 0;
for( int i = 0; i < n; i++ )
s += (_AccTp)cv_absdiff(a[i], b[i]);
return s;
}

, which calls cv_absdiff<int>

inline unsigned cv_absdiff(int x, int y) { return (unsigned)(std::max(x, y) - std::min(x, y)); }

, cannot produce consistent results with different compilers and optimization levels.

Reproducer is attached below. Here is the summary with the reproducer.

  • cv_absdiff:
Compiler -O0 -O3
x64, gcc-12.3.0 n=11074883890230.000000 n=6878700842038.000000
arm64, apple clang 16.0.0 n=11074883890230.000000 n=11073593898738.000000
risc-v, gcc-14.2.0 n=11074883890230.000000 n=11074883890230.000000
risc-v, clang-18.0.0 n=11074883890230.000000 n=8302375796980.000000
  • A improved version in demo:
Compiler -O0 -O3
x64, gcc-12.3.0 n=11074883890230.000000 n=11074883890230.000000
arm64, apple clang 16.0.0 n=11074883890230.000000 n=11074883890230.000000
risc-v, gcc-14.2.0 n=11074883890230.000000 n=11074883890230.000000
risc-v, clang-18.0.0 n=11074883890230.000000 n=11074883890230.000000

Steps to reproduce

Download

main.cpp.zip

, extract, compile and run with the following commands.

g++ -std=c++11 main.cpp
./a.out

g++ -std=c++11 -O3 main.cpp
./a.out

Note that input data is collected from opencv performance tests.

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions