/* * util.h * * Created on: Mar 28, 2014 * Author: meiermark */ #ifndef UTIL_H_ #define UTIL_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include "simd.h" #include "util-inl.h" // Round x_int up to nearest multiple of fac_int #define ICEIL(x_int, fac_int) ((x_int + fac_int - 1) / fac_int) * fac_int void split(const std::string& s, char c, std::vector& v); char *substr(char* substr, char* str, int a, int b); // Allocate a memory-aligned matrix as a single block in memory (plus a vector for the pointers). // This is important for matrices for which fast access is time-critical, as rows of // the matrix will be consecutive in memory and hence access is relatively local. // Each row of the matrix, matrix[i][0], is memory-aligned with multiples of ALIGN_FLOAT. // Usage: // float** X = malloc_matrix(400,1000); // ... // free(X); template T** malloc_matrix(int dim1, int dim2) { // Compute mem sizes rounded up to nearest multiple of ALIGN_FLOAT size_t size_pointer_array = ICEIL(dim1*sizeof(T*), ALIGN_FLOAT); size_t dim2_padded = ICEIL(dim2*sizeof(T), ALIGN_FLOAT)/sizeof(T); T** matrix = (T**) mem_align( ALIGN_FLOAT, size_pointer_array + dim1*dim2_padded*sizeof(T) ); if (matrix == NULL) return matrix; T* ptr = (T*) (matrix + (size_pointer_array/sizeof(T*)) ); for (int i=0; i