Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any real use case for using the calling convention fastcall?

Have you had any real use case for using the calling convention fastcall?

Thanks.

like image 333
rkellerm Avatar asked Jun 09 '10 08:06

rkellerm


People also ask

What is the purpose of calling convention?

Calling conventions specify how arguments are passed to a function, how return values are passed back out of a function, how the function is called, and how the function manages the stack and its stack frame. In short, the calling convention specifies how a function call in C or C++ is converted into assembly language.

What is Fastcall convention?

Microsoft Specific The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible. This calling convention only applies to the x86 architecture.

What calling convention does GCC use?

In Linux, GCC sets the de facto standard for calling conventions. Since GCC version 4.5, the stack must be aligned to a 16-byte boundary when calling a function (previous versions only required a 4-byte alignment). A version of cdecl is described in System V ABI for i386 systems.

What is the Winapi calling convention?

A calling convention is a scheme for how functions receive parameters from their caller and how they return a result. The calling conventions can differ in where parameters and return values are placed (in registers; on the call stack; a mix of both), the order they are placed.


3 Answers

__fastcall tries to pass the function arguments in the CPU registers instead of the stack if possible, which is faster.

Here's a link to an MSDN article explaining the __fastcall calling convention: http://msdn.microsoft.com/en-us/library/6xa169sk(VS.71).aspx

The first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments are passed right to left.

This means this will only work for the first two arguments and only if they're <= 32 Bits.
In general I would say, don't expect any big performance advantages from this.

like image 64
humbagumba Avatar answered Oct 29 '22 20:10

humbagumba


Here's an article explaining when to use fastcall. It actually specifies a case when you actually have no alternative but to use it:

Some VCL classes, such as TList, allow you to specify a callback function (a sort routine in the case of TList). You will have to use the __fastcall keyword in this case, too, as the VCL expects it.

like image 36
luvieere Avatar answered Oct 29 '22 22:10

luvieere


I have one case where I use it effectively - it's a very small asm routine (3 instructions) which manipulates a single value in a register.

For anything but the very smallest and most performance-critical routines though the calling convention should really make no difference.

like image 20
Paul R Avatar answered Oct 29 '22 20:10

Paul R