3.14 Variadic functions.
Listing 3.56: Optimizing GCC 4.9 ARM64
my_abs:
; sign-extend input 32-bit value to X0 64-bit register:
sxtw x0, w0
eor x1, x0, x0, asr 63
; X1=X0^(X0>>63) (shift is arithmetical)
sub x0, x1, x0, asr 63
; X0=X1-(X0>>63)=X0^(X0>>63)-(X0>>63) (all shifts are arithmetical)
ret
3.14 Variadic functions
Functionslikeprintf()andscanf()canhaveavariablenumberofarguments. Howarethesearguments
accessed?
3.14.1 Computing arithmetic mean
Let’s imagine that we want to calculatearithmetic mean, and for some weird reason we want to specify
all the values as function arguments.
But it’s impossible to get the number of arguments in a variadic function in C/C++, so let’s denote the
value of− 1 as a terminator.
Using va_arg macro
There is the standard stdarg.h header file which define macros for dealing with such arguments.
Theprintf()andscanf()functions use them as well.
#include <stdio.h>
#include <stdarg.h>
int arith_mean(int v, ...)
{
va_list args;
int sum=v, count=1, i;
va_start(args, v);
while(1)
{
i=va_arg(args, int);
if (i==-1) // terminator
break;
sum=sum+i;
count++;
}
va_end(args);
return sum/count;
};
int main()
{
printf ("%d\n", arith_mean (1, 2, 7, 10, 15, -1 / terminator /));
};
The first argument has to be treated just like a normal argument.
All other arguments are loaded using theva_argmacro and then summed.
So what is inside?