I have a class that looks roughly like this:
template<std::size_t dim>
class Foo {
public:
Foo(void const * const data);
private:
double vals[dim];
}
For the constructor, I know that void* data points to an array of float values (of dimension dim). Now I would like to initialize the vals-array (preferably) in the initialization list of that constructor.
To make matters worse, the floats pointed to do not necessarily have to be memory-aligned properly.
How could I do this efficiently?
Edit 1
With respect to the discussion taking place below maybe let me state my design priorities first. This might help you to focus on that problems that matter most for me.
- Cope with bad memory alignment
- Get as few operations as possible in terms of performance.
Honestly, if we need the constructors body to get a fast algorithm working, this is fine for me as well. The focus is on raw power.
If you’re afraid that passed pointer is not aligned to
floatboundary (for example, file mapping etc), then you can write something likePossibly an overzealous and not-so-portable solution:
Initializing in the initializer list won’t make your code faster, it’s just a convenience when it’s possible.
If you’re strongly concerned about performance, I would wrap this
ifin a macro and only useifon the architectures, which require properly aligned access (x86 is not one, it’s just slower on x86).Edit
Another solution proposed in the comments, thanks to
Steve Jessop. It focuses on reducing the temporary variable size.
A bit of micro-benchmarking/disassebmlying is possibly needed.