NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector_loads.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. 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  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <nvbio/basic/types.h>
35 #include <nvbio/basic/numbers.h>
36 
37 namespace nvbio {
38 
39 template <typename T> struct maximum_vector_width { static const uint32 VALUE = 1; };
40 template <> struct maximum_vector_width<char> { static const uint32 VALUE = 16; };
41 template <> struct maximum_vector_width< int8> { static const uint32 VALUE = 16; };
42 template <> struct maximum_vector_width<uint8> { static const uint32 VALUE = 16; };
43 template <> struct maximum_vector_width< int16> { static const uint32 VALUE = 4; };
44 template <> struct maximum_vector_width<uint16> { static const uint32 VALUE = 4; };
45 template <> struct maximum_vector_width< int32> { static const uint32 VALUE = 4; };
46 template <> struct maximum_vector_width<uint32> { static const uint32 VALUE = 4; };
47 template <> struct maximum_vector_width< int64> { static const uint32 VALUE = 2; };
48 template <> struct maximum_vector_width<uint64> { static const uint32 VALUE = 2; };
49 
50 template <uint32 VECTOR_WIDTH, typename T>
51 struct vector_loader {};
52 
53 template <typename T>
54 struct vector_loader<1,T> { NVBIO_HOST_DEVICE static void load(const T* ptr, T* vec) { *vec = *ptr; } };
55 
56 template <> struct vector_loader< 1,char> { NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec) { *vec = *ptr; } };
57 template <> struct vector_loader< 2,char> { NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec) { const char2 v = *(const char2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
58 template <> struct vector_loader< 3,char> { NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec) { const char3 v = *(const char3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
59 template <> struct vector_loader< 4,char> { NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec) { const char4 v = *(const char4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
60 template <> struct vector_loader< 8,char>
61 {
62  NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec)
63  {
64  const uint2 v = *(const uint2*)ptr;
65  vec[0] = (char)((v.x >> 0) & 0xFF); vec[1] = (char)((v.x >> 8) & 0xFF);
66  vec[2] = (char)((v.x >> 16) & 0xFF); vec[3] = (char)((v.x >> 24) & 0xFF);
67  vec[4] = (char)((v.y >> 0) & 0xFF); vec[5] = (char)((v.y >> 8) & 0xFF);
68  vec[6] = (char)((v.y >> 16) & 0xFF); vec[7] = (char)((v.y >> 24) & 0xFF);
69  }
70 };
71 template <> struct vector_loader<16,char>
72 {
73  NVBIO_HOST_DEVICE static void load(const char* ptr, char* vec)
74  {
75  const uint4 v = *(const uint4*)ptr;
76 
77  vec[0] = (char)((v.x >> 0) & 0xFF); vec[1] = (char)((v.x >> 8) & 0xFF);
78  vec[2] = (char)((v.x >> 16) & 0xFF); vec[3] = (char)((v.x >> 24) & 0xFF);
79  vec[4] = (char)((v.y >> 0) & 0xFF); vec[5] = (char)((v.y >> 8) & 0xFF);
80  vec[6] = (char)((v.y >> 16) & 0xFF); vec[7] = (char)((v.y >> 24) & 0xFF);
81 
82  vec[8] = (char)((v.z >> 0) & 0xFF); vec[9] = (char)((v.z >> 8) & 0xFF);
83  vec[10] = (char)((v.z >> 16) & 0xFF); vec[11] = (char)((v.z >> 24) & 0xFF);
84  vec[12] = (char)((v.w >> 0) & 0xFF); vec[13] = (char)((v.w >> 8) & 0xFF);
85  vec[14] = (char)((v.w >> 16) & 0xFF); vec[15] = (char)((v.w >> 24) & 0xFF);
86  }
87 };
88 
89 template <> struct vector_loader< 1,int8> { NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec) { *vec = *ptr; } };
90 template <> struct vector_loader< 2,int8> { NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec) { const uchar2 v = *(const uchar2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
91 template <> struct vector_loader< 3,int8> { NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec) { const uchar3 v = *(const uchar3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
92 template <> struct vector_loader< 4,int8> { NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec) { const uchar4 v = *(const uchar4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
93 template <> struct vector_loader< 8,int8>
94 {
95  NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec)
96  {
97  const uint2 v = *(const uint2*)ptr;
98  vec[0] = (int8)((v.x >> 0) & 0xFF); vec[1] = (int8)((v.x >> 8) & 0xFF);
99  vec[2] = (int8)((v.x >> 16) & 0xFF); vec[3] = (int8)((v.x >> 24) & 0xFF);
100  vec[4] = (int8)((v.y >> 0) & 0xFF); vec[5] = (int8)((v.y >> 8) & 0xFF);
101  vec[6] = (int8)((v.y >> 16) & 0xFF); vec[7] = (int8)((v.y >> 24) & 0xFF);
102  }
103 };
104 template <> struct vector_loader<16,int8>
105 {
106  NVBIO_HOST_DEVICE static void load(const int8* ptr, int8* vec)
107  {
108  const uint4 v = *(const uint4*)ptr;
109 
110  vec[0] = (int8)((v.x >> 0) & 0xFF); vec[1] = (int8)((v.x >> 8) & 0xFF);
111  vec[2] = (int8)((v.x >> 16) & 0xFF); vec[3] = (int8)((v.x >> 24) & 0xFF);
112  vec[4] = (int8)((v.y >> 0) & 0xFF); vec[5] = (int8)((v.y >> 8) & 0xFF);
113  vec[6] = (int8)((v.y >> 16) & 0xFF); vec[7] = (int8)((v.y >> 24) & 0xFF);
114 
115  vec[8] = (int8)((v.z >> 0) & 0xFF); vec[9] = (int8)((v.z >> 8) & 0xFF);
116  vec[10] = (int8)((v.z >> 16) & 0xFF); vec[11] = (int8)((v.z >> 24) & 0xFF);
117  vec[12] = (int8)((v.w >> 0) & 0xFF); vec[13] = (int8)((v.w >> 8) & 0xFF);
118  vec[14] = (int8)((v.w >> 16) & 0xFF); vec[15] = (int8)((v.w >> 24) & 0xFF);
119  }
120 };
121 
122 template <> struct vector_loader< 1,uint8> { NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec) { *vec = *ptr; } };
123 template <> struct vector_loader< 2,uint8> { NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec) { const uchar2 v = *(const uchar2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
124 template <> struct vector_loader< 3,uint8> { NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec) { const uchar3 v = *(const uchar3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
125 template <> struct vector_loader< 4,uint8> { NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec) { const uchar4 v = *(const uchar4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
126 template <> struct vector_loader< 8,uint8>
127 {
128  NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec)
129  {
130  const uint2 v = *(const uint2*)ptr;
131  vec[0] = (uint8)((v.x >> 0) & 0xFF); vec[1] = (uint8)((v.x >> 8) & 0xFF);
132  vec[2] = (uint8)((v.x >> 16) & 0xFF); vec[3] = (uint8)((v.x >> 24) & 0xFF);
133  vec[4] = (uint8)((v.y >> 0) & 0xFF); vec[5] = (uint8)((v.y >> 8) & 0xFF);
134  vec[6] = (uint8)((v.y >> 16) & 0xFF); vec[7] = (uint8)((v.y >> 24) & 0xFF);
135  }
136 };
137 template <> struct vector_loader<16,uint8>
138 {
139  NVBIO_HOST_DEVICE static void load(const uint8* ptr, uint8* vec)
140  {
141  const uint4 v = *(const uint4*)ptr;
142 
143  vec[0] = (uint8)((v.x >> 0) & 0xFF); vec[1] = (uint8)((v.x >> 8) & 0xFF);
144  vec[2] = (uint8)((v.x >> 16) & 0xFF); vec[3] = (uint8)((v.x >> 24) & 0xFF);
145  vec[4] = (uint8)((v.y >> 0) & 0xFF); vec[5] = (uint8)((v.y >> 8) & 0xFF);
146  vec[6] = (uint8)((v.y >> 16) & 0xFF); vec[7] = (uint8)((v.y >> 24) & 0xFF);
147 
148  vec[8] = (uint8)((v.z >> 0) & 0xFF); vec[9] = (uint8)((v.z >> 8) & 0xFF);
149  vec[10] = (uint8)((v.z >> 16) & 0xFF); vec[11] = (uint8)((v.z >> 24) & 0xFF);
150  vec[12] = (uint8)((v.w >> 0) & 0xFF); vec[13] = (uint8)((v.w >> 8) & 0xFF);
151  vec[14] = (uint8)((v.w >> 16) & 0xFF); vec[15] = (uint8)((v.w >> 24) & 0xFF);
152  }
153 };
154 
155 template <> struct vector_loader< 1,int16> { NVBIO_HOST_DEVICE static void load(const int16* ptr, int16* vec) { *vec = *ptr; } };
156 template <> struct vector_loader< 2,int16> { NVBIO_HOST_DEVICE static void load(const int16* ptr, int16* vec) { const int16_2 v = *(const int16_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
157 template <> struct vector_loader< 3,int16> { NVBIO_HOST_DEVICE static void load(const int16* ptr, int16* vec) { const int16_3 v = *(const int16_3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
158 template <> struct vector_loader< 4,int16> { NVBIO_HOST_DEVICE static void load(const int16* ptr, int16* vec) { const int16_4 v = *(const int16_4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
159 
160 template <> struct vector_loader< 1,uint16> { NVBIO_HOST_DEVICE static void load(const uint16* ptr, uint16* vec) { *vec = *ptr; } };
161 template <> struct vector_loader< 2,uint16> { NVBIO_HOST_DEVICE static void load(const uint16* ptr, uint16* vec) { const uint16_2 v = *(const uint16_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
162 template <> struct vector_loader< 3,uint16> { NVBIO_HOST_DEVICE static void load(const uint16* ptr, uint16* vec) { const uint16_3 v = *(const uint16_3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
163 template <> struct vector_loader< 4,uint16> { NVBIO_HOST_DEVICE static void load(const uint16* ptr, uint16* vec) { const uint16_4 v = *(const uint16_4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
164 
165 template <> struct vector_loader< 1,int32> { NVBIO_HOST_DEVICE static void load(const int32* ptr, int32* vec) { *vec = *ptr; } };
166 template <> struct vector_loader< 2,int32> { NVBIO_HOST_DEVICE static void load(const int32* ptr, int32* vec) { const int32_2 v = *(const int32_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
167 template <> struct vector_loader< 3,int32> { NVBIO_HOST_DEVICE static void load(const int32* ptr, int32* vec) { const int32_3 v = *(const int32_3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
168 template <> struct vector_loader< 4,int32> { NVBIO_HOST_DEVICE static void load(const int32* ptr, int32* vec) { const int32_4 v = *(const int32_4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
169 
170 template <> struct vector_loader< 1,uint32> { NVBIO_HOST_DEVICE static void load(const uint32* ptr, uint32* vec) { *vec = *ptr; } };
171 template <> struct vector_loader< 2,uint32> { NVBIO_HOST_DEVICE static void load(const uint32* ptr, uint32* vec) { const uint32_2 v = *(const uint32_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
172 template <> struct vector_loader< 3,uint32> { NVBIO_HOST_DEVICE static void load(const uint32* ptr, uint32* vec) { const uint32_3 v = *(const uint32_3*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; } };
173 template <> struct vector_loader< 4,uint32> { NVBIO_HOST_DEVICE static void load(const uint32* ptr, uint32* vec) { const uint32_4 v = *(const uint32_4*)ptr; vec[0] = v.x; vec[1] = v.y; vec[2] = v.z; vec[3] = v.w; } };
174 
175 template <> struct vector_loader< 1,int64> { NVBIO_HOST_DEVICE static void load(const int64* ptr, int64* vec) { *vec = *ptr; } };
176 template <> struct vector_loader< 2,int64> { NVBIO_HOST_DEVICE static void load(const int64* ptr, int64* vec) { const int64_2 v = *(const int64_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
177 
178 template <> struct vector_loader< 1,uint64> { NVBIO_HOST_DEVICE static void load(const uint64* ptr, uint64* vec) { *vec = *ptr; } };
179 template <> struct vector_loader< 2,uint64> { NVBIO_HOST_DEVICE static void load(const uint64* ptr, uint64* vec) { const uint64_2 v = *(const uint64_2*)ptr; vec[0] = v.x; vec[1] = v.y; } };
180 
183 template <uint32 VECTOR_WIDTH, typename T>
185 void vector_load(const T* ptr, T* vec)
186 {
188 }
189 
190 } // namespace nvbio