]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/tracking-ca/MemoryAssignmentHelpers.h
When Pt is bad defined (ex. no field), the multiple scattering effect is calculated...
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / MemoryAssignmentHelpers.h
CommitLineData
6de2bc40 1// **************************************************************************
2// * This file is property of and copyright by the ALICE HLT Project *
3// * All rights reserved. *
4// * *
5// * Primary Authors: *
6// * Copyright 2009 Matthias Kretz <kretz@kde.org> *
7// * *
8// * Permission to use, copy, modify and distribute this software and its *
9// * documentation strictly for non-commercial purposes is hereby granted *
10// * without fee, provided that the above copyright notice appears in all *
11// * copies and that both the copyright notice and this permission notice *
12// * appear in the supporting documentation. The authors make no claims *
13// * about the suitability of this software for any purpose. It is *
14// * provided "as is" without express or implied warranty. *
15// **************************************************************************
16
4acc2401 17#ifndef MEMORYASSIGNMENTHELPERS_H
18#define MEMORYASSIGNMENTHELPERS_H
19
7be9b0d7 20#ifndef assert
4acc2401 21#include <assert.h>
7be9b0d7 22#endif
4acc2401 23
24template<unsigned int X>
7be9b0d7 25GPUhd() static inline void AlignTo( char *&mem )
4acc2401 26{
27 STATIC_ASSERT( ( X & ( X - 1 ) ) == 0, X_needs_to_be_a_multiple_of_2 );
7be9b0d7 28 const int offset = reinterpret_cast<unsigned long long>( mem ) & ( X - 1 );
4acc2401 29 if ( offset > 0 ) {
30 mem += ( X - offset );
31 }
32 //assert( ( reinterpret_cast<unsigned long>( mem ) & ( X - 1 ) ) == 0 );
33}
34
35template<unsigned int X>
b22af1bf 36GPUhd() static inline unsigned int NextMultipleOf( unsigned int value )
4acc2401 37{
38 STATIC_ASSERT( ( X & ( X - 1 ) ) == 0, X_needs_to_be_a_multiple_of_2 );
39 const int offset = value & ( X - 1 );
40 if ( offset > 0 ) {
41 return value + X - offset;
42 }
43 return value;
44}
45
46template<typename T, unsigned int Alignment> static T *AssignMemory( char *&mem, unsigned int size )
47{
48 STATIC_ASSERT( ( Alignment & ( Alignment - 1 ) ) == 0, Alignment_needs_to_be_a_multiple_of_2 );
49 AlignTo<Alignment> ( mem );
50 T *r = reinterpret_cast<T *>( mem );
51 mem += size * sizeof( T );
52 return r;
53}
54
55template<typename T, unsigned int Alignment> static inline T *AssignMemory( char *&mem, unsigned int stride, unsigned int count )
56{
57 assert( 0 == ( stride & ( Alignment - 1 ) ) );
58 return AssignMemory<T, Alignment>( mem, stride * count );
59}
60
7be9b0d7 61template<typename T, unsigned int Alignment> GPUhd() static T *_assignMemory( char *&mem, unsigned int size )
4acc2401 62{
63 STATIC_ASSERT( ( Alignment & ( Alignment - 1 ) ) == 0, Alignment_needs_to_be_a_multiple_of_2 );
64 AlignTo<Alignment>( mem );
65 T *r = reinterpret_cast<T *>( mem );
66 mem += size * sizeof( T );
67 return r;
68}
69
7be9b0d7 70template<typename T> GPUhd() static inline void AssignMemory( T *&dst, char *&mem, int count )
4acc2401 71{
72 dst = _assignMemory < T, ( sizeof( T ) & ( sizeof( T ) - 1 ) ) == 0 && sizeof( T ) <= 16 ? sizeof( T ) : sizeof( void * ) > ( mem, count );
73}
74
75#endif // MEMORYASSIGNMENTHELPERS_H