Go to the documentation of this file.00001 #ifndef GIM_MEMORY_H_INCLUDED
00002 #define GIM_MEMORY_H_INCLUDED
00003
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gim_math.h"
00037 #include <string.h>
00038
00039 #ifdef PREFETCH
00040 #include <xmmintrin.h>
00041 #define pfval 64
00042 #define pfval2 128
00043
00044 #define pf(_x,_i) _mm_prefetch((void *)(_x + _i + pfval), 0)
00045
00046 #define pf2(_x,_i) _mm_prefetch((void *)(_x + _i + pfval2), 0)
00047 #else
00048
00049 #define pf(_x,_i)
00050
00051 #define pf2(_x,_i)
00052 #endif
00053
00054
00056 #define GIM_COPY_ARRAYS(dest_array,source_array,element_count)\
00057 {\
00058 for (GUINT _i_=0;_i_<element_count ;++_i_)\
00059 {\
00060 dest_array[_i_] = source_array[_i_];\
00061 }\
00062 }\
00063
00064 #define GIM_COPY_ARRAYS_1(dest_array,source_array,element_count,copy_macro)\
00065 {\
00066 for (GUINT _i_=0;_i_<element_count ;++_i_)\
00067 {\
00068 copy_macro(dest_array[_i_],source_array[_i_]);\
00069 }\
00070 }\
00071
00072
00073 #define GIM_ZERO_ARRAY(array,element_count)\
00074 {\
00075 for (GUINT _i_=0;_i_<element_count ;++_i_)\
00076 {\
00077 array[_i_] = 0;\
00078 }\
00079 }\
00080
00081 #define GIM_CONSTANT_ARRAY(array,element_count,constant)\
00082 {\
00083 for (GUINT _i_=0;_i_<element_count ;++_i_)\
00084 {\
00085 array[_i_] = constant;\
00086 }\
00087 }\
00088
00089
00091 typedef void * gim_alloc_function (size_t size);
00092 typedef void * gim_alloca_function (size_t size);
00093 typedef void * gim_realloc_function (void *ptr, size_t oldsize, size_t newsize);
00094 typedef void gim_free_function (void *ptr);
00095
00096
00099 void gim_set_alloc_handler (gim_alloc_function *fn);
00100 void gim_set_alloca_handler (gim_alloca_function *fn);
00101 void gim_set_realloc_handler (gim_realloc_function *fn);
00102 void gim_set_free_handler (gim_free_function *fn);
00103
00104
00106 gim_alloc_function *gim_get_alloc_handler (void);
00107 gim_alloca_function *gim_get_alloca_handler(void);
00108 gim_realloc_function *gim_get_realloc_handler (void);
00109 gim_free_function *gim_get_free_handler (void);
00110
00111
00113 void * gim_alloc(size_t size);
00114 void * gim_alloca(size_t size);
00115 void * gim_realloc(void *ptr, size_t oldsize, size_t newsize);
00116 void gim_free(void *ptr);
00117
00118
00119
00120 #if defined (_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
00121 #define GIM_SIMD_MEMORY 1
00122 #endif
00123
00125 #define SIMD_T GUINT64
00126
00127 #define SIMD_T_SIZE sizeof(SIMD_T)
00128
00129
00130 inline void gim_simd_memcpy(void * dst, const void * src, size_t copysize)
00131 {
00132 #ifdef GIM_SIMD_MEMORY
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 char * c_src_ptr = (char *)src;
00147 char * c_dst_ptr = (char *)dst;
00148 while(copysize>0)
00149 {
00150 *(c_dst_ptr++) = *(c_src_ptr++);
00151 copysize--;
00152 }
00153 return;
00154 #else
00155 memcpy(dst,src,copysize);
00156 #endif
00157 }
00158
00159
00160
00161 template<class T>
00162 inline void gim_swap_elements(T* _array,size_t _i,size_t _j)
00163 {
00164 T _e_tmp_ = _array[_i];
00165 _array[_i] = _array[_j];
00166 _array[_j] = _e_tmp_;
00167 }
00168
00169
00170 template<class T>
00171 inline void gim_swap_elements_memcpy(T* _array,size_t _i,size_t _j)
00172 {
00173 char _e_tmp_[sizeof(T)];
00174 gim_simd_memcpy(_e_tmp_,&_array[_i],sizeof(T));
00175 gim_simd_memcpy(&_array[_i],&_array[_j],sizeof(T));
00176 gim_simd_memcpy(&_array[_j],_e_tmp_,sizeof(T));
00177 }
00178
00179 template <int SIZE>
00180 inline void gim_swap_elements_ptr(char * _array,size_t _i,size_t _j)
00181 {
00182 char _e_tmp_[SIZE];
00183 _i*=SIZE;
00184 _j*=SIZE;
00185 gim_simd_memcpy(_e_tmp_,_array+_i,SIZE);
00186 gim_simd_memcpy(_array+_i,_array+_j,SIZE);
00187 gim_simd_memcpy(_array+_j,_e_tmp_,SIZE);
00188 }
00189
00190 #endif // GIM_MEMORY_H_INCLUDED