SMIL  0.9.1
DMemory.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _DMEMORY_HPP
31 #define _DMEMORY_HPP
32 
33 #ifdef __AVX__
34  #define SIMD_VEC_SIZE 32
35 #else // __AVX__
36  #define SIMD_VEC_SIZE 16
37 #endif // __AVX__
38 
39 
40 
41  #if (defined(__ICL) || defined(__ICC))
42  #include <fvec.h>
43  namespace smil
44  {
45  inline void *aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return _mm_malloc(size,align); }
46  inline void aligned_free (void *p) { return _mm_free(p); }
47  }
48  #elif defined (_MSC_VER)
49  #include <malloc.h>
50  namespace smil
51  {
52  inline void *aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return _aligned_malloc(size,align); }
53  inline void aligned_free (void *p) { return _aligned_free(p); }
54  }
55  #elif defined (__CYGWIN__)
56  #include <xmmintrin.h>
57  namespace smil
58  {
59  inline void *aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return _mm_malloc(size,align); }
60  inline void aligned_free (void *p) { return _mm_free(p); }
61  }
62  #elif defined(__MINGW64__)
63  #include <malloc.h>
64  namespace smil
65  {
66  inline void *aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return __mingw_aligned_malloc(size,align); }
67  inline void aligned_free (void *p) { return __mingw_aligned_free(p); }
68  }
69  #elif defined(__MINGW32__)
70  #include <malloc.h>
71  namespace smil
72  {
73  inline void *aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return __mingw_aligned_malloc(size,align); }
74  inline void aligned_free (void *p) { return __mingw_aligned_free(p); }
75  }
76  #elif defined(__FreeBSD__)
77  #include <stdlib.h>
78  namespace smil
79  {
80  inline void* aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return malloc(size); }
81  inline void aligned_free (void *p) { return free(p); }
82  }
83  #elif (defined(__MACOSX__) || defined(__APPLE__))
84  #include <stdlib.h>
85  namespace smil
86  {
87  inline void* aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return malloc(size); }
88  inline void aligned_free (void *p) { return free(p); }
89  }
90  #else
91  #include <malloc.h>
92  namespace smil
93  {
94  inline void* aligned_malloc (size_t size, size_t align=SIMD_VEC_SIZE) { return memalign(align,size); }
95  inline void aligned_free (void *p) { return free(p); }
96  }
97  #endif
98 
99 
100 #include <cmath>
101 #include <string>
102 #include <memory>
103 #include <limits>
104 #include <sstream>
105 #include <vector>
106 #include <cstdio>
107 #if defined(_MSC_VER)
108  #include <algorithm>
109 #endif
110 
111 namespace smil
112 {
113 
114  #define ASSUME_ALIGNED(buf) __builtin_assume_aligned(buf, SIMD_VEC_SIZE)
115 
116 
117  template<typename T>
118  inline T *createAlignedBuffer(size_t size)
119  {
120  void* ptr;
121 
122  ptr = aligned_malloc((SIMD_VEC_SIZE*(size/SIMD_VEC_SIZE+1))*sizeof(T), SIMD_VEC_SIZE);
123 
124  return ((T*) (ptr));
125  }
126 
127  template<typename T>
128  void deleteAlignedBuffer(T *ptr) {
129  aligned_free( (void*)(ptr) );
130  }
131 
132 
133 
134  template<typename T>
135  class Allocator : public std::allocator<T>
136  {
137  public :
138  // typedefs
139  typedef T value_type;
140  typedef value_type* pointer;
141  typedef const value_type* const_pointer;
142  typedef value_type& reference;
143  typedef const value_type& const_reference;
144  typedef std::size_t size_type;
145  typedef std::ptrdiff_t difference_type;
146 
147  public :
148  // convert an allocator<T> to allocator<U>
149  template<typename U>
150  struct rebind
151  {
152  typedef Allocator<U> other;
153  };
154 
155  public :
156  inline explicit Allocator() {}
157  inline ~Allocator() {}
158  inline Allocator(Allocator const&)
159  : std::allocator<T>() {}
160  template<typename U>
161  inline explicit Allocator(Allocator<U> const&) {}
162 
163  // address
164  inline pointer address(reference r) { return &r; }
165  inline const_pointer address(const_reference r) { return &r; }
166 
167  // memory allocation
168  inline pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0)
169  {
170  void* ptr;
171  ptr = aligned_malloc((SIMD_VEC_SIZE*(cnt/SIMD_VEC_SIZE+1))*sizeof(T), SIMD_VEC_SIZE);
172 
173  return reinterpret_cast<pointer>(ptr);
174  }
175  inline void deallocate(pointer p, size_type)
176  {
177  aligned_free(p);
178  }
179 
180  // size
181  inline size_type max_size() const
182  {
183  return std::numeric_limits<size_type>::max() / sizeof(T);
184  }
185 
186  // construction/destruction
187  inline void construct(pointer p, const T& t) { new(p) T(t); }
188  inline void destroy(pointer p) { p->~T(); }
189 
190  inline bool operator==(Allocator const&) { return true; }
191  inline bool operator!=(Allocator const& a) { return !operator==(a); }
192  }; // end of class Allocator
193 
194 
195  template<typename T>
196  inline void Dmemcpy(T *out, const T *in, size_t size)
197  {
198  while (size--)
199  {
200  *out++ = *in++;
201  }
202  }
203 
204 
205  template<typename T>
206  void t_LineCopyFromImage2D(T *rawImagePointerIn, const size_t lineSize, size_t y, T *lineout) {
207 
208  T *ptrin = rawImagePointerIn + y*lineSize;
209 
210  memcpy(lineout,ptrin,lineSize*sizeof(T));
211 
212  }
213 
214 
215  template<typename T>
216  void t_LineCopyToImage2D(T *linein, const size_t lineSize, size_t y, T *rawImagePointerOut) {
217 
218  T *ptrout = rawImagePointerOut + y*lineSize;
219 
220  memcpy(ptrout,linein,lineSize*sizeof(T));
221 
222  }
223 
224  template<typename T>
225  void t_LineShiftRight(const T *linein, const int lineWidth, const int nbshift, const T shiftValue, T *lineout)
226  {
227  int i;
228 
229  for(i=0 ; i<nbshift ; i++)
230  {
231  lineout[i] = shiftValue;
232  }
233 
234  memcpy(lineout+nbshift,linein,(lineWidth-nbshift)*sizeof(T));
235 
236  }
237 
238 
239  template<typename T>
240  void t_LineShiftLeft(const T *linein, const int lineWidth, const int nbshift, const T shiftValue, T *lineout)
241  {
242  int i;
243 
244  for(i=lineWidth-nbshift ; i<lineWidth ; i++)
245  {
246  lineout[i] = shiftValue;
247  }
248 
249  memcpy(lineout,linein+nbshift,(lineWidth-nbshift)*sizeof(T));
250 
251  }
252 
253  inline size_t PTR_OFFSET(void *p, size_t n=SIMD_VEC_SIZE)
254  {
255  return ((size_t)p) & (n-1);
256  }
257 
258  inline std::string displayBytes(size_t bytes)
259  {
260  std::ostringstream oss;
261  if (bytes==0)
262  {
263  oss << "0 B";
264  }
265  else
266  {
267  const char *units[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
268  const double base = 1024;
269  int c = std::min((int)(log((double)bytes)/log(base)), (int)sizeof(units) - 1);
270  oss << bytes / pow(base, c) << units[c];
271  }
272  return oss.str();
273  }
274 
275 
276 } // namespace smil
277 
278 #endif // _DMEMORY_HPP
279 
Definition: DColorConvert.h:38
Definition: DMemory.hpp:135
RES_T log(const Image< T > &imIn, Image< T > &imOut, int base=0)
Logarithm.
Definition: DImageArith.hpp:833
Definition: DMemory.hpp:150