Xref: csiph.com comp.arch:31704 Newsgroups: comp.arch Path: csiph.com!xmission!news.glorb.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!kithrup.com!mrs From: mrs@kithrup.com (Mike Stump) Subject: Re: RISC Message-ID: Date: Thu, 10 Dec 2015 02:03:31 GMT References: <58c12752-2066-4014-8f83-808e5c4dfd6d@googlegroups.com> Organization: Kithrup Enterprises, Ld. X-Newsreader: trn 4.0-test77 (Sep 1, 2010) Lines: 251 TL;DR: Here we venture far afield from the original point and most can stop reading, if you are hardcore, you might find it curious what one can do in g++. In article , Ivan Godard wrote: >> So, take saturating add for example. A language (C for example) >> can have a function define for sat_add and one can use it. This >> can be implemented as a built-in. Further, the optimizer is free >> to know about the builtin so that one cannot readily distinguish >> between the language that never has it and one that does. gcc for >> saturating add has the built in. Further, the optimizer knows >> about it. >> Further, the argument that applies to saturating add can be used for >> any instruction, any resource, any black box. >> > >That's not part of the language; that's in line assembler. See what I actually wrote. You quoted it above your comment. Please re-read it. No, that's a built-in, which is _not_ inline assembler, though, I can understand why you might think that. See my other post about rotate to make my point more clear for you. >Consider the statement: "i = j+k;". What are the language semantics? >Well, it depends on the type model of the language; "+" is not an >intrinsic or other assembler or machine notion; it is the high-level >notion of "add". Whether you get (in the resulting code) ADDS or ADDU or >ADDF or ADDD (using Mill mnemonics that I hope will be obvious; >mnemonics vary by platform) is determined by the language. C++ disagrees with you. In g++, one is free for it to mean inline assembly or a built-in or any expressible expression.[2] Indeed, I can have it mean saturating add if I want. And before you say that only applies to g++, it doesn't, it applies to every C++ language standard there are ever been and virtualy every C++ compiler there has ever been. >Now let's look a bit wider, to the declarations of i/j/k. Respectively, >those would be "signed i,j,k;", "unsigned i,j,k;", "float i,j,k;" and >"decimal i,j,k;". No saturation there. Now consider saturating i, j, k; in C++. Let me know what you think it would mean in my program to prove my point? Hint, it does saturating math.[2] >Now add (as we do) "_saturating >signed i,j,k" and "_saturating unsigned i,j,k;" to the possible >declarations. Now the compiler emits ADDSS and ADDSU respectively for >the source "i = j+k". That's part of the *language*. Unless it's not. saturating is free to be user defined in C++.[2] >But, you seem to ask, isn't it the same to just use inline assembler? >No, it's not, because the compiler does not know the semantics of that >"function". Wrong, The compiler is free to know the semantics of built-ins, and does. Alloca is one. Further, the code need not even be a built-in for the case of saturating add.[2] That can be expressed as easily as rotate can be expressed. Indeed, there isn't a builtin for rotate because the open code always works. And the compiler certainly can know the semantics of open code as well as it can know the semantics of a built-in. Further, an adventuresome compiler _could_ know the samantics of inline assembly, if it wanted. >If the arguments turn out to be manifest constants the >compiler will still emit the instruction, rather than doing constant >folding. In gcc, for constant folding, we call: /* Perform the binary or unary operation described by CODE. Note that OP0 and OP1 must have the same mode for binary operators. For a unary operation, leave OP1 NULL. Return true, if !SAT_P and overflow. */ bool fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0, const FIXED_VALUE_TYPE *op1, bool sat_p) { and when doing saturating math, sat_p is set, and when it is set, we do the math and then saturate and it all just disappears into the new constant. No emitted instruction at all, as one would expect from a quality compiler. Any compiler that does less, well, needs fixing. In the type system, we have: /* In a non-aggregate type, indicates a saturating type. */ #define TYPE_SATURATING(NODE) When + is performed on a saturating type, sat_p is set, the type determines the semantics. The semantics of saturing adds in gcc are the semantics that you expect. Now, even if we didn't have first class support for type in the optimizer of gcc, we can easily get this in c++.[2] Further, the optimizer has little trouble reducing this to a constant.[2] See tryconst, in x86_64: _Z8tryconstv: .LFB13: .cfi_startproc movl $3, %eax ret looks awefully like a constant to me. >And you cant use it in any other place where the language requires a >compile-time expressions, so no chance with "int A[add_s(j, k)]" In C++, you can run user code to produce constants that must be known at compile time.[1] In c++, one is free to make use of this to be used in places that require constants. Check out the generated size of array in[2]: const int i = 1; const int j = 2; int array[const_sadd(i,j)]; .type array, @object .size array, 12 array: .zero 12 looks pretty constant to me. Doesn't have the nice saturating in that version, but even that might be able to be fixed in c++17. >either - although "int A[j+k]" would be fine were j amnd k known to >be satuating. In g++, it would be folded to a constant, and the bytes allocated for A would be fixed. Indeed, in g++, one can do: #include int foo() { return 4; } main() { int A[foo()]; printf ("A is %ld bytes\n", sizeof (A)); } and currently the code gen will generate: movl $16, %edx to load up the argument for the printf. A ever so slightly better optimizer would merely generate: .LC0: .string "A is 16 bytes" movl $.LC0, %edi .LEHB0: call puts We already do that optimization today for printf calls with just 1 argument. This is true of any quality C++ compiler, most are. 1 - http://en.cppreference.com/w/cpp/language/constexpr 2 - #if 1 template T sadd(T a, T b) { T sum = a+b; T overflow = ((a^sum)&(b^sum)) >> (sizeof (T) * 8 - 1); return (overflow<<(sizeof(T) * 8 - 1)) ^ (sum>>overflow); } template constexpr T const_sadd(T a, T b) { #define sum (a+b) #define overflow (((a^sum)&(b^sum)) >> (sizeof (T) * 8 - 1)) return (overflow<<(sizeof(T) * 8 - 1)) ^ (sum>>overflow); #undef sum #undef overflow } const int i = 1; const int j = 2; int array[const_sadd(i,j)]; #endif template class saturating { public: T d; saturating() { } saturating(T v) : d(v) { } operator T () { return d; } saturating operator + (const saturating b) const { T r; #if 0 asm ("# adds\t%0,%2" : "=r" (r) : "0" (this->d), "r" (b.d)); #else r = sadd (this->d, b.d); #endif return r; } saturating operator - (const saturating b) const { T r; asm ("# subs\t%0,%2" : "=r" (r) : "0" (this->d), "r" (b.d)); return r; } }; long foo(long a, long b) { saturating sa = a; saturating sb = b; return sa + sb; } saturating bar(saturating a, saturating b) { return a+b; } class Rgb { public: saturating r, g, b; Rgb operator + (const Rgb b) const { Rgb r; r.r = this->r + b.r; r.g = this->g + b.g; r.b = this->b + b.b; return r; } Rgb operator - (const Rgb b) const { return (Rgb) { this->r - b.r, this->g - b.g, this->b - b.b }; } }; Rgb testradd(Rgb a, Rgb b) { return a + b; } long tryconst () { return saturating (1) + saturating (2); }