1/* This file is part of the Vc library. {{{
2Copyright © 2009-2015 Matthias Kretz <kretz@kde.org>
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the names of contributing organizations nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#include "common/types.h"
32#include "common/simdarrayfwd.h"
33#include "common/memoryfwd.h"
36#if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
37#define Vc_HACK_OSTREAM_FOR_TTY 1
40#ifdef Vc_HACK_OSTREAM_FOR_TTY
42#include <ext/stdio_sync_filebuf.h>
45namespace Vc_VERSIONED_NAMESPACE
49#ifdef Vc_HACK_OSTREAM_FOR_TTY
50class hacked_ostream : public std::ostream
53 using std::ostream::_M_streambuf;
55bool mayUseColor(const std::ostream &os) __attribute__((__const__));
56bool mayUseColor(const std::ostream &os)
58 std::basic_streambuf<char> *hack1 =
59 const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
60 __gnu_cxx::stdio_sync_filebuf<char> *hack =
61 dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
65 FILE *file = hack->file();
66 return 1 == isatty(fileno(file));
69bool mayUseColor(const std::ostream &) { return false; }
71} // anonymous namespace
79static const Type green = {"\033[1;40;32m"};
80static const Type yellow = {"\033[1;40;33m"};
81static const Type blue = {"\033[1;40;34m"};
82static const Type normal = {"\033[0m"};
84inline std::ostream &operator<<(std::ostream &out, const Type &c)
86 if (mayUseColor(out)) {
91} // namespace AnsiColor
95 * \headerfile IO <Vc/IO>
97 * Prints the contents of a vector into a stream object.
100 * const Vc::int_v v(Vc::IndexesFromZero);
101 * std::cout << v << std::endl;
103 * will output (with SSE):
108 * \param out Any standard C++ ostream object. For example std::cout or a
109 * std::stringstream object.
110 * \param v Any Vc::Vector object.
111 * \return The ostream object: to chain multiple stream operations.
113 * \note With the GNU standard library this function will check whether the
114 * output stream is a tty in which case it colorizes the output.
116template <typename T, typename Abi>
117inline std::ostream &operator<<(std::ostream &out, const Vc::Vector<T, Abi> &v)
119 using TT = typename std::conditional<std::is_same<T, char>::value ||
120 std::is_same<T, unsigned char>::value ||
121 std::is_same<T, signed char>::value,
124 out << AnsiColor::green << '[';
126 for (size_t i = 1; i < v.Size; ++i) {
127 out << ", " << TT(v[i]);
129 out << ']' << AnsiColor::normal;
135 * \headerfile IO <Vc/IO>
137 * Prints the contents of a mask into a stream object.
140 * const Vc::short_m m = Vc::short_v::IndexesFromZero() < 3;
141 * std::cout << m << std::endl;
143 * will output (with SSE):
148 * \param out Any standard C++ ostream object. For example std::cout or a
149 * std::stringstream object.
150 * \param m Any Vc::Mask object.
151 * \return The ostream object: to chain multiple stream operations.
153 * \note With the GNU standard library this function will check whether the
154 * output stream is a tty in which case it colorizes the output.
156template <typename T, typename Abi>
157inline std::ostream &operator<<(std::ostream &out, const Vc::Mask<T, Abi> &m)
159 out << AnsiColor::blue << "m[";
160 for (unsigned int i = 0; i < m.Size; ++i) {
161 if (i > 0 && (i % 4) == 0) {
165 out << AnsiColor::yellow << '1';
167 out << AnsiColor::blue << '0';
170 out << AnsiColor::blue << ']' << AnsiColor::normal;
179 * \headerfile dox.h <Vc/IO>
181 * Prints the contents of a Memory object into a stream object.
184 * Vc::Memory<int_v, 10> m;
185 * for (int i = 0; i < m.entriesCount(); ++i) {
188 * std::cout << m << std::endl;
190 * will output (with SSE):
192{[0, 1, 2, 3] [4, 5, 6, 7] [8, 9, 0, 0]}
195 * \param s Any standard C++ ostream object. For example std::cout or a std::stringstream object.
196 * \param m Any Vc::Memory object.
197 * \return The ostream object: to chain multiple stream operations.
199 * \note With the GNU standard library this function will check whether the
200 * output stream is a tty in which case it colorizes the output.
202 * \warning Please do not forget that printing a large memory object can take a long time.
204template<typename V, typename Parent, typename Dimension, typename RM>
205inline std::ostream &operator<<(std::ostream &s, const Vc::MemoryBase<V, Parent, Dimension, RM> &m);
208template<typename V, typename Parent, typename RM>
209inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 1, RM> &m )
211 out << AnsiColor::blue << '{' << AnsiColor::normal;
212 for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
213 out << V(m.vector(i));
215 out << AnsiColor::blue << '}' << AnsiColor::normal;
219template<typename V, typename Parent, typename RM>
220inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 2, RM> &m )
222 out << AnsiColor::blue << '{' << AnsiColor::normal;
223 for (size_t i = 0; i < m.rowsCount(); ++i) {
227 const size_t vcount = m[i].vectorsCount();
228 for (size_t j = 0; j < vcount; ++j) {
229 out << V(m[i].vector(j));
232 out << AnsiColor::blue << '}' << AnsiColor::normal;
237template<typename T, std::size_t N>
238inline std::ostream &operator<<(std::ostream &out, const SimdArray<T, N> &v)
240 out << AnsiColor::green << '<' << v[0];
241 for (size_t i = 1; i < N; ++i) {
242 if (i % 4 == 0) out << " |";
245 return out << '>' << AnsiColor::normal;
248template<typename T, std::size_t N>
249inline std::ostream &operator<<(std::ostream &out, const SimdMaskArray<T, N> &m)
251 out << AnsiColor::blue << "«";
252 for (size_t i = 0; i < N; ++i) {
253 if (i > 0 && (i % 4) == 0) {
257 out << AnsiColor::yellow << '1';
259 out << AnsiColor::blue << '0';
262 return out << AnsiColor::blue << "»" << AnsiColor::normal;
268// vim: ft=cpp foldmethod=marker