Vc  1.1.0
SIMD Vector Classes for C++
IO
1 /* This file is part of the Vc library. {{{
2 Copyright © 2009-2015 Matthias Kretz <kretz@kde.org>
3 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 names of contributing organizations 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 THE COPYRIGHT HOLDER 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 }}}*/
28 
29 #ifndef VC_IO_
30 #define VC_IO_
31 
32 #include "common/types.h"
33 #include "common/simdarrayfwd.h"
34 #include "common/memoryfwd.h"
35 #include <iostream>
36 
37 #if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
38 #define Vc_HACK_OSTREAM_FOR_TTY 1
39 #endif
40 
41 #ifdef Vc_HACK_OSTREAM_FOR_TTY
42 #include <unistd.h>
43 #include <ext/stdio_sync_filebuf.h>
44 #endif
45 
46 namespace Vc_VERSIONED_NAMESPACE
47 {
48 namespace
49 {
50 #ifdef Vc_HACK_OSTREAM_FOR_TTY
51 class hacked_ostream : public std::ostream
52 {
53 public:
54  using std::ostream::_M_streambuf;
55 };
56 bool mayUseColor(const std::ostream &os) __attribute__((__const__));
57 bool mayUseColor(const std::ostream &os)
58 {
59  std::basic_streambuf<char> *hack1 =
60  const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
61  __gnu_cxx::stdio_sync_filebuf<char> *hack =
62  dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
63  if (!hack) {
64  return false;
65  }
66  FILE *file = hack->file();
67  return 1 == isatty(fileno(file));
68 }
69 #else
70 bool mayUseColor(const std::ostream &) { return false; }
71 #endif
72 } // anonymous namespace
73 
74 namespace AnsiColor
75 {
76 struct Type
77 {
78  const char *data;
79 };
80 static const Type green = {"\033[1;40;32m"};
81 static const Type yellow = {"\033[1;40;33m"};
82 static const Type blue = {"\033[1;40;34m"};
83 static const Type normal = {"\033[0m"};
84 
85 inline std::ostream &operator<<(std::ostream &out, const Type &c)
86 {
87  if (mayUseColor(out)) {
88  out << c.data;
89  }
90  return out;
91 }
92 } // namespace AnsiColor
93 
117 template <typename T, typename Abi>
118 inline std::ostream &operator<<(std::ostream &out, const Vc::Vector<T, Abi> &v)
119 {
120  using TT = typename std::conditional<std::is_same<T, char>::value ||
121  std::is_same<T, unsigned char>::value ||
122  std::is_same<T, signed char>::value,
123  int,
124  T>::type;
125  out << AnsiColor::green << '[';
126  out << TT(v[0]);
127  for (size_t i = 1; i < v.Size; ++i) {
128  out << ", " << TT(v[i]);
129  }
130  out << ']' << AnsiColor::normal;
131  return out;
132 }
133 
157 template <typename T, typename Abi>
158 inline std::ostream &operator<<(std::ostream &out, const Vc::Mask<T, Abi> &m)
159 {
160  out << AnsiColor::blue << "m[";
161  for (unsigned int i = 0; i < m.Size; ++i) {
162  if (i > 0 && (i % 4) == 0) {
163  out << ' ';
164  }
165  if (m[i]) {
166  out << AnsiColor::yellow << '1';
167  } else {
168  out << AnsiColor::blue << '0';
169  }
170  }
171  out << AnsiColor::blue << ']' << AnsiColor::normal;
172  return out;
173 }
174 
175 namespace Common
176 {
177 #ifdef DOXYGEN
178 
205 template<typename V, typename Parent, typename Dimension, typename RM>
206 inline std::ostream &operator<<(std::ostream &s, const Vc::MemoryBase<V, Parent, Dimension, RM> &m);
207 #endif
208 
209 template<typename V, typename Parent, typename RM>
210 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 1, RM> &m )
211 {
212  out << AnsiColor::blue << '{' << AnsiColor::normal;
213  for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
214  out << V(m.vector(i));
215  }
216  out << AnsiColor::blue << '}' << AnsiColor::normal;
217  return out;
218 }
219 
220 template<typename V, typename Parent, typename RM>
221 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 2, RM> &m )
222 {
223  out << AnsiColor::blue << '{' << AnsiColor::normal;
224  for (size_t i = 0; i < m.rowsCount(); ++i) {
225  if (i > 0) {
226  out << "\n ";
227  }
228  const size_t vcount = m[i].vectorsCount();
229  for (size_t j = 0; j < vcount; ++j) {
230  out << V(m[i].vector(j));
231  }
232  }
233  out << AnsiColor::blue << '}' << AnsiColor::normal;
234  return out;
235 }
236 } // namespace Common
237 
238 template<typename T, std::size_t N>
239 inline std::ostream &operator<<(std::ostream &out, const SimdArray<T, N> &v)
240 {
241  out << AnsiColor::green << '<' << v[0];
242  for (size_t i = 1; i < N; ++i) {
243  if (i % 4 == 0) out << " |";
244  out << ' ' << v[i];
245  }
246  return out << '>' << AnsiColor::normal;
247 }
248 
249 template<typename T, std::size_t N>
250 inline std::ostream &operator<<(std::ostream &out, const SimdMaskArray<T, N> &m)
251 {
252  out << AnsiColor::blue << "«";
253  for (size_t i = 0; i < N; ++i) {
254  if (i > 0 && (i % 4) == 0) {
255  out << ' ';
256  }
257  if ( m[i] ) {
258  out << AnsiColor::yellow << '1';
259  } else {
260  out << AnsiColor::blue << '0';
261  }
262  }
263  return out << AnsiColor::blue << "»" << AnsiColor::normal;
264 }
265 }
266 
267 #endif // VC_IO_
268 
269 // vim: ft=cpp foldmethod=marker
Common::AdaptSubscriptOperator< std::vector< T, Allocator >> vector
An adapted std::vector container with an additional subscript operator which implements gather and sc...
Definition: vector:51
std::ostream & operator<<(std::ostream &s, const Vc::MemoryBase< V, Parent, Dimension, RM > &m)
Prints the contents of a Memory object into a stream object.