B
Brian Gladman
A lot of low level cryptographic code and some hardware cryptographic
accelerators either fail completely or perform very poorly if their input,
output and/or key storage areas in memory are not aligned on specific memory
boundaries. Moreover, in many situations the cryptographic code does not
itself have control over the memory alignment of its parameters so the best
it can do is to detect if these aligmments are correct and act accordingly.
This hence rasises the question of the most appropriate way of determining
the memory alignment of the memory referenced by a pointer in C. Here I am
less interested in the 'political' correctness of C code but rather in which
methods are most likely to work in practice on the highest proportion of
widely deployed processors and C compilers.
For example, when 'x' is a pointer of some kind, 'n' is a power of two and
'pint' is a pointer sized integer type, on what proportion of systems will:
#define PTR_OFFSET(x,n) (((pint)x) & ((n) - 1))
return the correct memory alignment of 'x ' from an 'n' byte boundary? Is:
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)0) & ((n) - 1))
any better (or worse)? Or, on systems that allow for the declaration of
aligned variables, is:
declare_aligned(n) type var;
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)&var) & ((n) - 1))
any better? To what extent is this likely to depend on declaring 'var' in
the same way that 'x' may have been declared (static, dynamic, auto, ...)?
And on what proportion of 'current' systems is a flat memory model used
anyway?
I would much appreciate knowing of any experiences that people here have on
the practical portability of ways to determine the physical alignment of the
memory referenced by a pointer when this pointer is outside the control of
the software in which this alignment has to be determined.
Brian Gladman
accelerators either fail completely or perform very poorly if their input,
output and/or key storage areas in memory are not aligned on specific memory
boundaries. Moreover, in many situations the cryptographic code does not
itself have control over the memory alignment of its parameters so the best
it can do is to detect if these aligmments are correct and act accordingly.
This hence rasises the question of the most appropriate way of determining
the memory alignment of the memory referenced by a pointer in C. Here I am
less interested in the 'political' correctness of C code but rather in which
methods are most likely to work in practice on the highest proportion of
widely deployed processors and C compilers.
For example, when 'x' is a pointer of some kind, 'n' is a power of two and
'pint' is a pointer sized integer type, on what proportion of systems will:
#define PTR_OFFSET(x,n) (((pint)x) & ((n) - 1))
return the correct memory alignment of 'x ' from an 'n' byte boundary? Is:
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)0) & ((n) - 1))
any better (or worse)? Or, on systems that allow for the declaration of
aligned variables, is:
declare_aligned(n) type var;
#define PTR_OFFSET(x,n) (((char*)(x) - (char*)&var) & ((n) - 1))
any better? To what extent is this likely to depend on declaring 'var' in
the same way that 'x' may have been declared (static, dynamic, auto, ...)?
And on what proportion of 'current' systems is a flat memory model used
anyway?
I would much appreciate knowing of any experiences that people here have on
the practical portability of ways to determine the physical alignment of the
memory referenced by a pointer when this pointer is outside the control of
the software in which this alignment has to be determined.
Brian Gladman