How to force an ADD asm instruction (x64)?
Jeffrey Walton
noloader@gmail.com
Thu Jun 16 21:27:00 GMT 2011
On Thu, Jun 16, 2011 at 4:28 PM, Ian Lance Taylor <iant@google.com> wrote:
> Jeffrey Walton <noloader@gmail.com> writes:
>
>> Any ideas on what knob turning I should perform? The C/C++ source for
>> `sadd` is below. There's not much to it - just a test on an x86 flag.
>
> I don't think there is any straightforward way to do this. gcc is not
> in the business of letting you pick and choose the assembly instructions
> that it generates--that's what the assembler is for. If you really must
> have an add instruction, use an asm statement.
I was trying to stay away from coding the ADD myself - I wanted to
give the tool chain the most latitude over picking registers and
scheduling.
>
>> int sadd(int a, int b, int* r)
>> {
>> int overflow = 0;
>> int result = a + b;
>>
>> asm volatile("jo 1f");
>> asm volatile("jmp 2f");
>>
>> asm volatile("1:");
>> overflow = 1;
>>
>> asm volatile("2:");
>> if(r)
>> *r = result;
>>
>> // TRUE (1) if safe to use
>> return !overflow;
>> }
>
> Note that jumping from one asm statement to another is explicitly not
> supported and will fail in some cases. You can jump to a C label, but
> not to an asm label.
When I tried to jump to a C label, I got an undefined reference from
ld. I'll keep chipping away at it.
Thanks for the help,
Jeff
More information about the Gcc-help
mailing list