How to force an ADD asm instruction (x64)?

Ian Lance Taylor iant@google.com
Thu Jun 16 20:43:00 GMT 2011


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.

> 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.

Ian



More information about the Gcc-help mailing list