"Modern compilers will outperform handwritten assembly."

You can talk about almost anything that you want to on this board.

Moderator: Moderators

supercat
Posts: 161
Joined: Thu Apr 18, 2019 9:13 am

Re: "Modern compilers will outperform handwritten assembly."

Post by supercat » Sun May 12, 2019 10:20 pm

rainwarrior wrote:Also if you wait long enough things that everybody implements anyway do usually eventually make it into the standard spec.
Only if it's something that the authors of the Standard hadn't expected to be supported by any compiler writers who weren't being deliberately obtuse. Unfortunately, the authors of the Standard are unwilling to make clear that the Standard was never intended to forbid all of the stupid things compilers might do that would make them unsuitable for many purposes, but instead relied upon compiler writers being capable of exercising sound judgment in such things. The authors of the Standard have said that they'd expect most compilers to process something like:

Code: Select all

unsigned mul_mod_65536(unsigned short x, unsigned short y) { return (x*y) & 0xFFFF; }
There was no need to mandate that the multiplication be processed as unsigned because implementations where it would make sense to process it that way would behave in that fashion with or without a mandate. The only situations where the authors would have expected a mandate would matter would be those where some other treatment would be more useful for some reason (e.g. on ones'-complement hardware, where making the result be accurate for values larger than INT_MAX would require an extra correction step), and in those cases the authors of the Standard saw no reason to prohibit the more useful treatment.

User avatar
Bregalad
Posts: 7768
Joined: Fri Nov 12, 2004 2:49 pm
Location: Chexbres, VD, Switzerland

Re: "Modern compilers will outperform handwritten assembly."

Post by Bregalad » Sun May 12, 2019 11:39 pm

rainwarrior wrote: Standard C maybe, but that's also why most compilers have extensions like __declspec(align), or __attribute__ ((aligned)). There's a lot of very practical ways to address domain specific problems when you actually allow domain specific solutions.

Also if you wait long enough things that everybody implements anyway do usually eventually make it into the standard spec.
This is also extremely ugly and unelegant.

User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: "Modern compilers will outperform handwritten assembly."

Post by rainwarrior » Mon May 13, 2019 2:58 am

Bregalad wrote:This is also extremely ugly and unelegant.
In practice, not really? You put the declspec/attribute in a single typedef and reuse it as much as you want with whatever name you choose. It's one line of code, probably in some header you don't need to look at much.

Code: Select all

// define once
typedef __declspec(align(8)) unsigned int auint;

// use everywhere
auint x;
auint y,z;

User avatar
koitsu
Posts: 4216
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: "Modern compilers will outperform handwritten assembly."

Post by koitsu » Mon May 13, 2019 3:38 am

__attribute__ can certainly get pretty damn ugly. Here's one you have to do squelch warnings on newer gcc and clang when presented with functions like vfprintf/vsprintf() and friends, when using -Wformat-nonliteral (which I believe is brought in via -Wall or -Wextras, I forget which):

Code: Select all

__attribute__((__format__ (__printf__, 1, 0)))
void status(const char *fmt, ...)
{
    va_list argp;

    va_start(argp, fmt);
    vfprintf(stdout, fmt, argp);
    va_end(argp);
    fflush(statusfp);
}
The 1 there is critical: it's the positional location of the formatting parameter. So, the number has to change depending on where that fmt argument happens to be, order-wise. Eventually I ceased writing/using "helper functions" of this sort and used the libc ones directly, solely to avoid this madness. (Oh, also, tools like Valgrind tend to get really pissy about all of this -- and rightfully so.)

I certainly wish there was a... I don't know how to put it -- cleaner and more "simply declared"? -- way to request alignment on data types (esp. within structs). I imagine Bregalad feels similarly. Ah well, no PL is perfect; at least we have *something*.

User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: "Modern compilers will outperform handwritten assembly."

Post by rainwarrior » Mon May 13, 2019 3:51 am

koitsu wrote:I certainly wish there was a... I don't know how to put it -- cleaner and more "simply declared"? -- way to request alignment on data types (esp. within structs).
The third link in my post above was to the C++11 alignas keyword, which is exactly that. Wish granted.

I brought up __declspec and __attribute__ to point out that compiler authors have been supplementing the standard to address needs like this since the very beginning.

Post Reply