memcpy: hide some memory latencies
[nova-simd.git] / benchmarks / cache_aligned_array.hpp
blob252d624f445b98028f0256f1bd6cf2955a040803
1 /* cache-aligned array, based on boost::array
3 * The following code declares class array,
4 * an STL container (as wrapper) for arrays of constant size.
6 * See
7 * http://www.boost.org/libs/array/
8 * for documentation.
10 * The original author site is at: http://www.josuttis.com/
12 * (C) Copyright Nicolai M. Josuttis 2001.
14 * Distributed under the Boost Software License, Version 1.0. (See
15 * accompanying file LICENSE_1_0.txt or copy at
16 * http://www.boost.org/LICENSE_1_0.txt)
18 * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
19 * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
20 * 05 Aug 2001 - minor update (Nico Josuttis)
21 * 20 Jan 2001 - STLport fix (Beman Dawes)
22 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
24 * Jan 29, 2004
26 #ifndef CACHE_ALIGNED_ARRAY_HPP
27 #define CACHE_ALIGNED_ARRAY_HPP
29 #include <cstddef>
30 #include <stdexcept>
31 #include <boost/assert.hpp>
33 // Handles broken standard libraries better than <iterator>
34 #include <boost/detail/iterator.hpp>
35 #include <boost/throw_exception.hpp>
36 #include <algorithm>
38 // FIXES for broken compilers
39 #include <boost/config.hpp>
41 #include "malloc_aligned.hpp"
43 namespace nova {
45 template<class T, std::size_t N>
46 class aligned_array {
47 public:
48 T * elems; // fixed-size array of elements of type T
50 public:
51 aligned_array(void)
53 elems = (T*)malloc_aligned(N * sizeof(T));
54 for (int i = 0; i != N; ++i)
55 new(elems+i) T();
58 aligned_array(aligned_array const & rhs)
60 elems = (T*)malloc_aligned(N * sizeof(T));
61 for (int i = 0; i != N; ++i)
62 new(elems+i) T();
63 operator=(rhs);
66 ~aligned_array(void)
68 for (int i = 0; i != N; ++i)
69 elems[i].~T();
70 free_aligned(elems);
73 // type definitions
74 typedef T value_type;
75 typedef T* iterator;
76 typedef const T* const_iterator;
77 typedef T& reference;
78 typedef const T& const_reference;
79 typedef std::size_t size_type;
80 typedef std::ptrdiff_t difference_type;
82 // iterator support
83 iterator begin() { return elems; }
84 const_iterator begin() const { return elems; }
85 iterator end() { return elems+N; }
86 const_iterator end() const { return elems+N; }
88 // reverse iterator support
89 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
93 // workaround for broken reverse_iterator in VC7
94 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
95 reference, iterator, reference> > reverse_iterator;
96 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
97 const_reference, iterator, reference> > const_reverse_iterator;
98 #else
99 // workaround for broken reverse_iterator implementations
100 typedef std::reverse_iterator<iterator,T> reverse_iterator;
101 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
102 #endif
104 reverse_iterator rbegin() { return reverse_iterator(end()); }
105 const_reverse_iterator rbegin() const {
106 return const_reverse_iterator(end());
108 reverse_iterator rend() { return reverse_iterator(begin()); }
109 const_reverse_iterator rend() const {
110 return const_reverse_iterator(begin());
113 // operator[]
114 reference operator[](size_type i)
116 BOOST_ASSERT( i < N && "out of range" );
117 return elems[i];
120 const_reference operator[](size_type i) const
122 BOOST_ASSERT( i < N && "out of range" );
123 return elems[i];
126 // at() with range check
127 reference at(size_type i) { rangecheck(i); return elems[i]; }
128 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
130 // front() and back()
131 reference front()
133 return elems[0];
136 const_reference front() const
138 return elems[0];
141 reference back()
143 return elems[N-1];
146 const_reference back() const
148 return elems[N-1];
151 // size is constant
152 static size_type size() { return N; }
153 static bool empty() { return false; }
154 static size_type max_size() { return N; }
155 enum { static_size = N };
157 // swap (note: linear complexity)
158 void swap (aligned_array<T,N>& y) {
159 std::swap_ranges(begin(),end(),y.begin());
162 // direct access to data (read-only)
163 const T* data() const { return elems; }
164 T* data() { return elems; }
166 // use array as C array (direct read/write access to data)
167 T* c_array() { return elems; }
169 // assignment with type conversion
170 aligned_array & operator=(const aligned_array & rhs)
172 std::copy(rhs.begin(),rhs.end(), begin());
173 return *this;
176 template <typename T2>
177 aligned_array<T,N>& operator= (const aligned_array<T2,N>& rhs) {
178 std::copy(rhs.begin(),rhs.end(), begin());
179 return *this;
182 // assign one value to all elements
183 void assign (const T& value)
185 std::fill_n(begin(),size(),value);
188 // check range (may be private because it is static)
189 static void rangecheck (size_type i) {
190 if (i >= size()) {
191 throw std::out_of_range("aligned_array<>: index out of range");
197 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
198 template< class T >
199 class aligned_array< T, 0 > {
201 public:
202 // type definitions
203 typedef T value_type;
204 typedef T* iterator;
205 typedef const T* const_iterator;
206 typedef T& reference;
207 typedef const T& const_reference;
208 typedef std::size_t size_type;
209 typedef std::ptrdiff_t difference_type;
211 // iterator support
212 iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
213 const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
214 iterator end() { return begin(); }
215 const_iterator end() const { return begin(); }
217 // reverse iterator support
218 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
219 typedef std::reverse_iterator<iterator> reverse_iterator;
220 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
221 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
222 // workaround for broken reverse_iterator in VC7
223 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
224 reference, iterator, reference> > reverse_iterator;
225 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
226 const_reference, iterator, reference> > const_reverse_iterator;
227 #else
228 // workaround for broken reverse_iterator implementations
229 typedef std::reverse_iterator<iterator,T> reverse_iterator;
230 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
231 #endif
233 reverse_iterator rbegin() { return reverse_iterator(end()); }
234 const_reverse_iterator rbegin() const {
235 return const_reverse_iterator(end());
237 reverse_iterator rend() { return reverse_iterator(begin()); }
238 const_reverse_iterator rend() const {
239 return const_reverse_iterator(begin());
242 // operator[]
243 reference operator[](size_type i)
245 return failed_rangecheck();
248 const_reference operator[](size_type i) const
250 return failed_rangecheck();
253 // at() with range check
254 reference at(size_type i) { return failed_rangecheck(); }
255 const_reference at(size_type i) const { return failed_rangecheck(); }
257 // front() and back()
258 reference front()
260 return failed_rangecheck();
263 const_reference front() const
265 return failed_rangecheck();
268 reference back()
270 return failed_rangecheck();
273 const_reference back() const
275 return failed_rangecheck();
278 // size is constant
279 static size_type size() { return 0; }
280 static bool empty() { return true; }
281 static size_type max_size() { return 0; }
282 enum { static_size = 0 };
284 void swap (aligned_array<T,0>& y) {
287 // direct access to data (read-only)
288 const T* data() const { return 0; }
289 T* data() { return 0; }
291 // use array as C array (direct read/write access to data)
292 T* c_array() { return 0; }
294 // assignment with type conversion
295 template <typename T2>
296 aligned_array<T,0>& operator= (const aligned_array<T2,0>& ) {
297 return *this;
300 // assign one value to all elements
301 void assign (const T& ) { }
303 // check range (may be private because it is static)
304 static reference failed_rangecheck () {
305 std::out_of_range e("attempt to access element of an empty aligned_array");
306 boost::throw_exception(e);
308 // We need to return something here to keep
309 // some compilers happy: however we will never
310 // actually get here....
312 static T placeholder;
313 return placeholder;
316 #endif
318 // comparisons
319 template<class T, std::size_t N>
320 bool operator== (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
321 return std::equal(x.begin(), x.end(), y.begin());
323 template<class T, std::size_t N>
324 bool operator< (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
325 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
327 template<class T, std::size_t N>
328 bool operator!= (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
329 return !(x==y);
331 template<class T, std::size_t N>
332 bool operator> (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
333 return y<x;
335 template<class T, std::size_t N>
336 bool operator<= (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
337 return !(y<x);
339 template<class T, std::size_t N>
340 bool operator>= (const aligned_array<T,N>& x, const aligned_array<T,N>& y) {
341 return !(x<y);
344 // global swap()
345 template<class T, std::size_t N>
346 inline void swap (aligned_array<T,N>& x, aligned_array<T,N>& y) {
347 x.swap(y);
350 template <typename sample_type, unsigned int n>
351 #ifdef GCC
352 inline sample_type __attribute__((aligned(64))) * get_samples(aligned_array<sample_type, n> & arg)
353 #else
354 inline sample_type * get_samples(aligned_array<sample_type, n> & arg)
355 #endif
357 return arg.begin();
360 template <typename sample_type, unsigned int n>
361 #ifdef GCC
362 inline const sample_type __attribute__((aligned(64))) * get_samples(aligned_array<sample_type, n> const & arg)
363 #else
364 inline const sample_type * get_samples(aligned_array<sample_type, n> const & arg)
365 #endif
367 return arg.begin();
371 } /* namespace nova */
373 #endif /*CACHE_ALIGNED_ARRAY_HPP*/