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 | //#define PREFETCH 1 |
---|
40 | //! \defgroup PREFETCH |
---|
41 | //! @{ |
---|
42 | #ifdef PREFETCH |
---|
43 | #include <xmmintrin.h> // for prefetch |
---|
44 | #define pfval 64 |
---|
45 | #define pfval2 128 |
---|
46 | //! Prefetch 64 |
---|
47 | #define pf(_x,_i) _mm_prefetch((void *)(_x + _i + pfval), 0) |
---|
48 | //! Prefetch 128 |
---|
49 | #define pf2(_x,_i) _mm_prefetch((void *)(_x + _i + pfval2), 0) |
---|
50 | #else |
---|
51 | //! Prefetch 64 |
---|
52 | #define pf(_x,_i) |
---|
53 | //! Prefetch 128 |
---|
54 | #define pf2(_x,_i) |
---|
55 | #endif |
---|
56 | //! @} |
---|
57 | |
---|
58 | /*! \defgroup ARRAY_UTILITIES |
---|
59 | \brief |
---|
60 | Functions for manip packed arrays of numbers |
---|
61 | */ |
---|
62 | //! @{ |
---|
63 | #define GIM_COPY_ARRAYS(dest_array,source_array,element_count)\ |
---|
64 | {\ |
---|
65 | for (GUINT _i_=0;_i_<element_count ;++_i_)\ |
---|
66 | {\ |
---|
67 | dest_array[_i_] = source_array[_i_];\ |
---|
68 | }\ |
---|
69 | }\ |
---|
70 | |
---|
71 | #define GIM_COPY_ARRAYS_1(dest_array,source_array,element_count,copy_macro)\ |
---|
72 | {\ |
---|
73 | for (GUINT _i_=0;_i_<element_count ;++_i_)\ |
---|
74 | {\ |
---|
75 | copy_macro(dest_array[_i_],source_array[_i_]);\ |
---|
76 | }\ |
---|
77 | }\ |
---|
78 | |
---|
79 | |
---|
80 | #define GIM_ZERO_ARRAY(array,element_count)\ |
---|
81 | {\ |
---|
82 | for (GUINT _i_=0;_i_<element_count ;++_i_)\ |
---|
83 | {\ |
---|
84 | array[_i_] = 0;\ |
---|
85 | }\ |
---|
86 | }\ |
---|
87 | |
---|
88 | #define GIM_CONSTANT_ARRAY(array,element_count,constant)\ |
---|
89 | {\ |
---|
90 | for (GUINT _i_=0;_i_<element_count ;++_i_)\ |
---|
91 | {\ |
---|
92 | array[_i_] = constant;\ |
---|
93 | }\ |
---|
94 | }\ |
---|
95 | //! @} |
---|
96 | |
---|
97 | /*! \defgroup MEMORY_FUNCTION_PROTOTYPES |
---|
98 | Function prototypes to allocate and free memory. |
---|
99 | */ |
---|
100 | //! @{ |
---|
101 | typedef void * gim_alloc_function (size_t size); |
---|
102 | typedef void * gim_alloca_function (size_t size);//Allocs on the heap |
---|
103 | typedef void * gim_realloc_function (void *ptr, size_t oldsize, size_t newsize); |
---|
104 | typedef void gim_free_function (void *ptr); |
---|
105 | //! @} |
---|
106 | |
---|
107 | /*! \defgroup MEMORY_FUNCTION_HANDLERS |
---|
108 | \brief |
---|
109 | Memory Function Handlers |
---|
110 | set new memory management functions. if fn is 0, the default handlers are |
---|
111 | used. */ |
---|
112 | //! @{ |
---|
113 | void gim_set_alloc_handler (gim_alloc_function *fn); |
---|
114 | void gim_set_alloca_handler (gim_alloca_function *fn); |
---|
115 | void gim_set_realloc_handler (gim_realloc_function *fn); |
---|
116 | void gim_set_free_handler (gim_free_function *fn); |
---|
117 | //! @} |
---|
118 | |
---|
119 | /*! \defgroup MEMORY_FUNCTION_GET_HANDLERS |
---|
120 | \brief |
---|
121 | get current memory management functions. |
---|
122 | */ |
---|
123 | //! @{ |
---|
124 | gim_alloc_function *gim_get_alloc_handler (void); |
---|
125 | gim_alloca_function *gim_get_alloca_handler(void); |
---|
126 | gim_realloc_function *gim_get_realloc_handler (void); |
---|
127 | gim_free_function *gim_get_free_handler (void); |
---|
128 | //! @} |
---|
129 | |
---|
130 | /*! \defgroup MEMORY_FUNCTIONS |
---|
131 | Standar Memory functions |
---|
132 | */ |
---|
133 | //! @{ |
---|
134 | void * gim_alloc(size_t size); |
---|
135 | void * gim_alloca(size_t size); |
---|
136 | void * gim_realloc(void *ptr, size_t oldsize, size_t newsize); |
---|
137 | void gim_free(void *ptr); |
---|
138 | //! @} |
---|
139 | |
---|
140 | |
---|
141 | #if defined (WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) |
---|
142 | #define GIM_SIMD_MEMORY 1 |
---|
143 | #endif |
---|
144 | |
---|
145 | //! SIMD POINTER INTEGER |
---|
146 | #define SIMD_T GUINT64 |
---|
147 | //! SIMD INTEGER SIZE |
---|
148 | #define SIMD_T_SIZE sizeof(SIMD_T) |
---|
149 | |
---|
150 | |
---|
151 | inline void gim_simd_memcpy(void * dst, const void * src, size_t copysize) |
---|
152 | { |
---|
153 | #ifdef GIM_SIMD_MEMORY |
---|
154 | /* |
---|
155 | //'long long int' is incompatible with visual studio 6... |
---|
156 | //copy words |
---|
157 | SIMD_T * ui_src_ptr = (SIMD_T *)src; |
---|
158 | SIMD_T * ui_dst_ptr = (SIMD_T *)dst; |
---|
159 | while(copysize>=SIMD_T_SIZE) |
---|
160 | { |
---|
161 | *(ui_dst_ptr++) = *(ui_src_ptr++); |
---|
162 | copysize-=SIMD_T_SIZE; |
---|
163 | } |
---|
164 | if(copysize==0) return; |
---|
165 | */ |
---|
166 | |
---|
167 | char * c_src_ptr = (char *)src; |
---|
168 | char * c_dst_ptr = (char *)dst; |
---|
169 | while(copysize>0) |
---|
170 | { |
---|
171 | *(c_dst_ptr++) = *(c_src_ptr++); |
---|
172 | copysize--; |
---|
173 | } |
---|
174 | return; |
---|
175 | #else |
---|
176 | memcpy(dst,src,copysize); |
---|
177 | #endif |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | template<class T> |
---|
183 | inline void gim_swap_elements(T* _array,size_t _i,size_t _j) |
---|
184 | { |
---|
185 | T _e_tmp_ = _array[_i]; |
---|
186 | _array[_i] = _array[_j]; |
---|
187 | _array[_j] = _e_tmp_; |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | template<class T> |
---|
192 | inline void gim_swap_elements_memcpy(T* _array,size_t _i,size_t _j) |
---|
193 | { |
---|
194 | char _e_tmp_[sizeof(T)]; |
---|
195 | gim_simd_memcpy(_e_tmp_,&_array[_i],sizeof(T)); |
---|
196 | gim_simd_memcpy(&_array[_i],&_array[_j],sizeof(T)); |
---|
197 | gim_simd_memcpy(&_array[_j],_e_tmp_,sizeof(T)); |
---|
198 | } |
---|
199 | |
---|
200 | template <int SIZE> |
---|
201 | inline void gim_swap_elements_ptr(char * _array,size_t _i,size_t _j) |
---|
202 | { |
---|
203 | char _e_tmp_[SIZE]; |
---|
204 | _i*=SIZE; |
---|
205 | _j*=SIZE; |
---|
206 | gim_simd_memcpy(_e_tmp_,_array+_i,SIZE); |
---|
207 | gim_simd_memcpy(_array+_i,_array+_j,SIZE); |
---|
208 | gim_simd_memcpy(_array+_j,_e_tmp_,SIZE); |
---|
209 | } |
---|
210 | |
---|
211 | #endif // GIM_MEMORY_H_INCLUDED |
---|