News for hackers. Stuff that matters.
Re:Maximal Min() and Max() by jonathrg on Thursday August 08, 2024 @11:16AM (#41192435)
That series of macros is a nice demonstration of the incredible effort it takes to attempt the most basic generic programming in C. Perhaps it would be more productive to just accept the limitations of the language and define a version of `min` and `max` for each type.
Re:Maximal Min() and Max() by asplake on Thursday August 08, 2024 @11:50AM (#41192834)
Also a demonstration of how much work other compilers do for you behind the scenes.
Re:Maximal Min() and Max() by Groxx on Thursday August 08, 2024 @11:58AM (#41192918)
Ehhh... not much more so than how much `npm install` hides. These macros are part of a gigantic tower of absurdity that costs many, many times more than they need to, but we use them because our time is more valuable than CPU time.
Re:Maximal Min() and Max() by rerdavies on Thursday August 08, 2024 @08:19PM (#41197721)
Dev time spent waiting for compiles is way more expensive than CPU time.
Re:Maximal Min() and Max() by Groxx on Thursday August 08, 2024 @11:00PM (#41198466)
And way, way, WAY less expensive than a troubleshooting session
Re:Maximal Min() and Max() by hughesjj on Thursday August 08, 2024 @08:42PM (#41197821)
Well, depends on how much CPU time we're talking about.

The Linux kernel is one of the few cases where just the sheer scale of instances may make a difference

Re:Maximal Min() and Max() by jonathrg on Thursday August 08, 2024 @12:33PM (#41193378)
A compiler for a language with generic types will definitely do less work to implement generic min/max than the C preprocessor has to do in order to perform all the text substitutions generated by these macros.
Re:Maximal Min() and Max() by asplake on Thursday August 08, 2024 @12:55PM (#41193663)
Oh totally! It has the information to do it both properly and efficiently.
Re:Maximal Min() and Max() by usr1106 on Thursday August 08, 2024 @02:36PM (#41194764)
Or a demonstration about the poor state of programming languages in 2024.

Of course there are others than C. But not many suitable for writing a kernel. And no obvious choice for what Linux could do today. Of course they are starting with Rust, but nobody can predict when that will make the last C macro unnecessary. Unless your prediction is never...

Re:Maximal Min() and Max() by pjmlp on Thursday August 08, 2024 @05:52PM (#41196728)
Plenty of kernels have been written in other languages, naturally UNIX/POSIX folks can hardly think of something else.

Even Linus accepting Rust is kind of interesting, because his C++ rants also apply to plenty of Rust code bases.

Re:Maximal Min() and Max() by gary_0 on Thursday August 08, 2024 @11:54PM (#41198645)
Doesn't Rust's `no_std` make it easy to throw out idioms you can't use (or don't like) and just keep the syntax and most of the tooling? I recall that one of Linus's preconditions for inclusion was that Rust's default of panic!ing on allocation failures had to be overrideable.

Whereas C++ tends to make you eat the whole enchilada.

Re:Maximal Min() and Max() by pjmlp on Friday August 09, 2024 @02:17AM (#41199212)
C++ compilers have similar options, e.g. VC++ has /kernel, and there is the freestanding standard as well.

Also see Embedded C++ as used by macOS IO Kit, which thankfully there isn't a Linus at Apple.

Regarding Rust, no_std, doesn't change the npm like dependencies, macro festival (with two ways of doing all kinds of stuff), abstraction party with type driven development, the compile times.

Re:Maximal Min() and Max() by tialaramex on Friday August 09, 2024 @11:53AM (#41202999)
The C++ "freestanding" even when the current round of work is completed, is nowhere close to Rust's core.

Partly this is because Rust thought about this earlier, and partly it's because of the relationship between C++ and the allocator, having operators for a feature which might not even exist on your target, awkward.

Re:Maximal Min() and Max() by pjmlp on Friday August 09, 2024 @04:55PM (#41205337)
True, doesn't change the fact that outside Linus hate for C++, other places, with more sensible persons, have chosen otherwise.
Re:Maximal Min() and Max() by gary_0 on Friday August 09, 2024 @06:29PM (#41205897)
It's not just Linus. I use C++ daily for a variety of things, but I understand the hesitance to use it in real-world kernels and embedded code. For instance: The switches to disable features like exceptions are generally considered "not supported, use at own risk"; removing them is not an official part of the language. C++ templates are slow to compile and have many obvious shortcomings — but if you avoid them, you're back to using C macros. The C++ abstract machine puts a lot of weird complications in the way, adding new UB landmines on top of C.

This doesn't mean using C++ for kernels and such is impossible, but anyone with a conservative, risk-averse mindset (like Linus) isn't going to want to touch it.

Rust, on the other hand, was deliberately designed for such uses, starting from a fresh sheet of paper. Giving systems programmers everything they want without any hidden "gotchas" is its whole raison d'être.

Re:Maximal Min() and Max() by tialaramex on Friday August 09, 2024 @10:14PM (#41206836)
I don't see how picking C++ counts as "more sensible". Linus chose C 30+ years ago because it was the only practical option. Bjarne (and some other C++ proponents) worked hard to present C++ as somehow a natural upgrade to C and that worked on Microsoft and at some of the defunct proprietary Unix vendors but it didn't work on Linus.

My strong impression is that both Apple and Microsoft are moving away from C++ in their core systems.

Re:Maximal Min() and Max() by j16sdiz on Friday August 09, 2024 @03:31AM (#41199568)
On of the reason is: They have Rust compiler developers on board. This is a "direct line" to any language features or bugs reports.

C++, otoh, is run by committee and far removed from the community. (C is not much better, but at least they are stable)

Re:Maximal Min() and Max() by pjmlp on Friday August 09, 2024 @05:45AM (#41200283)
Stable? C23 has just been approved.
Re:Maximal Min() and Max() by PaulDavisThe1st on Friday August 09, 2024 @12:44PM (#41203436)
> C++, otoh, is run by committee and far removed from the community.

It is hard to be close to a community with the size and divergence of the C++ one. It is (relatively) easy to be close to a community the size of the Rust one.

Re:Maximal Min() and Max() by kbolino on Friday August 09, 2024 @11:49AM (#41202971)
Most kernels not written in C (or stripped-down, unsafe C++, or an even-lower-level language like assembly) were written for special hardware. Such hardware was built to handle a lot of the low-level details so that the software didn't have to. This particular approach doesn't apply to modern general-purpose microprocessor-based platforms. Even if we were able to transition to such a do-more-work-in-hardware platform somehow, then we would just be trading kernel bugs for hardware bugs, and the latter aren't always fixable (even with firmware and microcode).
Re:Maximal Min() and Max() by pjmlp on Friday August 09, 2024 @04:53PM (#41205332)
Outside Bell Labs many other languages came to be.
Re:Maximal Min() and Max() by kbolino on Friday August 09, 2024 @04:59PM (#41205368)
The x86 family didn't come out of Bell Labs. It's not a question of where the programming languages came from but of the boundary line between hardware and software.
Re:Maximal Min() and Max() by lifthrasiir on Thursday August 08, 2024 @11:22AM (#41192505)
That will make `typedef` much less useful, though. C is the real problem; not the attempt to do generic programming in C. (And `_Generic` won't solve this problem anyway. Only a GCC extension of statement expressions will.)
Re:Maximal Min() and Max() by peheje on Thursday August 08, 2024 @02:50PM (#41194910)
Why don't they define a min/max for each primitive type? How many are there, 15?
Re:Maximal Min() and Max() by wahern on Thursday August 08, 2024 @03:33PM (#41195317)
The reason the current macro is so complex is because it supports mixed types while avoiding (failing on) integer promotion bugs. A version supporting arguments of all the same type would be just as trivial as in C++ (albeit relying on GCC extensions like statement expressions).
Re:Maximal Min() and Max() by kzrdude on Thursday August 08, 2024 @03:03PM (#41195037)
Even use a macro to define all typed min max macros
Re:Maximal Min() and Max() by tempfile on Friday August 09, 2024 @12:22PM (#41203256)
This is addressed if you chase down the original kernel mailing list thread (the "flamewar" linked in the article). It was important to Linus that the macro was actually min/max and not min_slong max_uint etc, so that people couldn't "accidentally" use the untyped version. In other words he was trying quite hard to "force" people to use these macros.

If you would then say "why doesn't min/max just implement a switch on each primitive type", I think on some level it does just do that.

Re:Maximal Min() and Max() by colonwqbang on Thursday August 08, 2024 @05:03PM (#41196243)
Genericity is not really the issue as I see it. The basic K&R macro using ?: is fully generic. The problem is that C has implicit conversion between essentially any numeric types. The main point of the kernel macro was to prevent such conversions.

I think implicit type conversion is a mistake, perhaps one of the few true design flaws in C. Languages like haskell and rust went with explicit conversions which is probably a better idea overall, even if it does increase the code verbosity a bit. C++ instead doubled down and added many more ways for implicit conversions to happen.

Re:Maximal Min() and Max() by PaulDavisThe1st on Friday August 09, 2024 @12:43PM (#41203422)
> C++ instead doubled down and added many more ways for implicit conversions to happen.

but also allows you to turn them off for objects, thankfully.