Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: math functions - use cpp-style casts #7685

Merged
merged 1 commit into from Aug 31, 2019

Conversation

@nikolas
Copy link
Contributor

nikolas commented Aug 6, 2019

No description provided.

@nikolas nikolas force-pushed the nikolas:cpp-cast-mathfunc branch from 67ee372 to 7659155 Aug 6, 2019
@nikolas nikolas changed the title Codechange: math_func.hpp - use cpp-style casts Codechange: math functions - use cpp-style casts Aug 6, 2019
@nikolas nikolas force-pushed the nikolas:cpp-cast-mathfunc branch from 7659155 to d838605 Aug 6, 2019
@glx22

This comment has been minimized.

Copy link
Contributor

glx22 commented Aug 6, 2019

Of course commit checker fails because # should move to first column when the line is modified.
<tabs>#xxx must now be #<tabs>xxx

@nikolas

This comment has been minimized.

Copy link
Contributor Author

nikolas commented Aug 6, 2019

Ah i see, i just found an old conversation about this here: #7407 (comment)

I'll make that change.

@nikolas nikolas force-pushed the nikolas:cpp-cast-mathfunc branch from d838605 to c018e49 Aug 6, 2019
@glx22

This comment has been minimized.

Copy link
Contributor

glx22 commented Aug 6, 2019

Please restore the indentation between # and define

@nikolas nikolas force-pushed the nikolas:cpp-cast-mathfunc branch from c018e49 to 78dc3dc Aug 6, 2019
@nikolas nikolas force-pushed the nikolas:cpp-cast-mathfunc branch from 78dc3dc to ed211aa Aug 7, 2019
@LordAro

This comment has been minimized.

Copy link
Member

LordAro commented Aug 13, 2019

Not that I necessarily disagree with the changes, but I'd like to see some justification for them. What problem do they solve? How do they improve things?

@nikolas

This comment has been minimized.

Copy link
Contributor Author

nikolas commented Aug 13, 2019

Using C++ style casts makes the intention of the code more explicit:

The problem with C casts is the ambiguity of the operation; sometimes you are doing a conversion (e.g., (int)3.5) and sometimes you are doing a cast (e.g., (int)"hello"). Brace initialization and C++ casts can often help avoid this ambiguity. Additionally, C++ casts are more visible when searching for them.

From: https://google.github.io/styleguide/cppguide.html#Casting

That said, maybe this should be an all-or-nothing style decision. C++ casts could be used everywhere if that's decided to be an improvement, otherwise just keep everything as it is.

@@ -383,7 +383,7 @@ static inline T ROR(const T x, const uint8 n)
{
#if !defined(__ICC) && defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ >= 3))
/* GCC >= 4.3 provides a builtin, resulting in faster code */
return (uint32)__builtin_bswap32((int32)x);
return static_cast<uint32>(__builtin_bswap32(static_cast<int32>(x)));

This comment has been minimized.

Copy link
@LordAro

LordAro Aug 17, 2019

Member

According to https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html, the cast to int32 is unnecessary (or possibly wrong)

Copy link
Member

LordAro left a comment

Close enough

@LordAro LordAro merged commit 28e1162 into OpenTTD:master Aug 31, 2019
8 checks passed
8 checks passed
OpenTTD CI Build #20190807.4 succeeded
Details
OpenTTD CI (Linux commit-checker) Linux commit-checker succeeded
Details
OpenTTD CI (Linux linux-amd64-clang-3.8) Linux linux-amd64-clang-3.8 succeeded
Details
OpenTTD CI (Linux linux-amd64-gcc-6) Linux linux-amd64-gcc-6 succeeded
Details
OpenTTD CI (Linux linux-i386-gcc-6) Linux linux-i386-gcc-6 succeeded
Details
OpenTTD CI (MacOS) MacOS succeeded
Details
OpenTTD CI (Windows Win32) Windows Win32 succeeded
Details
OpenTTD CI (Windows Win64) Windows Win64 succeeded
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.