[Pyrex] code specialisation/optimisation

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Apr 17 01:10:04 CEST 2003


Robin Becker <robin at reportlab.com>:

> I notice in my C code base many complex routines which attempt to do
> optimal loop unrolling etc etc. Typically they are pre-processor
> specialised according to one or more flags.
> 
> What does pyrex allow/encourage in the way of this common C practice.

In some cases you might be able to convert, e.g.

  #define SOMEFLAG 1
  ...
    #if SOMEFLAG
      ...
    #endif

into

  cdef extern from "someflags.h":
    cdef enum:
      SOMEFLAG

  ...
    if SOMEFLAG:
      ...

and rely on the C compiler optimising away anything inside
if (0) {...}.

This also depends on the flag being defined as 1 or 0, as
opposed to being defined or undefined. In the latter case
you might have to include a header file which will
#define another flag, e.g.

  #ifdef FLAG
    #define MYFLAG 1
  #else
    #define MYFLAG 0
  #endif

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list