On Thu, Jan 16, 2020 at 07:11:36AM +0100, Christophe Leroy wrote: > Hi Segher, > > I'm trying to see if we could enhance TCP checksum calculations by splitting > inline assembly blocks to give GCC the opportunity to mix it with other > stuff, but I'm getting difficulties with the carry. > > As far as I can read in the documentation, the z constraint represents > '‘XER[CA]’ carry bit (part of the XER register)'
Well, the documentation is very optimisitic. From the GCC source code (thanks for switching to git last week-end ;-)), it is clear that the carry is not, for the time being, properly modeled. Right now, in the machine description, all setters and users of the carry are in the same block of generated instructions. For a start, all single instructions patterns that set the carry (and do not use it) as a side effect should mention the they clobber the carry, otherwise inserting one between a setter and a user of the carry would break. This includes all arithmetic right shift (sra[wd]{,i}, subfic, addic{,\.} and I may have forgotten some. If you want to future proof your code just in case, you should also add an "xer" clobber to all instruction sequences that may modify the carry bit. But any inline assembly that touches XER might break if GCC is ugraded to properly model the carry bit, and a lot of code might need to be audited. Gabriel > > I've tried the following, but I get errors. Can you help ? > > unsigned long cksum(unsigned long a, unsigned long b, unsigned long c) > { > unsigned long sum; > unsigned long carry; > > asm("addc %0, %2, %3" : "=r"(sum), "=z"(carry) : "r"(a), "r"(b)); > asm("adde %0, %0, %2" : "+r"(sum), "+z"(carry) : "r"(c)); > asm("addze %0, %0" : "+r"(sum) : "z"(carry)); > > return sum; > } > > > > csum.c: In function 'cksum': > csum.c:6:2: error: inconsistent operand constraints in an 'asm' > asm("addc %0, %2, %3" : "=r"(sum), "=z"(carry) : "r"(a), "r"(b)); > ^ > csum.c:7:2: error: inconsistent operand constraints in an 'asm' > asm("adde %0, %0, %2" : "+r"(sum), "+z"(carry) : "r"(c)); > ^ > csum.c:8:2: error: inconsistent operand constraints in an 'asm' > asm("addze %0, %0" : "+r"(sum) : "z"(carry)); > ^ > > Thanks > Christophe >