Swarm-NG  1.1
gpulog_ttraits.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Mario Juric *
3  * mjuric@cfa.harvard.EDU *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
27 #ifndef bits_gpulog_ttraits_h__
28 #define bits_gpulog_ttraits_h__
29 
30 #if GPULOG_DEBUG && !__CUDACC__
31 #include<iostream>
32 #endif
33 
34 #ifdef _WIN32
35 #define _USE_MATH_DEFINES
36 #include <math.h>
37 #define __alignof__ __alignof
38 #endif
39 
40 namespace gpulog
41 {
42 
43  namespace internal
44  {
45 
47  template<typename T> struct alignment { static const int value = __alignof__(T); };
48 
51  template<> struct alignment<float2> { static const int value = 8; };
52  template<> struct alignment<float4> { static const int value = 16; };
53  template<> struct alignment<char2> { static const int value = 2; };
54  template<> struct alignment<char4> { static const int value = 4; };
55  template<> struct alignment<uchar2> { static const int value = 2; };
56  template<> struct alignment<uchar4> { static const int value = 4; };
57  template<> struct alignment<short2> { static const int value = 4; };
58  template<> struct alignment<short4> { static const int value = 8; };
59  template<> struct alignment<ushort2> { static const int value = 4; };
60  template<> struct alignment<ushort4> { static const int value = 8; };
61  template<> struct alignment<int2> { static const int value = 8; };
62  template<> struct alignment<int4> { static const int value = 16; };
63  template<> struct alignment<uint2> { static const int value = 8; };
64  template<> struct alignment<uint4> { static const int value = 16; };
65  template<> struct alignment<long2> { static const int value = 2*alignment<long1>::value; }; // this should take care of 32 vs. 64 bit differences, assuming -malign-double is active
66  template<> struct alignment<long4> { static const int value = 4*alignment<long1>::value; };
67  template<> struct alignment<ulong2> { static const int value = 2*alignment<ulong1>::value; };
68  template<> struct alignment<ulong4> { static const int value = 4*alignment<ulong1>::value; };
69  template<> struct alignment<double2> { static const int value = 16; };
70 
71 
73  template<typename T>
74  struct ttrait
75  {
76  typedef T scalarT;
77 
78  static const size_t size = sizeof(T);
79  static const size_t align = alignment<T>::value;
80 
81  static const bool isptr = false;
82 
83  static const bool isarr = false;
84 
85  static const bool isunspec = false;
86 
87  static const size_t dim = 1;
88  };
89 
91  template<typename T>
92  struct ttrait<T*> : public ttrait<T>
93  {
94  static const bool isptr = true;
95  };
96 
98  template<typename T, int N>
99  struct ttrait<T[N]> : public ttrait<T>
100  {
101  static const size_t dim = N;
102  };
103 
105  template<typename T>
106  struct ttrait<array<T> > : public ttrait<T>
107  {
108  static const bool isarr = true;
109  };
110 
112  template<>
113  struct ttrait<Tunspec>
114  {
115  typedef Tunspec scalarT;
116  static const bool isunspec = true;
117 
118  static const size_t size = 0;
119  static const size_t align = 1;
120  static const bool isptr = false;
121  static const bool isarr = false;
122  static const size_t dim = 0;
123  };
124 
125  /* Alignment, size and type trait convenience macros */
127  #define ALIGNOF(T) (ttrait<T>::align)
128 
130  #define ESIZE(T) (ttrait<T>::size)
131 
133  #define DIMEN(T) (ttrait<T>::dim)
134 
136  #define SIZEOF(T) (ESIZE(T)*DIMEN(T))
137 
139  #define ISARRAY(T) (ttrait<T>::isarr)
140 
142  #define SCALAR(T) typename gpulog::internal::ttrait<T>::scalarT
143  #define ISUNSPEC(T) (ttrait<T>::isunspec)
144 
145  #if GPULOG_DEBUG && !__CUDACC__
146  template<typename T>
147  void dump_ttraits(const T& obj)
148  {
149  std::cerr << "align = " << ALIGNOF(T) << "\n";
150  std::cerr << "element size = " << ESIZE(T) << "\n";
151  std::cerr << "dimen = " << DIMEN(T) << "\n";
152  std::cerr << "sizeof = " << SIZEOF(T) << "\n";
153  std::cerr << "isarray = " << ISARRAY(T) << "\n";
154  std::cerr << "isunspec = " << ISUNSPEC(T) << "\n";
155  }
156  #endif
157 
158  } // namespace internal
159 } // namespace gpulog
160 
161 #endif // bits_gpulog_ttraits_h__