Repository: opencv
Issue: OpenCV can't be built on a platform with int32_t == long int
Labels: bug,RFC,effort: ∞

### System Information

OpenCV version: 4.6.0
Build platform: Linux, macOS (doesn't matter)
Target platform: http://github.com/espressif/esp-idf (RTOS)
Compiler & compiler version: GCC 11.2.0 for 32-bit bare-metal `arm`, `riscv32` or `xtensa`.

### Detailed description

There are several 32-bit embedded platforms where `int32_t` is defined as `long int` rather than the more usual `int`. Although in many cases C++ performs implicit conversions betwee int and long, there are some cases where this is not the case. One example is template specializations (which are used in OpenCV a lot):

https://godbolt.org/z/M8Gdr7YKM

When built with ARM gcc ("none", i.e. bare metal), this will fail on the attempt to pass `unsigned int` value to function which is only specialized for uint32_t.

In OpenCV source code, this leads to a bunch of errors. I'm not listing all of them, here is just one representative case:
<details><summary>(error log)</summary>

```
/home/user/work/opencvlib/opencv/modules/imgproc/src/color_lab.cpp:3702:39: error: invalid conversion from 'int32_t*' {aka 'long int*'} to 'const int*' [-fpermissive]
 3702 |             x[k] = vx_load_aligned(vx + k*vsize/4);
      |                                    ~~~^~~~~~~~~~~
      |                                       |
      |                                       int32_t* {aka long int*}
In file included from /home/user/work/opencvlib/opencv/modules/imgproc/src/color_lab.cpp:13:
/home/user/work/opencvlib/opencv/modules/core/include/opencv2/core/hal/intrin.hpp:555:21: note: candidate: 'cv::hal_baseline::simd128_cpp::v_uint32 cv::hal_baseline::simd128_cpp::vx_load_aligned(const unsigned int*)' (near match)
  555 |     inline v_uint32 vx_load_aligned(const unsigned * ptr) { return VXPREFIX(_load_aligned)(ptr); }
      |                     ^~~~~~~~~~~~~~~
/home/user/work/opencvlib/opencv/modules/core/include/opencv2/core/hal/intrin.hpp:555:21: note:   conversion of argument 1 would be ill-formed:
/home/user/work/opencvlib/opencv/modules/imgproc/src/color_lab.cpp:3702:39: error: invalid conversion from 'int32_t*' {aka 'long int*'} to 'const unsigned int*' [-fpermissive]
 3702 |             x[k] = vx_load_aligned(vx + k*vsize/4);
      |                                    ~~~^~~~~~~~~~~
      |                                       |
      |                                       int32_t* {aka long int*}
```
</details>

### Background and possible fixes

I found that a while ago, in https://github.com/opencv/opencv/pull/10263 some support for platforms with `int32_t == long` has been added. It can be enabled by defining `CV_INT32_T_IS_LONG_INT` macro. However since builds for such platforms aren't tested regularly, I guess this has regressed in recent years. Adding this macro doesn't fix the compilation errors reported in this issue.

We have draft commits for [opencv](https://github.com/opencv/opencv/commit/02cdb16d792d92a0e750529a7869f1fb672b328b) and [opencv-contrib](https://github.com/opencv/opencv_contrib/commit/de81ed6977ef575455b6a998eefd800aed17dd15) which seem to fix the issue. The changes required are not huge. I can open PRs for these changes if you think this is a valid issue which deserves to be fixed upstream. I could also submit a PR with a CI job to keep the builds for such platforms verified.

### Steps to reproduce

Here is one way to reproduce the issue, using RISC-V embedded GCC toolchain. Same would apply to other 32-bit embedded targets supported by GCC, for example ARM or Xtensa.

1. Download the toolchain from https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/tag/v11.3.0-1/
2. Extract it, add it to PATH
3. Run CMake using the command line shown below
4. Run cmake --build .

<details><summary>CMake command line</summary>

```
cmake -S ../opencv -B . -DCMAKE_TOOLCHAIN_FILE=toolchain-riscv32.cmake  \
        -DOPENCV_FORCE_3RDPARTY_BUILD=1 \
        -DCMAKE_SYSTEM_PROCESSOR=embedded \
        -DENABLE_PIC=0 \
        -DTARGET=esp \
        -DWITH_ADE=0 \
        -DWITH_JPEG=0 \
        -DWITH_OPENCL=0 \
        -DWITH_OPENEXR=0 \
        -DWITH_OPENGL=0 \
        -DWITH_OPENMP=0 \
        -DWITH_PNG=1 \
        -DWITH_PROTOBUF=0 \
        -DWITH_TIFF=0 \
        -DWITH_WEBP=0 \
        -DBUILD_EXAMPLES=0 \
        -DBUILD_PACKAGE=0 \
        -DBUILD_PERF_TESTS=0 \
        -DBUILD_PNG=1 \
        -DBUILD_SHARED_LIBS=0 \
        -DBUILD_TESTS=0 \
        -DBUILD_ZLIB=1 \
        -DBUILD_opencv_apps=0 \
        -DBUILD_opencv_bgsegm=0 \
        -DBUILD_opencv_datasets=0 \
        -DBUILD_opencv_imgcodecs=0 \
        -DBUILD_opencv_imgproc=1 \
        -DBUILD_opencv_tracking=0 \
        -DBUILD_opencv_video=0 \
        -DBUILD_opencv_videoio=0 \
        -DBUILD_opencv_xfeatures2d=0 \
        -DBUILD_opencv_ximgproc=0
```
</details>

<details><summary>toolchain-riscv32.cmake</summary>

```
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER riscv32-esp-elf-gcc)
set(CMAKE_CXX_COMPILER riscv32-esp-elf-g++)
set(CMAKE_ASM_COMPILER riscv32-esp-elf-gcc)
set(_CMAKE_TOOLCHAIN_PREFIX riscv32-esp-elf-)

set(CMAKE_C_FLAGS "-march=rv32imc" CACHE STRING "C Compiler Base Flags")
set(CMAKE_CXX_FLAGS "-march=rv32imc" CACHE STRING "C++ Compiler Base Flags")
set(CMAKE_EXE_LINKER_FLAGS "-nostartfiles -march=rv32imc --specs=nosys.specs" CACHE STRING "Linker Base Flags")
```
</details>

This should produce the errors similar to:
<details><summary>example error message</summary>

```
/Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:3537:107: error: call of overloaded 'softdouble(int)' is ambiguous
 3537 | static const float64_t exp_prescale = float64_t::fromRaw(0x3ff71547652b82fe) * float64_t(1 << EXPTAB_SCALE);
      |                                                                                                           ^
In file included from /Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:68:
/Users/ivan/e/opencv/opencv/modules/core/include/opencv2/core/softfloat.hpp:257:14: note: candidate: 'cv::softdouble::softdouble(double)'
  257 |     explicit softdouble( const double a ) { Cv64suf s; s.f = a; v = s.u; }
      |              ^~~~~~~~~~
/Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:248:1: note: candidate: 'cv::softdouble::softdouble(int64_t)'
  248 | softdouble::softdouble( const  int64_t a ) { *this =  i64_to_f64(a); }
      | ^~~~~~~~~~
/Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:247:1: note: candidate: 'cv::softdouble::softdouble(int32_t)'
  247 | softdouble::softdouble( const  int32_t a ) { *this =  i32_to_f64(a); }
      | ^~~~~~~~~~
/Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:246:1: note: candidate: 'cv::softdouble::softdouble(uint64_t)'
  246 | softdouble::softdouble( const uint64_t a ) { *this = ui64_to_f64(a); }
      | ^~~~~~~~~~
/Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:245:1: note: candidate: 'cv::softdouble::softdouble(uint32_t)'
  245 | softdouble::softdouble( const uint32_t a ) { *this = ui32_to_f64(a); }
      | ^~~~~~~~~~
In file included from /Users/ivan/e/opencv/opencv/modules/core/src/softfloat.cpp:68:
/Users/ivan/e/opencv/opencv/modules/core/include/opencv2/core/softfloat.hpp:232:5: note: candidate: 'cv::softdouble::softdouble(const cv::softdouble&)'
  232 |     softdouble( const softdouble& c) { v = c.v; }
      |     ^~~~~~~~~~
```
</details>

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)