Originally posted by michal
Announcement
Collapse
No announcement yet.
C++20 Making Progress On Modules, Memory Model Updates
Collapse
X
-
-
Originally posted by DavidBrown View PostYes, I know. Many people don't. That is another good reason to insist on using "x / 8" when you mean divide by 8 - the compiler will get it right, while someone "optimising" it to "x >> 3" will get it wrong. They should have written "(x + ((x < 0) ? 7 : 0)) >> 3".
(If you like, you can disagree with the C standard's decision to define division as truncate towards 0. I would be just as happy with either type of truncation. But that is the way it is defined in C.)
Now, it actually makes even less sense to do x / 8 if, for example, you use it to truncate some bit offsets to byte offsets. Not only will the generated code be terrible, it will also be wrong. Consider what happens when your bit offset is 7, you end up with 0, all the way down to 0 (so 8 values). When it's 8, you end up with 1. But when it's -1, you end up with... 0 still, when it should have been -1. Now you have 15 offsets that all go to 0. Why is 0 special, again?
Originally posted by DavidBrown View PostIt is quite easy, and compilers have been optimising it for a very long time.
Originally posted by DavidBrown View PostYou are right that division of signed integers by powers of two usually requires a couple of instructions more than a right-shift would have done - but division makes it easier for the compiler to do other reasoning and other simplifications or optimisations. You win some, you lose some.
Originally posted by DavidBrown View PostNo matter what gets generated, the small details of code generation here are an irrelevant detail compared to the improvement in code readability and correctness.
What is "code correctness"? Wrong code?
Originally posted by DavidBrown View Post"People like you" ? You don't like the discussion, so you want to reduce it to insults?
It doesn't matter how well someone disagrees, you can just pull the "code readability" or "maintainability" card, and that's it. There is no discussion when using those words because they are just opinions.
Buit guess what? I can pull it too. x >> 3 is more maintainable to me and more readable in some cases like the above.
So what's the point of discussing then?
Originally posted by DavidBrown View PostI do understand this, I am not parroting, and it is not crap. The C world is full of truly dreadful code written by smart-arses who think "bit twiddling tricks" are a good idea. There are very occasional cases where you want to do everything possible to get the very fastest code, and where tricks or "clever" code makes sense. But those cases are rare - for the vast majority of code, you will get more efficient results by writing clean, clear, readable code that says naturally what you mean, and letting the compiler optimise the details. The big win, of course, is that the code is readable, maintainable, correct, and easily seen to be correct.
The entire world is full of truly dreadful code written by people who code like retards will see their code and must make sure retards will understand it. No wonder software is more dreadful as more time passes, we are losing the "smart-asses" and the "bit twiddling hacks" people who coded proper software (not due to the bit hacks, but because by coding the hacks, it proved that they are COMPETENT).
I've LITERALLY seen people who use a freaking LOOP to add a constant up instead of a multiplication, "because it is more readable and easier for first grade drop outs". "Code like a 6 year old is going to read it".
I mean, clearly multiplication is too hard for some people, just like right shift. It's evil. Use addition loops instead.
Seriously, I can say the exact same thing you said, but use x >> 3 as "readable, maintainable, correct, and easily seen to be correct". It's not my fault other people are too dumb to understand the beauty in "x >> 3" maybe they should let go of their prejudice and start learning C properly.
Remember one thing. Software is not meant to be read by incompetents. The world has shitter software than ever because people have this misconception that anyone -- even a first schooler dropout -- should understand the code and be "as simple as possible". No, software, especially C, needs to be written for competent people only.
In some cases yes I do agree -- some hacks or clever hacks can be unmaintainable (hard to change). I agree there. But a right shift? It literally does NOT require more thinking process than a division.
The fact that you think it does only shows how dumb some programmers are that they won't even learn the operations properly (remember the multiplication above, same thing). Such a thing must NOT be encouraged. Filter them out.
Originally posted by DavidBrown View PostI can certainly agree that the compiler can generate tighter code for unsigned divisions than for signed divisions, if that is what you mean. But yes, optimising signed integer division is easy for compilers.Last edited by Weasel; 15 November 2018, 01:15 PM.
Leave a comment:
-
Originally posted by michal
writing cross platform code is possible in languages that don't have a pre processor.
The closest you can get is deriving i.e from Socket and having something like PosixSocket and WinsockSocket and then using the build system to do the cross platform stuff for you by including one or the other. This still isn't cross platform code. This is a cross platform build system .
Though you can add a preprocessor to most languages. Even to replace the really weak one in C#. Just pass the source files through CPP (C Preprocessor) or M4 as part of the build system.Last edited by kpedersen; 15 November 2018, 10:39 AM.
- Likes 2
Leave a comment:
-
Originally posted by michalwriting cross platform code is possible in languages that don't have a pre processor.
Can they use alternative standard libraries?
Can they change allocation strategies (per container)?
The more assumptions you make, the more restricted you are. C/C++ certainly are not in the Goldilocks zone for desktops or modern mobile systems, but they are still the one language that will be able to run on the more exotic platforms.
Want a restricted environment for C/C++, then use a framework like Qt which is easier to deal with.
- Likes 2
Leave a comment:
-
Originally posted by andreano View Post
It's not just about right shift. Comparions between signed and unsigned integers triggers the dreaded -Wsign-compare warning on gcc, even for equality comparisons (==, !=), which, when you know that the target CPU is two's complement, does exactly the same for signed and unsigned integers – a false positive!
Now that the compiler agrees that the target CPU is two's complement, I'm hoping we can finally compile with -Wextra without seeing that warning inappropriately.
Don't get me wrong here - I like the restriction to two's complement being the only representation supported by modern C and C++. I'd like padding bits to be banned too (except for bool), char to be restricted to 8, 16 or 32 bits, and int to be 16-bit or 32-bit. I don't think this will make a noticeable difference to most programming, but it will mean that code that relies on these features today (most code handling any kind of binary interchange of data) will be viewed as more portable.
The big fear here is that this change will mean some people will think that signed integer overflow is now defined as two's complement wrapping - it is not, and it should not be.
- Likes 1
Leave a comment:
-
Originally posted by Weasel View Post...and apparently not easy enough for people like you to understand.
Right shift truncates towards -inf for negative numbers, while division truncates towards 0.
(If you like, you can disagree with the C standard's decision to define division as truncate towards 0. I would be just as happy with either type of truncation. But that is the way it is defined in C.)
So no, it's actually not easy AT ALL for the compiler to optimize. If it can't prove the numbers are strictly positive (in which case you should shoot yourself for using a signed type), then it will generate about 4 instructions for what should be just 1.
No matter what gets generated, the small details of code generation here are an irrelevant detail compared to the improvement in code readability and correctness.
But people like you just parrot and preach crap they don't understand (but WANT to believe it's true) and pollute sites like stackoverflow with wrong information.
I do understand this, I am not parroting, and it is not crap. The C world is full of truly dreadful code written by smart-arses who think "bit twiddling tricks" are a good idea. There are very occasional cases where you want to do everything possible to get the very fastest code, and where tricks or "clever" code makes sense. But those cases are rare - for the vast majority of code, you will get more efficient results by writing clean, clear, readable code that says naturally what you mean, and letting the compiler optimise the details. The big win, of course, is that the code is readable, maintainable, correct, and easily seen to be correct.
x / 8 truly generates worse code for signed integers, and it only gets worse with other types of divisors (non power of 2). Keep believing it's "easy" for the compiler to optimize tho.
- Likes 1
Leave a comment:
-
Originally posted by Kushan View Post
The preprocessor isn't the issue, the issue is that it's utterly abused. It's simple, but often used for complicated gubbins that nobody understands and is incredibly brittle because the preprocessor is not in line with the goals and ideals of C++ itself (i.e. it's not type safe).
Other languages do it better - they still have your #if statements but that's it, that's all you need to give yourself flexibility and configuration. For everything else, use constexpr.
Originally posted by michal
writing cross platform code is possible in languages that don't have a pre processor.
Leave a comment:
-
Originally posted by DavidBrown View Postno one sane uses right-shift on negative integers anyway.
Now that the compiler agrees that the target CPU is two's complement, I'm hoping we can finally compile with -Wextra without seeing that warning inappropriately.Last edited by andreano; 14 November 2018, 06:23 PM.
- Likes 1
Leave a comment:
-
Originally posted by paulpach View PostBy that logic, why duplicate it only once? you should be required to write the same code 10 times to be extra sure that you did not make a mistake.
There's no gain going from 2 to 3 because the moment you changed it TWICE, you already are aware of the change.
- Likes 1
Leave a comment:
-
Originally posted by Weasel View PostCopy-paste is a thing. Duplicating it is actually better because it will give an error when they mismatch later so you know you changed the interface which is a big deal.
Leave a comment:
Leave a comment: