| [2431] | 1 | #ifndef GIM_MEMORY_H_INCLUDED | 
|---|
 | 2 | #define GIM_MEMORY_H_INCLUDED | 
|---|
 | 3 | /*! \file gim_memory.h | 
|---|
 | 4 | \author Francisco Len Nßjera | 
|---|
 | 5 | */ | 
|---|
 | 6 | /* | 
|---|
 | 7 | ----------------------------------------------------------------------------- | 
|---|
 | 8 | This source file is part of GIMPACT Library. | 
|---|
 | 9 |  | 
|---|
 | 10 | For the latest info, see http://gimpact.sourceforge.net/ | 
|---|
 | 11 |  | 
|---|
 | 12 | Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371. | 
|---|
 | 13 | email: projectileman@yahoo.com | 
|---|
 | 14 |  | 
|---|
 | 15 |  This library is free software; you can redistribute it and/or | 
|---|
 | 16 |  modify it under the terms of EITHER: | 
|---|
 | 17 |    (1) The GNU Lesser General Public License as published by the Free | 
|---|
 | 18 |        Software Foundation; either version 2.1 of the License, or (at | 
|---|
 | 19 |        your option) any later version. The text of the GNU Lesser | 
|---|
 | 20 |        General Public License is included with this library in the | 
|---|
 | 21 |        file GIMPACT-LICENSE-LGPL.TXT. | 
|---|
 | 22 |    (2) The BSD-style license that is included with this library in | 
|---|
 | 23 |        the file GIMPACT-LICENSE-BSD.TXT. | 
|---|
 | 24 |    (3) The zlib/libpng license that is included with this library in | 
|---|
 | 25 |        the file GIMPACT-LICENSE-ZLIB.TXT. | 
|---|
 | 26 |  | 
|---|
 | 27 |  This library is distributed in the hope that it will be useful, | 
|---|
 | 28 |  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 29 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files | 
|---|
 | 30 |  GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details. | 
|---|
 | 31 |  | 
|---|
 | 32 | ----------------------------------------------------------------------------- | 
|---|
 | 33 | */ | 
|---|
 | 34 |  | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "gim_math.h" | 
|---|
 | 37 | #include <memory.h> | 
|---|
 | 38 |  | 
|---|
 | 39 | #ifdef PREFETCH | 
|---|
 | 40 | #include <xmmintrin.h>  // for prefetch | 
|---|
 | 41 | #define pfval   64 | 
|---|
 | 42 | #define pfval2  128 | 
|---|
 | 43 | //! Prefetch 64 | 
|---|
 | 44 | #define pf(_x,_i)       _mm_prefetch((void *)(_x + _i + pfval), 0) | 
|---|
 | 45 | //! Prefetch 128 | 
|---|
 | 46 | #define pf2(_x,_i)      _mm_prefetch((void *)(_x + _i + pfval2), 0) | 
|---|
 | 47 | #else | 
|---|
 | 48 | //! Prefetch 64 | 
|---|
 | 49 | #define pf(_x,_i) | 
|---|
 | 50 | //! Prefetch 128 | 
|---|
 | 51 | #define pf2(_x,_i) | 
|---|
 | 52 | #endif | 
|---|
 | 53 |  | 
|---|
 | 54 |  | 
|---|
 | 55 | ///Functions for manip packed arrays of numbers | 
|---|
 | 56 | #define GIM_COPY_ARRAYS(dest_array,source_array,element_count)\ | 
|---|
 | 57 | {\ | 
|---|
 | 58 |     for (GUINT _i_=0;_i_<element_count ;++_i_)\ | 
|---|
 | 59 |     {\ | 
|---|
 | 60 |         dest_array[_i_] = source_array[_i_];\ | 
|---|
 | 61 |     }\ | 
|---|
 | 62 | }\ | 
|---|
 | 63 |  | 
|---|
 | 64 | #define GIM_COPY_ARRAYS_1(dest_array,source_array,element_count,copy_macro)\ | 
|---|
 | 65 | {\ | 
|---|
 | 66 |     for (GUINT _i_=0;_i_<element_count ;++_i_)\ | 
|---|
 | 67 |     {\ | 
|---|
 | 68 |         copy_macro(dest_array[_i_],source_array[_i_]);\ | 
|---|
 | 69 |     }\ | 
|---|
 | 70 | }\ | 
|---|
 | 71 |  | 
|---|
 | 72 |  | 
|---|
 | 73 | #define GIM_ZERO_ARRAY(array,element_count)\ | 
|---|
 | 74 | {\ | 
|---|
 | 75 |     for (GUINT _i_=0;_i_<element_count ;++_i_)\ | 
|---|
 | 76 |     {\ | 
|---|
 | 77 |         array[_i_] = 0;\ | 
|---|
 | 78 |     }\ | 
|---|
 | 79 | }\ | 
|---|
 | 80 |  | 
|---|
 | 81 | #define GIM_CONSTANT_ARRAY(array,element_count,constant)\ | 
|---|
 | 82 | {\ | 
|---|
 | 83 |     for (GUINT _i_=0;_i_<element_count ;++_i_)\ | 
|---|
 | 84 |     {\ | 
|---|
 | 85 |         array[_i_] = constant;\ | 
|---|
 | 86 |     }\ | 
|---|
 | 87 | }\ | 
|---|
 | 88 |  | 
|---|
 | 89 |  | 
|---|
 | 90 | ///Function prototypes to allocate and free memory. | 
|---|
 | 91 | typedef void * gim_alloc_function (size_t size); | 
|---|
 | 92 | typedef void * gim_alloca_function (size_t size);//Allocs on the heap | 
|---|
 | 93 | typedef void * gim_realloc_function (void *ptr, size_t oldsize, size_t newsize); | 
|---|
 | 94 | typedef void gim_free_function (void *ptr); | 
|---|
 | 95 |  | 
|---|
 | 96 |  | 
|---|
 | 97 | ///Memory Function Handlers | 
|---|
 | 98 | ///set new memory management functions. if fn is 0, the default handlers are used. | 
|---|
 | 99 | void gim_set_alloc_handler (gim_alloc_function *fn); | 
|---|
 | 100 | void gim_set_alloca_handler (gim_alloca_function *fn); | 
|---|
 | 101 | void gim_set_realloc_handler (gim_realloc_function *fn); | 
|---|
 | 102 | void gim_set_free_handler (gim_free_function *fn); | 
|---|
 | 103 |  | 
|---|
 | 104 |  | 
|---|
 | 105 | ///get current memory management functions. | 
|---|
 | 106 | gim_alloc_function *gim_get_alloc_handler (void); | 
|---|
 | 107 | gim_alloca_function *gim_get_alloca_handler(void); | 
|---|
 | 108 | gim_realloc_function *gim_get_realloc_handler (void); | 
|---|
 | 109 | gim_free_function  *gim_get_free_handler (void); | 
|---|
 | 110 |  | 
|---|
 | 111 |  | 
|---|
 | 112 | ///Standar Memory functions | 
|---|
 | 113 | void * gim_alloc(size_t size); | 
|---|
 | 114 | void * gim_alloca(size_t size); | 
|---|
 | 115 | void * gim_realloc(void *ptr, size_t oldsize, size_t newsize); | 
|---|
 | 116 | void gim_free(void *ptr); | 
|---|
 | 117 |  | 
|---|
 | 118 |  | 
|---|
 | 119 |  | 
|---|
 | 120 | #if defined (WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) | 
|---|
 | 121 |     #define GIM_SIMD_MEMORY 1 | 
|---|
 | 122 | #endif | 
|---|
 | 123 |  | 
|---|
 | 124 | //! SIMD POINTER INTEGER | 
|---|
 | 125 | #define SIMD_T GUINT64 | 
|---|
 | 126 | //! SIMD INTEGER SIZE | 
|---|
 | 127 | #define SIMD_T_SIZE sizeof(SIMD_T) | 
