The C book
This summer I am working at the Air Force Research Lab. Today I was trying to use RDieHarder to compute statistics about randomness of keys generated from ReRAM PUFs, a collaboration with Bertrand Cambou.
I found Robert G. Brown’s DieHarder web page which provides some documentation about the different tests and random number generators. His web site also hosts a copy of The C Book which is a great reference for learning and mastering C programming.
Because of my recent mentoring of Diego Urgell, a R project Google
Summer of Code student, I was interested to read Chapter 7
preprocessor
directives.
Like I mentioned to Diego in an issue
comment
the C preprocessor can be used to convert a token to a string, in a
process that is commonly known as “stringizing.” The chapter explains
that if a macro formal parameter is preceded by #
, for example
#define STRINGIZE(x) #x
then the # and the token list for the actual
argument are turned into a single string literal. We used this in our
GSOC project about efficient Binary Segmentation algorithms, to avoid
repeating distribution-specific subclass names:
#define DISTRIBUTION(SUBCLASS,BODY) class \
SUBCLASS: public Distribution, \
public Registration<SUBCLASS, Distribution, DistributionFactory> { \
public: \
inline static std::string factoryName = #SUBCLASS ; \
SUBCLASS(){is_registered;} \
BODY \
};
DISTRIBUTION(mean_norm,
double costFunction(int start, int end){
double lSum = this -> cumsum -> getLinearSum(start, end);
double N = end - start + 1;
return - pow(lSum, 2)/N;
}
)
Note in the code below I used #SUBCLASS
in the macro definition,
which becomes the quoted string "mean_norm"
after preprocessing. You
can run gcc -E
on this code to see the result (exercise for the
reader). In the issue comment I mentioned the “double macro expansion”
trick. We are not using double macro expansion in this code, so why
does it work? Double macro expansion is only necessary when you want
to concatenate strings, and we don’t need to do that here. Note also
that you can do token joining (not just strings!) – see the C book
for details.