mamedev / mame

MAME
https://www.mamedev.org/
Other
7.76k stars 1.95k forks source link

'lround' is not a member of 'std' #12458

Closed 0-wiz-0 closed 3 weeks ago

0-wiz-0 commented 3 weeks ago

MAME version

0.266

System information

NetBSD 10.99.10/amd64 gcc 10.5.0

INI configuration details

n/a

Emulated system/software

n/a

Incorrect behaviour

The build of mame fails with:

../../../../../src/osd/modules/input/input_sdl.cpp:891:16: error: 'lround' is not a member of 'std'; did you mean 'lround'?

Expected behaviour

Build succeeds.

Steps to reproduce

Build mame on NetBSD.

Additional details

I use this patch to work around this problem - please let me know if it's not the correct solution:

--- src/osd/modules/input/input_sdl.cpp.orig    2024-05-29 17:42:54.000000000 +0000
+++ src/osd/modules/input/input_sdl.cpp
@@ -786,8 +786,8 @@ public:
                case SDL_MOUSEWHEEL:
                        // adjust SDL 1-per-click to match Win32 120-per-click
 #if SDL_VERSION_ATLEAST(2, 0, 18)
-                       m_v += std::lround(event.wheel.preciseY * 120 * input_device::RELATIVE_PER_PIXEL);
-                       m_h += std::lround(event.wheel.preciseX * 120 * input_device::RELATIVE_PER_PIXEL);
+                       m_v += lround(event.wheel.preciseY * 120 * input_device::RELATIVE_PER_PIXEL);
+                       m_h += lround(event.wheel.preciseX * 120 * input_device::RELATIVE_PER_PIXEL);
 #else
                        m_v += event.wheel.y * 120 * input_device::RELATIVE_PER_PIXEL;
                        m_h += event.wheel.x * 120 * input_device::RELATIVE_PER_PIXEL;
@@ -888,8 +888,8 @@ public:
                case SDL_MOUSEWHEEL:
                        // adjust SDL 1-per-click to match Win32 120-per-click
 #if SDL_VERSION_ATLEAST(2, 0, 18)
-                       m_v += std::lround(event.wheel.preciseY * 120 * input_device::RELATIVE_PER_PIXEL);
-                       m_h += std::lround(event.wheel.preciseX * 120 * input_device::RELATIVE_PER_PIXEL);
+                       m_v += lround(event.wheel.preciseY * 120 * input_device::RELATIVE_PER_PIXEL);
+                       m_h += lround(event.wheel.preciseX * 120 * input_device::RELATIVE_PER_PIXEL);
 #else
                        m_v += event.wheel.y * 120 * input_device::RELATIVE_PER_PIXEL;
                        m_h += event.wheel.x * 120 * input_device::RELATIVE_PER_PIXEL;
cuavas commented 3 weeks ago

The title of this issue is factually incorrect – lround is a member of namespace std (round and return long int, overloaded for standard floating point types): https://en.cppreference.com/w/cpp/numeric/math/round

This is implemented correctly in GNU libstdc++ and llvm libc++. The patch is not correct.

jackson2k2 commented 3 weeks ago

You mentioned using NetBSD. Is this with pkgsrc gcc or native GCC? Please take it to the maintainers of those repositories to solve this issue.

0-wiz-0 commented 3 weeks ago

I reported this to a NetBSD mailing list, and the following test program works fine:

#include <cmath>
int main(int argc, char **argv)
{
        long double v = 1.7;
        auto a = std::lround(v);
        return a;
}

However, when cmath is not included, I get:

i.cc: In function ‘int main(int, char**)’:
i.cc:5:23: error: ‘lround’ is not a member of ‘std’
    5 |         auto a = std::lround(v);
      |                       ^~~~~~

I'm currently testing this patch instead:

--- src/osd/modules/input/input_sdl.cpp.orig    2024-05-29 17:42:54.000000000 +0000
+++ src/osd/modules/input/input_sdl.cpp
@@ -31,6 +31,7 @@
 #include <SDL2/SDL.h>

 #include <algorithm>
+#include <cmath>
 #include <cctype>
 #include <chrono>
 #include <cstddef>

and will let you know when the compilation finishes.

0-wiz-0 commented 3 weeks ago

Yes, adding that header fixes the build for me, and https://en.cppreference.com/w/cpp/numeric/math/round that you cited says you should include that - please add it! Thanks.

rb6502 commented 3 weeks ago

Thanks, this has been added in https://github.com/mamedev/mame/commit/c49f62b4b34e819e27e635d5a2edf07dbed888ad