1 /* This file is part of the Vc library. {{{
2 Copyright © 2009-2015 Matthias Kretz <kretz@kde.org>
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.
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.
32 #include "common/types.h"
33 #include "common/simdarrayfwd.h"
34 #include "common/memoryfwd.h"
37 #if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
38 #define Vc_HACK_OSTREAM_FOR_TTY 1
41 #ifdef Vc_HACK_OSTREAM_FOR_TTY
43 #include <ext/stdio_sync_filebuf.h>
46 namespace Vc_VERSIONED_NAMESPACE
50 #ifdef Vc_HACK_OSTREAM_FOR_TTY
51 class hacked_ostream : public std::ostream
54 using std::ostream::_M_streambuf;
56 bool mayUseColor(const std::ostream &os) __attribute__((__const__));
57 bool mayUseColor(const std::ostream &os)
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);
66 FILE *file = hack->file();
67 return 1 == isatty(fileno(file));
70 bool mayUseColor(const std::ostream &) { return false; }
72 } // anonymous namespace
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"};
85 inline std::ostream &operator<<(std::ostream &out, const Type &c)
87 if (mayUseColor(out)) {
92 } // namespace AnsiColor
96 * \headerfile IO <Vc/IO>
98 * Prints the contents of a vector into a stream object.
101 * const Vc::int_v v(Vc::IndexesFromZero);
102 * std::cout << v << std::endl;
104 * will output (with SSE):
109 * \param out Any standard C++ ostream object. For example std::cout or a
110 * std::stringstream object.
111 * \param v Any Vc::Vector object.
112 * \return The ostream object: to chain multiple stream operations.
114 * \note With the GNU standard library this function will check whether the
115 * output stream is a tty in which case it colorizes the output.
117 template <typename T, typename Abi>
118 inline std::ostream &operator<<(std::ostream &out, const Vc::Vector<T, Abi> &v)
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,
125 out << AnsiColor::green << '[';
127 for (size_t i = 1; i < v.Size; ++i) {
128 out << ", " << TT(v[i]);
130 out << ']' << AnsiColor::normal;
136 * \headerfile IO <Vc/IO>
138 * Prints the contents of a mask into a stream object.
141 * const Vc::short_m m = Vc::short_v::IndexesFromZero() < 3;
142 * std::cout << m << std::endl;
144 * will output (with SSE):
149 * \param out Any standard C++ ostream object. For example std::cout or a
150 * std::stringstream object.
151 * \param m Any Vc::Mask object.
152 * \return The ostream object: to chain multiple stream operations.
154 * \note With the GNU standard library this function will check whether the
155 * output stream is a tty in which case it colorizes the output.
157 template <typename T, typename Abi>
158 inline std::ostream &operator<<(std::ostream &out, const Vc::Mask<T, Abi> &m)
160 out << AnsiColor::blue << "m[";
161 for (unsigned int i = 0; i < m.Size; ++i) {
162 if (i > 0 && (i % 4) == 0) {
166 out << AnsiColor::yellow << '1';
168 out << AnsiColor::blue << '0';
171 out << AnsiColor::blue << ']' << AnsiColor::normal;
180 * \headerfile dox.h <Vc/IO>
182 * Prints the contents of a Memory object into a stream object.
185 * Vc::Memory<int_v, 10> m;
186 * for (int i = 0; i < m.entriesCount(); ++i) {
189 * std::cout << m << std::endl;
191 * will output (with SSE):
193 {[0, 1, 2, 3] [4, 5, 6, 7] [8, 9, 0, 0]}
196 * \param s Any standard C++ ostream object. For example std::cout or a std::stringstream object.
197 * \param m Any Vc::Memory object.
198 * \return The ostream object: to chain multiple stream operations.
200 * \note With the GNU standard library this function will check whether the
201 * output stream is a tty in which case it colorizes the output.
203 * \warning Please do not forget that printing a large memory object can take a long time.
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);
209 template<typename V, typename Parent, typename RM>
210 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 1, RM> &m )
212 out << AnsiColor::blue << '{' << AnsiColor::normal;
213 for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
214 out << V(m.vector(i));
216 out << AnsiColor::blue << '}' << AnsiColor::normal;
220 template<typename V, typename Parent, typename RM>
221 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 2, RM> &m )
223 out << AnsiColor::blue << '{' << AnsiColor::normal;
224 for (size_t i = 0; i < m.rowsCount(); ++i) {
228 const size_t vcount = m[i].vectorsCount();
229 for (size_t j = 0; j < vcount; ++j) {
230 out << V(m[i].vector(j));
233 out << AnsiColor::blue << '}' << AnsiColor::normal;
236 } // namespace Common
238 template<typename T, std::size_t N>
239 inline std::ostream &operator<<(std::ostream &out, const SimdArray<T, N> &v)
241 out << AnsiColor::green << '<' << v[0];
242 for (size_t i = 1; i < N; ++i) {
243 if (i % 4 == 0) out << " |";
246 return out << '>' << AnsiColor::normal;
249 template<typename T, std::size_t N>
250 inline std::ostream &operator<<(std::ostream &out, const SimdMaskArray<T, N> &m)
252 out << AnsiColor::blue << "«";
253 for (size_t i = 0; i < N; ++i) {
254 if (i > 0 && (i % 4) == 0) {
258 out << AnsiColor::yellow << '1';
260 out << AnsiColor::blue << '0';
263 return out << AnsiColor::blue << "»" << AnsiColor::normal;
269 // vim: ft=cpp foldmethod=marker