News for hackers. Stuff that matters.
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Saturday May 06, 2023 @11:57PM (#35847788)
If you're doing metaprogramming using the C preprocessor, it's time to move to a more advanced language.
Re:qsort.h – Quicksort as a C macro (2019) by taneq on Sunday May 07, 2023 @12:32AM (#35847955)
Maybe they don't want to move to a more advanced language, maybe they want to mess around with the C preprocessor. :)
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @12:43AM (#35848002)
If you want to show cleverness, by all means!

I still use some primitive tools, and know there are better alternatives, but I'm not going to defend sticking with them.

BTW, I did win the Obfuscated C contest one year, and (naturally) used the preprocessor:

https://www.ioccc.org/1986/bright/bright.c

Re:qsort.h – Quicksort as a C macro (2019) by rootw0rm on Sunday May 07, 2023 @01:51AM (#35848308)
just last night i was thinking how cool it would be for an IDE plugin that expands rust macros for easier grokking. now i need one for your #defines, lol
Re:qsort.h – Quicksort as a C macro (2019) by gdprrrr on Sunday May 07, 2023 @02:47AM (#35848585)
cargo expand?
Re:qsort.h – Quicksort as a C macro (2019) by taneq on Monday May 08, 2023 @11:21PM (#35869831)
Must... resist... being... nerdsniped... oh no too late, now I have to figure out what it's doing without looking it up. :D I've never entered the OCCC but I've read a bunch of past entries and always wanted to enter someday.
Re:qsort.h – Quicksort as a C macro (2019) by FpUser on Sunday May 07, 2023 @12:05AM (#35847819)
I knew someone would come up with this kind of "wisdom". For starters you do not know what other languages they might be using for development. And whatever they do they definitely do not need patronizing.
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @12:19AM (#35847891)
I've done a share of C metaprogramming with the preprocessor myself, and have dealt a lot with other peoples' C metaprogramming.

I've also seen people use pages of algrebra as a substitute for a couple lines of calculus.

I struggled for years with a soldering iron, always frustrated by bad solder joints. Then, I discovered a Weller thermostat controlled iron, and get a perfect joint every time.

Not everyone knows there are better ways to do things.

Re:qsort.h – Quicksort as a C macro (2019) by webstrand on Sunday May 07, 2023 @12:28AM (#35847936)
Your experience soldering is a much better response. You present a concrete suggestion for use a weller thermostat controlled iron. But you did not present a concrete suggestion for an alternative to the C pre-processor.

You could do this with C++ templates, for instance. But the LESS and SWAP operations may or may not get inlined. With the C pre-processor you can be certain that the operations get inlined, since no other alternative interpretation is available to the compiler.

Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @12:37AM (#35847973)
> But you did not present a concrete suggestion for an alternative to the C pre-processor.

If someone asked, I would. But since I do have a dog in that hunt, I felt it would be more appropriate for others to make a suggestion, as there are several.

Re:qsort.h – Quicksort as a C macro (2019) by Dalton9 on Sunday May 07, 2023 @02:31AM (#35848478)
Oh come on, Walter, don't make us beg... Show us how you'd do it in D to achieve the exact same result.
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @12:51AM (#35848040)
> But the LESS and SWAP operations may or may not get inlined.

True. But modern inliners are pretty good, and if they can't inline it due to its complexity, it's pretty unlikely it'll be faster.

Note the performance comparison in the article.

Low level hand-optimizations paid off handsomely in the 1980s, but are usually best left to the compiler's optimizer these days.

Re:qsort.h – Quicksort as a C macro (2019) by rootw0rm on Sunday May 07, 2023 @01:59AM (#35848335)
SIMD being an exception still (unless maybe Intel compiler + C/C++ + proper annotations)
Re:qsort.h – Quicksort as a C macro (2019) by alphaglosined on Sunday May 07, 2023 @04:08AM (#35849045)
LLVM can do an awful lot with SIMD that isn't visible in clang.

We have experienced this with LDC in D.

What I've been told is that LDC produces better IR than clang (I haven't compared, I only know that LDC is practically magical from both mine and other peoples experiments).

About the only thing I've seen that has problems with inlining is inline assembly. Intrinsics are fine (which you don't need thanks to vectorization being practically magical as long as you do some annotations like assert and get the memory layout right).

Re:qsort.h – Quicksort as a C macro (2019) by freedom-fries on Sunday May 07, 2023 @02:14AM (#35848407)
The article is talking code and demonstrating results.

Talk is cheap, show the code!

Re:qsort.h – Quicksort as a C macro (2019) by ivalm on Sunday May 07, 2023 @02:25AM (#35848447)
Parent comment is by the inventor of D, among other things…
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @03:44AM (#35848909)
A rote translation of Q_SORT3 would look like:

    /* Sort 3 elements. */
    void Q_SORT3(alias Q_LESS, alias Q_SWAP, Q_UINT) (Q_UINT q_a1, Q_UINT q_a2, Q_UINT q_a3)
    {
        if (Q_LESS(q_a2, q_a1)) {
            if (Q_LESS(q_a3, q_a2))
                Q_SWAP(q_a1, q_a3);
            else {
                Q_SWAP(q_a1, q_a2);
                if (Q_LESS(q_a3, q_a2))
                    Q_SWAP(q_a2, q_a3);
            }
        }
        else if (Q_LESS(q_a3, q_a2)) {
            Q_SWAP(q_a2, q_a3);
            if (Q_LESS(q_a2, q_a1))
                Q_SWAP(q_a1, q_a2);
        }
    }
A D template function is distinguished by having two parameter lists, the compile time parameter list and the runtime parameter list. Q_UINT would be a type parameter with its type inferred from the function arguments. Q_LESS and Q_SWAP are alias parameters, which can be an alias to any symbol or type. This is often used to pass lambdas.

Note that D allows the following ways to pass a parameter:

1. by value

2. by reference

3. by name

4. by type

The alias parameters are "by name". C++ has by name parameters in the form of template template parameters, along with the restriction that such a parameter can only be a template. D allows any symbol or type to be passed by name.

Passing the lambdas by name means a direct function call/inlining rather than an indirect call through a function pointer.

Anyhow, with this example you can see how the rest can be fairly easily translated.

So, you might ask, what's the advantage?

1. name hygiene

2. your debugger will see the symbols

3. no ugly \ line splicing

4. no wacky do-while(0) kludge

5. the names are actual symbols rather than transitory apparitions seen only by the preprocessor

6. color syntax highlighting in your editor works

7. the compiler will check the syntax and give error messages in terms of the symbols, not generated preprocessor text

8. It's not going to conflict with another symbol named Q_SORT

Re:qsort.h – Quicksort as a C macro (2019) by FpUser on Sunday May 07, 2023 @02:46AM (#35848584)
>"Not everyone knows there are better ways to do things."

In theory you might be right but I doubt it is applicable in this particular case.

Re:qsort.h – Quicksort as a C macro (2019) by Asooka on Sunday May 07, 2023 @08:26AM (#35850725)
I would probably use a separate templating language to manually generate variants of the function for each type I need, check those in git along with the template.
Re:qsort.h – Quicksort as a C macro (2019) by AbsoluteCabbage on Sunday May 07, 2023 @02:51AM (#35848619)
C is still the King of languages.
Re:qsort.h – Quicksort as a C macro (2019) by WhereIsTheTruth on Sunday May 07, 2023 @09:50AM (#35851498)
I agree with that

None of the new languages managed to capture what makes C great

The few "modern" ones are interesting, Zig, Jai and Odin for example, I like how they took C designated initialization to the next level

Zig's comptime is also nice

D is nice, but is loosing its charm now that the competition is here.. also what used to be a D strength now is a weakness; it's slow to compile thanks to the std.. debugging still is a pain, and code can become quite overly verbose

Re:qsort.h – Quicksort as a C macro (2019) by pjmlp on Sunday May 07, 2023 @11:59AM (#35852781)
Almost, it is the prince of memory corruption bugs, the throne belongs to Assembly.
Re:qsort.h – Quicksort as a C macro (2019) by flohofwoe on Sunday May 07, 2023 @03:36AM (#35848867)
In many cases it's also enough to just enable LTO to give the compiler enough information for stamping out a specialized version.

But TBH I'm a bit tired of people shitting on the C preprocessor. It's a relatively simple text replacement tool, and provides an incredible amount of bang for the buck (e.g. many problems can be solved without having to add more specialized bells and whistles to the language). It's a trade-off like anything else in computing.

Re:qsort.h – Quicksort as a C macro (2019) by jaridivak on Sunday May 07, 2023 @03:40AM (#35848891)
> many problems can be solved without having to invent new language features

You mean something like a type checker?

Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @03:55AM (#35848951)
The C preprocessor was an easy and compact way to extend C back when memory was really really tight. You can't even fit a C++98 compiler into DOS's memory space, but you can fit a C compiler in 64K. (Well, an earlier version of C.)

C has been adding generics anyway, like _Generic.

Re:qsort.h – Quicksort as a C macro (2019) by alphaglosined on Sunday May 07, 2023 @04:00AM (#35848983)
About a year ago I had a look at a C compiler for 16bit computers, featured in disk form on I think Adrian's digital basement YT channel.

The compiler was very basic, nothing like you'd expect from a compiler even from the dragon book.

So simple and it was a production compiler too!

Sadly I can't remember the name of it to reference here.

Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @04:08AM (#35849044)
I wrote one back in 1983 or so for 16 bit DOS, too!

A few years later, I was at a C++ conference where they asked me to sit on an "Ask us anything" panel. I was there along with the developers of Microsoft C, Borland C, etc.

The first question was "do you still ship a version of your compiler that will run on a floppy disk system?"

Vendor 1 said sure, and launched into a long description of how the files could be shuffled about on the floppy to make it work.

Vendor 2 said sure, and launched into ...

My turn. I said sure, and said the floppy disk version costs $200 and comes with a hard disk drive. (That was the price of a hard disk in those days.)

That was the end of that, I never heard that question again from anybody.

Progress...

Re:qsort.h – Quicksort as a C macro (2019) by actionfromafar on Sunday May 07, 2023 @06:45AM (#35850068)
Wow, in 1983 I hadn’t even seen a hard disk in person!
Re:qsort.h – Quicksort as a C macro (2019) by WalterBright on Sunday May 07, 2023 @01:44PM (#35853774)
I had one back then, but I did say "a few years later...".
Re:qsort.h – Quicksort as a C macro (2019) by 1980phipsi on Sunday May 14, 2023 @07:30PM (#35942309)
What versions did you offer otherwise? I don't recall CD becoming popular on computers until the 90s.
Re:qsort.h – Quicksort as a C macro (2019) by 1ris on Sunday May 07, 2023 @05:30AM (#35849566)
>C has been adding generics anyway, like _Generic.

"_Generic" added function overloads to c, not generics. The naming is exceptionally poor.

The submission is an example of generic programming. It an implementatin of something that works on all types that fullfill certain constrains. In this case comparable and swapable. "All types" include user defined types.

"_Generic" does something completely different. The C++ equvivalent would be std::conditional_v<std::is_same<T, int>, myIntFunc, std::conditional<std::is_same<T, double>, myDoubleFunc ............ The exact opposite of a generic, I'd argue, as myIntFunc and myDoubleFunc do depend on the type. You might use the preprocessor to create these functions that differ only in type, but then again, you might do what OP did.

Re:qsort.h – Quicksort as a C macro (2019) by murdoze on Sunday May 07, 2023 @05:11AM (#35849455)
Agree, but it's nowhere near TASM or even MASM macro processor ;0)
Re:qsort.h – Quicksort as a C macro (2019) by Y_Y on Sunday May 07, 2023 @10:12AM (#35851726)
You're right, but also famously an interested party in the promotion of one of these "more advanced languages".

People often take bias to mean that a source is false or untrustworthy and I think this is a nice counterexample.

Re:qsort.h – Quicksort as a C macro (2019) by tpoacher on Sunday May 07, 2023 @10:53AM (#35852142)
"A witty quote proves nothing" ~Voltaire