Skip to content

bgsegm: Enable __popcnt intrinsic for MSVC on ARM to fix LSBP test failure - #3953

Merged
asmorkalov merged 1 commit into
opencv:4.xfrom
pratham-mcw:fix/bgsegm-lsbp-arm-failure
Sep 10, 2025
Merged

asmorkalov merged 1 commit into
opencv:4.xfrom
pratham-mcw:fix/bgsegm-lsbp-arm-failure

Conversation

@pratham-mcw

Copy link
Copy Markdown
Contributor

This PR enables use of the __popcnt intrinsic for ARM/ARM64 targets when building with MSVC. Previously, these targets(ARM/ARM64) were explicitly excluded using macro guards.

When building OpenCV on Windows on ARM64 with MSVC (cl.exe), the BackgroundSubtractor_LSBP.Accuracy test fails because the code falls into the else block using different bit counting method, which leads to incorrect results and causes the test to fail.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov

Copy link
Copy Markdown
Contributor

I do not see __popcnt support for ARM in the Microsoft documentation: https://learn.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64?view=msvc-170. It describes x86 specific.

@Kumataro

Copy link
Copy Markdown
Contributor

Hello, at Microsoft blog, Visual Studio 2022 v17.11 and later seems to support __popcnt family instructions for ARM64 platform (not ARM32).

See https://devblogs.microsoft.com/cppblog/msvc-backend-updates-in-visual-studio-2022-version-17-11/

Support popcnt instructions, __popcnt, popcnt16, and popcnt64, thanks to our friends at ARM.

To relax this macro guards for ARM64, We have to validate MSVC version.

@asmorkalov

Copy link
Copy Markdown
Contributor

ok, msvs version check should be added then

Comment thread modules/bgsegm/src/bgfg_gsoc.cpp Resolved
@asmorkalov asmorkalov self-assigned this Sep 9, 2025
@asmorkalov
asmorkalov merged commit 095981c into opencv:4.x Sep 10, 2025
10 of 12 checks passed
@asmorkalov asmorkalov mentioned this pull request Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants