Commit 7d43cc55 authored by Michael Büsch's avatar Michael Büsch

Add Cython #+likely and #+unlikely annotations

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent f44c8e41
......@@ -26,6 +26,8 @@ In order to support both compiling the Awlsim core with Cython and running the s
* `#+cdef` : Add a `cdef` to this line. For functions or methods that means to change `def` into `cdef` in the function signature. For classes that means to change `class` to `cdef class`.
* `#+cpdef` : Same as `#+cdef`, but add `cpdef` instead.
* `#+cdef-foobar-bizz` : Same as `#+cdef`, but also add the additional words `foobar bizz` after `cdef`. Arbitrary words may be specified and the number of words is not limited. The dash `-` will be transformed into a space character.
* `#+likely` : Mark an `if` condition as being likely to evaluate to True. This annotation can only be used in lines with an `if` statement. It helps the C compiler to generate better machine code.
* `#+unlikely` : Mark an `if` condition as being unlikely to evaluate to True. This annotation can only be used in lines with an `if` statement. It helps the C compiler to generate better machine code.
To disable code coverage tracing an additional special comment is provided:
......
......@@ -10,3 +10,8 @@ ctypedef bint _Bool
# An exception is encoded as special value ExBool_val.
ctypedef signed int ExBool_t
cdef enum: ExBool_val = -1
# Make the Cython likely()/unlikely() condition macros accessible.
cdef extern from *:
_Bool likely(_Bool)
_Bool unlikely(_Bool)
#
# Cython patcher
# v1.12
# v1.13
#
# Copyright (C) 2012-2018 Michael Buesch <m@bues.ch>
#
......@@ -181,6 +181,12 @@ def pyCythonPatch(fromFile, toFile):
line = re.sub(r'\bdef\b', "cpdef", line)
# Add likely()/unlikely() to if-conditions.
for likely in ("likely", "unlikely"):
if "#+" + likely in stripLine:
line = re.sub(r'\bif\s(.*):', r'if ' + likely + r'(\1):', line)
break
# Comment all lines containing #@nocy
# or #@cyX for the not matching version.
if "#@nocy" in stripLine:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment