__builtin_constant_p for trivial operations

Torbjorn Granlund tg at gmplib.org
Sun Feb 20 21:59:46 CET 2011


Marc Glisse <marc.glisse at inria.fr> writes:

  On Sun, 20 Feb 2011, Torbjorn Granlund wrote:
  
  > I don't understand how this could work.
  >
  > It seems that you are suggesting to apply __builtin_constant_p to an
  > argument of an inline function.  Alas, I don't think that would ever
  > evaluate to 1, since __builtin_constant_p is expanded quite early.
  
  It is expanded late enough to work for inline functions.
  
  #include <stdio.h>
  static inline void f(int*a,int*b){
  	printf("%d\n",__builtin_constant_p(a==b));
  }
  #define g(a,b) \
  	printf("%d\n",__builtin_constant_p((a)==(b)));
  int main(){
  	int p=42;
  	f(&p,&p);
  	g(&p,&p);
  	return 0;
  }
  
  With gcc-4.4, without optimization it prints 0 and 1, but at -O1 it
  prints 1 and 1.

Nice.  My gcc knowledge is apparently severely outdated.
(I checked gcc 3.4 now, and it also expands __builtin_constant_p late.)

This makes inline functions in the GMP context quite more useful.

-- 
Torbjörn


More information about the gmp-discuss mailing list