46 #if (defined(__MMX__) || defined(__SSE__)) 52 #endif // if __GNUC__ > 3 54 #else // if defined(__GNUC__) // not gcc, assume it can use _mm_malloc since it supports MMX/SSE 58 #endif // if defined(__GNUC__) 59 #endif // if (defined(__MMX__) || defined(__SSE__)) 62 #include <mm_malloc.h> 78 #define ALIHLTARRAY_STATIC_ASSERT_CONCAT_HELPER(a, b) a##b 79 #define ALIHLTARRAY_STATIC_ASSERT_CONCAT(a, b) ALIHLTARRAY_STATIC_ASSERT_CONCAT_HELPER(a, b) 80 #define ALIHLTARRAY_STATIC_ASSERT_NC(cond, msg) \ 81 typedef PndCAArrayInternal::STATIC_ASSERT_FAILURE<cond> ALIHLTARRAY_STATIC_ASSERT_CONCAT(_STATIC_ASSERTION_FAILED_##msg, __LINE__); \ 82 ALIHLTARRAY_STATIC_ASSERT_CONCAT(_STATIC_ASSERTION_FAILED_##msg, __LINE__) Error_##msg 83 #define ALIHLTARRAY_STATIC_ASSERT(cond, msg) \ 84 ALIHLTARRAY_STATIC_ASSERT_NC(cond, msg); \ 87 template <
typename T,
int Dim>
91 template <
unsigned int Size>
102 template <
typename T>
106 MaskedSize =
sizeof(T) & (CacheLineSize - 1),
107 RequiredSize = MaskedSize == 0 ?
sizeof(T) :
sizeof(T) + CacheLineSize - MaskedSize,
108 PaddingSize = RequiredSize -
sizeof(T)
111 template <
typename T>
120 template <
typename T,
int alignment>
124 template <
typename T>
134 #ifndef ENABLE_ARRAY_BOUNDS_CHECKING 146 #define BOUNDS_CHECK(x, y) 151 class ArrayBoundsCheck {
153 virtual inline ~ArrayBoundsCheck() {}
157 inline bool IsInBounds(
int x)
const;
161 inline void SetBounds(
int start,
int end)
169 inline void MoveBounds(
int d)
175 inline void ReinterpretCast(
const ArrayBoundsCheck &other,
int sizeofOld,
int sizeofNew)
177 fStart = other.fStart * sizeofNew / sizeofOld;
178 fEnd = other.fEnd * sizeofNew / sizeofOld;
185 #define BOUNDS_CHECK(x, y) \ 186 if (PndCAInternal::ArrayBoundsCheck::IsInBounds(x)) { \ 190 template <
typename T,
int alignment>
194 static inline T *Alloc(
int s)
196 T *p =
reinterpret_cast<T *
>(_mm_malloc(s *
sizeof(T), alignment));
199 static inline void Free(T *
const p,
int size)
201 for (
int i = 0;
i < size; ++
i) {
210 posix_memalign(&p, alignment, s *
sizeof(T));
213 static inline void Free(T *
const p,
int size)
215 for (
int i = 0;
i < size; ++
i) {
222 template <
typename T>
227 static inline T2 *Alloc(
int s)
229 T2 *p =
reinterpret_cast<T2 *
>(_mm_malloc(s *
sizeof(T2), 128));
230 return new (p) T2[s];
232 static inline void Free(T2 *
const p,
int size)
234 for (
int i = 0;
i < size; ++
i) {
243 posix_memalign(&p, 128, s *
sizeof(T2));
244 return new (p) T2[s];
246 static inline void Free(T2 *
const p,
int size)
248 for (
int i = 0;
i < size; ++
i) {
255 template <
typename T>
259 static inline T *Alloc(
int s)
261 T *p =
reinterpret_cast<T *
>(_mm_malloc(s *
sizeof(T), 128));
264 static inline void Free(T *
const p,
int size)
266 for (
int i = 0;
i < size; ++
i) {
275 posix_memalign(&p, 128, s *
sizeof(T));
278 static inline void Free(T *
const p,
int size)
280 for (
int i = 0;
i < size; ++
i) {
292 template <
typename T>
296 template <
typename T>
303 template <
typename T,
int Dim>
309 template <
typename T>
318 ArrayBoundsCheck::operator=(rhs);
344 inline void SetSize(
int x,
int,
int) { fSize = x; }
351 template <
typename T>
360 ArrayBoundsCheck::operator=(rhs);
363 fStride = rhs.fStride;
373 return fData[x * fStride + y];
381 return fData[x * fStride + y];
407 template <
typename T>
410 ArrayBase() : fData(0), fSize(0), fStrideX(0), fStrideY(0) {}
414 ArrayBoundsCheck::operator=(rhs);
417 fStrideX = rhs.fStrideX;
418 fStrideY = rhs.fStrideY;
425 inline R &operator()(
int x,
int y,
int z);
429 inline const R &operator()(
int x,
int y,
int z)
const;
448 fSize = fStrideX * x;
452 template <
typename T,
unsigned int Size,
int _alignment>
457 const int offset =
reinterpret_cast<unsigned long>(&fUnalignedArray[0]) & (Alignment - 1);
458 void *mem = &fUnalignedArray[0] + (Alignment - offset);
459 return new (mem) T[Size];
463 const int offset =
reinterpret_cast<unsigned long>(&fUnalignedArray[0]) & (Alignment - 1);
464 T *mem =
reinterpret_cast<T *
>(&fUnalignedArray[0] + (Alignment - offset));
465 for (
unsigned int i = 0;
i < Size; ++
i) {
474 char fUnalignedArray[PaddedSize];
476 template <
typename T,
unsigned int Size>
489 template <
typename T,
int Dim = 1>
498 inline int Size()
const {
return Parent::fSize; }
503 inline operator bool()
const {
return Parent::fData != 0; }
507 inline bool IsValid()
const {
return Parent::fData != 0; }
515 return *Parent::fData;
523 return *Parent::fData;
530 inline T *
Data() {
return Parent::fData; }
535 inline const T *
Data()
const {
return Parent::fData; }
546 template <
typename Other>
550 r.fData =
reinterpret_cast<Other *
>(Parent::fData);
588 template <
typename T,
int Dim = 1,
int alignment = 0>
621 inline void Resize(
int x);
628 inline void Resize(
int x,
int y);
635 inline void Resize(
int x,
int y,
int z);
639 void *
operator new(size_t);
643 PndCAResizableArray &operator=(
const PndCAResizableArray &);
646 template <
unsigned int x,
unsigned int y = 0,
unsigned int z = 0>
649 enum { Size = y == 0 ? x : (z == 0 ? x * y : x * y * z), Dim = y == 0 ? 1 : (z == 0 ? 2 : 3), X = x, Y = y, Z = z };
664 template <
typename T,
typename Size,
int alignment = 0>
671 fData = fFixedArray.ConstructAlignedData();
672 Parent::SetBounds(0, Size::Size - 1);
673 SetSize(Size::X, Size::Y, Size::Z);
677 fData = fFixedArray.ConstructAlignedData();
678 Parent::SetBounds(0, Size::Size - 1);
679 SetSize(Size::X, Size::Y, Size::Z);
680 std::memcpy(fData, rhs.fData, Size::Size *
sizeof(T));
687 void *
operator new(size_t);
700 #ifdef ENABLE_ARRAY_BOUNDS_CHECKING 701 inline bool ArrayBoundsCheck::IsInBounds(
int x)
const 705 return (x >= fStart && x <= fEnd);
709 template <
typename T>
717 a.ArrayBoundsCheck::operator=(*this);
722 template <
typename T>
730 a.ArrayBoundsCheck::operator=(*this);
735 template <
typename T>
738 BOUNDS_CHECK(x * fStrideX + y + fStrideY + z, fData[0]);
739 return fData[x * fStrideX + y + fStrideY + z];
741 template <
typename T>
744 BOUNDS_CHECK(x * fStrideX + y + fStrideY + z, fData[0]);
745 return fData[x * fStrideX + y + fStrideY + z];
747 template <
typename T>
755 a.fStride = fStrideY;
756 a.ArrayBoundsCheck::operator=(*this);
760 template <
typename T>
768 a.fStride = fStrideY;
769 a.ArrayBoundsCheck::operator=(*this);
775 template <
typename T,
int Dim>
783 template <
typename T,
int Dim>
792 template <
typename T,
int Dim,
int alignment>
796 Parent::SetSize(0, 0, 0);
797 Parent::SetBounds(0, -1);
799 template <
typename T,
int Dim,
int alignment>
804 Parent::SetSize(x, 0, 0);
805 Parent::SetBounds(0, x - 1);
807 template <
typename T,
int Dim,
int alignment>
812 Parent::SetSize(x, y, 0);
813 Parent::SetBounds(0, x * y - 1);
815 template <
typename T,
int Dim,
int alignment>
820 Parent::SetSize(x, y, z);
821 Parent::SetBounds(0, x * y * z - 1);
824 template <
typename T,
int Dim,
int alignment>
830 Parent::SetSize(x, 0, 0);
831 Parent::SetBounds(0, x - 1);
833 template <
typename T,
int Dim,
int alignment>
839 Parent::SetSize(x, y, 0);
840 Parent::SetBounds(0, x * y - 1);
842 template <
typename T,
int Dim,
int alignment>
848 Parent::SetSize(x, y, z);
849 Parent::SetBounds(0, x * y * z - 1);
854 #endif // ALIHLTARRAY_H
const R & operator[](int x) const
void SetSize(int x, int y, int z)
void SetSize(int x, int y, int)
R & operator()(int x, int y)
virtual ~ArrayBoundsCheck()
static void Free(T *const p, int size)
void ReinterpretCast(const ArrayBoundsCheck &, int, int)
T * ConstructAlignedData()
RhoLorentzVectorErr operator+(const RhoLorentzVectorErr &, const RhoLorentzVectorErr &)
ArrayBase & operator=(const ArrayBase &rhs)
static void Free(T *const p, int size)
ArrayBase & operator=(const ArrayBase &rhs)
PndCAInternal::TypeForAlignmentHelper< T, alignment >::Type T2
ArrayBase(const ArrayBase &rhs)
const R & operator()(int x, int y) const
#define ALIHLTARRAY_STATIC_ASSERT(cond, msg)
const T & operator*() const
PndCAArray< Other, Dim > ReinterpretCast() const
ReturnTypeHelper< T >::Type R
PndCAInternal::ArrayBase< T2, Size::Dim > Parent
ArrayBase & operator=(const ArrayBase &rhs)
PndCAArray operator-(int x) const
PndCAInternal::ArrayBase< T, Dim > Parent
ReturnTypeHelper< T >::Type R
void SetSize(int x, int, int)
float & operator[](int i)
ArrayBase(const ArrayBase &rhs)
PndCAFixedArray(const PndCAFixedArray &rhs)
ArrayBase(const ArrayBase &rhs)
#define ALIHLTARRAY_STATIC_ASSERT_NC(cond, msg)
CacheLineSizeHelper< T > Type
CacheLineSizeHelper< T > T2
PndCAInternal::TypeForAlignmentHelper< T, alignment >::Type T2
T * ConstructAlignedData()
PndCAInternal::ArrayBase< T2, Dim > Parent
bool IsInBounds(int) const
#define BOUNDS_CHECK(x, y)
RhoLorentzVectorErr operator-(const RhoLorentzVectorErr &, const RhoLorentzVectorErr &)
PndCAArray operator+(int x) const
ReturnTypeHelper< T >::Type R
static void Free(T2 *const p, int size)