News for hackers. Stuff that matters.
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