Richard G. Riley a écrit :
Gnu C has a feature for optimised branching using likely() and
unlikely() for branch conditions
e.g
if(likely(f)){
...
or
if(unlikely()){
(you pick your check function based on knowledge of likely behaviour)
Does anyone one know of any thing specific to the C language standards
which enables one to approximate these functions for the "more
general" case e.g remain standard compliant and platform independant?
Any hints or tips on optimised conditional branching would be appreciated.
(I could DEFINE them as returning f but thats not what I'm after)
This is a similar case to the subtypes problem.
Sub types are a specialization of some standard type, like
const char *p;
"const" is a subtype of "char *", the char pointer type is annotated
(i.e. specialized) with some attribute that tells the compiler
something, in that case that it can't be changed.
Your problem could be understood as the same if we do:
bool condition = (f > 23 && f < 56);
if (condition) {
// some code
}
else {
// some another code
}
You would like to say:
bool likely condition = (f > 23 && f < 56);
or
bool unlikely condition = (f > 23 && f < 56);
This is a general problem that has never been addressed in the
language but have been solved by different compilers each time in
an ad-hoc, specialized way.
We have for instance the way Microsoft does it:
__declspec(naked)
__declspec(dllimport)
__declspec(align 16)
etc
The way GNU does it is with their
__attribute__ (nonnull)
__attribute_pure
etc.
You would like to add two attributes to the "bool" type: likely and
unlikely, that would tell the compiler that a boolean value should
be preferred/not preferred in the context of a conditional expression.
Recently Microsoft proposed a standard syntax for annotating types.
I quote from
http://msdn2.microsoft.com/en-us/library/ms235402.aspx:
<<<
If you examine the library header files, you will notice some unusual
annotations such as __in_z and __out_ecount_part. These are examples of
Microsoft's standard source code annotation language (SAL), which
provides a set of annotations to describe how a function uses its
parameters—the assumptions it makes about them, and the guarantees it
makes upon finishing. The header file <sal.h> defines the annotations.
Annotations may be placed before either a function parameter's type or
its return type, and describe the function's behavior regarding the
parameter or return value. There are two classes of annotations: buffer
annotations and advanced annotations. Buffer annotations describe how
functions use their pointer parameters, and advanced annotations either
describe complex/unusual buffer behavior, or provide additional
information about a parameter that is not otherwise expressible.
where they annotate function attributes
In your case you would propose a new annotation stating the which branch
to prefer in a conditional.
Until now, no standard solution exists, but the problem is so pervasive
in C (and C++ too by the way) that a standard solution would be really a
bonus for the language.