|---|
 | 128 |  | 
|---|
 | 129 |  | 
|---|
 | 130 | inline void gim_simd_memcpy(void * dst, const void * src, size_t copysize) | 
|---|
 | 131 | { | 
|---|
 | 132 | #ifdef GIM_SIMD_MEMORY | 
|---|
 | 133 | /* | 
|---|
 | 134 | //'long long int' is incompatible with visual studio 6... | 
|---|
 | 135 |     //copy words | 
|---|
 | 136 |     SIMD_T * ui_src_ptr = (SIMD_T *)src; | 
|---|
 | 137 |     SIMD_T * ui_dst_ptr = (SIMD_T *)dst; | 
|---|
 | 138 |     while(copysize>=SIMD_T_SIZE) | 
|---|
 | 139 |     { | 
|---|
 | 140 |         *(ui_dst_ptr++) = *(ui_src_ptr++); | 
|---|
 | 141 |         copysize-=SIMD_T_SIZE; | 
|---|
 | 142 |     } | 
|---|
 | 143 |     if(copysize==0) return; | 
|---|
 | 144 | */ | 
|---|
 | 145 |  | 
|---|
 | 146 |     char * c_src_ptr = (char *)src; | 
|---|
 | 147 |     char * c_dst_ptr = (char *)dst; | 
|---|
 | 148 |     while(copysize>0) | 
|---|
 | 149 |     { | 
|---|
 | 150 |         *(c_dst_ptr++) = *(c_src_ptr++); | 
|---|
 | 151 |         copysize--; | 
|---|
 | 152 |     } | 
|---|
 | 153 |     return; | 
|---|
 | 154 | #else | 
|---|
 | 155 |     memcpy(dst,src,copysize); | 
|---|
 | 156 | #endif | 
|---|
 | 157 | } | 
|---|
 | 158 |  | 
|---|
 | 159 |  | 
|---|
 | 160 |  | 
|---|
 | 161 | template<class T> | 
|---|
 | 162 | inline void gim_swap_elements(T* _array,size_t _i,size_t _j) | 
|---|
 | 163 | { | 
|---|
 | 164 |         T _e_tmp_ = _array[_i]; | 
|---|
 | 165 |         _array[_i] = _array[_j]; | 
|---|
 | 166 |         _array[_j] = _e_tmp_; | 
|---|
 | 167 | } | 
|---|
 | 168 |  | 
|---|
 | 169 |  | 
|---|
 | 170 | template<class T> | 
|---|
 | 171 | inline void gim_swap_elements_memcpy(T* _array,size_t _i,size_t _j) | 
|---|
 | 172 | { | 
|---|
 | 173 |         char _e_tmp_[sizeof(T)]; | 
|---|
 | 174 |         gim_simd_memcpy(_e_tmp_,&_array[_i],sizeof(T)); | 
|---|
 | 175 |         gim_simd_memcpy(&_array[_i],&_array[_j],sizeof(T)); | 
|---|
 | 176 |         gim_simd_memcpy(&_array[_j],_e_tmp_,sizeof(T)); | 
|---|
 | 177 | } | 
|---|
 | 178 |  | 
|---|
 | 179 | template <int SIZE> | 
|---|
 | 180 | inline void gim_swap_elements_ptr(char * _array,size_t _i,size_t _j) | 
|---|
 | 181 | { | 
|---|
 | 182 |         char _e_tmp_[SIZE]; | 
|---|
 | 183 |         _i*=SIZE; | 
|---|
 | 184 |         _j*=SIZE; | 
|---|
 | 185 |         gim_simd_memcpy(_e_tmp_,_array+_i,SIZE); | 
|---|
 | 186 |         gim_simd_memcpy(_array+_i,_array+_j,SIZE); | 
|---|
 | 187 |         gim_simd_memcpy(_array+_j,_e_tmp_,SIZE); | 
|---|
 | 188 | } | 
|---|
 | 189 |  | 
|---|
 | 190 | #endif // GIM_MEMORY_H_INCLUDED | 
|---|