Blender V2.61 - r43446

gim_memory.h

Go to the documentation of this file.
00001 #ifndef GIM_MEMORY_H_INCLUDED
00002 #define GIM_MEMORY_H_INCLUDED
00003 
00006 /*
00007 -----------------------------------------------------------------------------
00008 This source file is part of GIMPACT Library.
00009 
00010 For the latest info, see http://gimpact.sourceforge.net/
00011 
00012 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
00013 email: projectileman@yahoo.com
00014 
00015  This library is free software; you can redistribute it and/or
00016  modify it under the terms of EITHER:
00017    (1) The GNU Lesser General Public License as published by the Free
00018        Software Foundation; either version 2.1 of the License, or (at
00019        your option) any later version. The text of the GNU Lesser
00020        General Public License is included with this library in the
00021        file GIMPACT-LICENSE-LGPL.TXT.
00022    (2) The BSD-style license that is included with this library in
00023        the file GIMPACT-LICENSE-BSD.TXT.
00024    (3) The zlib/libpng license that is included with this library in
00025        the file GIMPACT-LICENSE-ZLIB.TXT.
00026 
00027  This library is distributed in the hope that it will be useful,
00028  but WITHOUT ANY WARRANTY; without even the implied warranty of
00029  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
00030  GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
00031 
00032 -----------------------------------------------------------------------------
00033 */
00034 
00035 
00036 #include "gim_math.h"
00037 #include <string.h>
00038 
00039 #ifdef PREFETCH
00040 #include <xmmintrin.h>  // for prefetch
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);//Allocs on the heap
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 //'long long int' is incompatible with visual studio 6...
00135     //copy words
00136     SIMD_T * ui_src_ptr = (SIMD_T *)src;
00137     SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
00138     while(copysize>=SIMD_T_SIZE)
00139     {
00140         *(ui_dst_ptr++) = *(ui_src_ptr++);
00141         copysize-=SIMD_T_SIZE;
00142     }
00143     if(copysize==0) return;
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