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
Member

@nikolas nikolas commented Aug 6, 2019

No description provided.

@nikolas nikolas changed the title Codechange: math_func.hpp - use cpp-style casts Codechange: math functions - use cpp-style casts Aug 6, 2019
@glx22
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
Copy link
Member 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.

@glx22
Copy link
Contributor

glx22 commented Aug 6, 2019

Please restore the indentation between # and define

@LordAro
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
Copy link
Member 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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Close enough

@LordAro LordAro merged commit 28e1162 into OpenTTD:master Aug 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants