]> git.uio.no Git - u/mrichter/AliRoot.git/blame - Vc/include/Vc/IO
update to Vc 0.7.3-dev
[u/mrichter/AliRoot.git] / Vc / include / Vc / IO
CommitLineData
f22341db 1/* This file is part of the Vc library.
2
3 Copyright (C) 2009-2011 Matthias Kretz <kretz@kde.org>
4
5 Vc is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of
8 the License, or (at your option) any later version.
9
10 Vc is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with Vc. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#ifndef VECIO_H
21#define VECIO_H
22
23#include "vector.h"
24#include "Memory"
25#include <iostream>
26
c017a39f 27#if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
28#define VC_HACK_OSTREAM_FOR_TTY 1
29#endif
30
31#ifdef VC_HACK_OSTREAM_FOR_TTY
f22341db 32#include <unistd.h>
33#include <ext/stdio_sync_filebuf.h>
34#endif
35
36#include "internal/namespace.h"
37
38namespace
39{
40 namespace AnsiColor
41 {
42 struct Type { const char *data; };
43 static const Type green = { "\033[1;40;32m" };
44 static const Type yellow = { "\033[1;40;33m" };
45 static const Type blue = { "\033[1;40;34m" };
46 static const Type normal = { "\033[0m" };
47 } // namespace AnsiColor
48
c017a39f 49#ifdef VC_HACK_OSTREAM_FOR_TTY
f22341db 50 class hacked_ostream : public std::ostream
51 {
52 public:
53 using std::ostream::_M_streambuf;
54 };
c017a39f 55 __attribute__((__const__)) bool mayUseColor(const std::ostream &os)
f22341db 56 {
57 std::basic_streambuf<char> *hack1 = const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
58 __gnu_cxx::stdio_sync_filebuf<char> *hack = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
59 if (!hack) {
60 return false;
61 }
62 FILE *file = hack->file();
63 return 1 == isatty(fileno(file));
64 }
65#else
c017a39f 66 inline bool mayUseColor(const std::ostream &) { return false; }
f22341db 67#endif
68} // anonymous namespace
69
70namespace std
71{
c017a39f 72inline std::ostream &operator<<(std::ostream &out, const AnsiColor::Type &c)
f22341db 73{
74 if (mayUseColor(out)) {
75 out << c.data;
76 }
77 return out;
78}
79
80template<typename T>
c017a39f 81inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<T> &v)
f22341db 82{
83 out << AnsiColor::green << "[";
84 out << v[0];
85 for (int i = 1; i < v.Size; ++i) {
86 out << ", " << v[i];
87 }
88 out << "]" << AnsiColor::normal;
89 return out;
90}
91
c017a39f 92inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<char> &v)
f22341db 93{
94 out << AnsiColor::green << "[";
95 out << int(v[0]);
96 for (int i = 1; i < v.Size; ++i) {
97 out << ", " << int(v[i]);
98 }
99 out << "]" << AnsiColor::normal;
100 return out;
101}
c017a39f 102inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<unsigned char> &v)
f22341db 103{
104 out << AnsiColor::green << "[";
105 out << int(v[0]);
106 for (int i = 1; i < v.Size; ++i) {
107 out << ", " << int(v[i]);
108 }
109 out << "]" << AnsiColor::normal;
110 return out;
111}
112
113#ifdef VC_HAVE_FMA
114template<typename T>
c017a39f 115inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::VectorMultiplication<T> &v)
f22341db 116{
117 return out << VECTOR_NAMESPACE::Vector<T>(v);
118}
119#endif
120
121#ifdef VC_IMPL_AVX
122template<unsigned int VectorSize, size_t RegisterWidth>
c017a39f 123inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Mask<VectorSize, RegisterWidth> &m)
f22341db 124#else
125template<unsigned int VectorSize>
c017a39f 126inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Mask<VectorSize> &m)
f22341db 127#endif
128{
129 out << AnsiColor::blue << "m[";
130 for (unsigned int i = 0; i < VectorSize; ++i) {
131 if (i > 0 && (i % 4) == 0) {
132 out << " ";
133 }
134 if ( m[i] ) {
135 out << AnsiColor::yellow << '1';
136 } else {
137 out << AnsiColor::blue << '0';
138 }
139 }
140 out << AnsiColor::blue << "]" << AnsiColor::normal;
141 return out;
142}
c017a39f 143#ifdef VC_IMPL_SSE
144inline std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Float8Mask &m)
f22341db 145{
146 out << AnsiColor::blue << "m[";
147 for (unsigned int i = 0; i < 8; ++i) {
148 if (i > 0 && (i % 4) == 0) {
149 out << " ";
150 }
151 if ( m[i] ) {
152 out << AnsiColor::yellow << '1';
153 } else {
154 out << AnsiColor::blue << '0';
155 }
156 }
157 out << AnsiColor::blue << "]" << AnsiColor::normal;
158 return out;
159}
160#endif
161
162template<typename V, typename Parent, typename RM>
c017a39f 163inline std::ostream &operator<<(std::ostream &out, const Vc::MemoryBase<V, Parent, 1, RM> &m )
f22341db 164{
165 out << AnsiColor::blue << "{" << AnsiColor::normal;
166 for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
167 out << V(m.vector(i));
168 }
169 out << AnsiColor::blue << "}" << AnsiColor::normal;
170 return out;
171}
172
173template<typename V, typename Parent, typename RM>
c017a39f 174inline std::ostream &operator<<(std::ostream &out, const Vc::MemoryBase<V, Parent, 2, RM> &m )
f22341db 175{
176 out << AnsiColor::blue << "{" << AnsiColor::normal;
177 for (size_t i = 0; i < m.rowsCount(); ++i) {
178 if (i > 0) {
179 out << "\n ";
180 }
181 const size_t vcount = m[i].vectorsCount();
182 for (size_t j = 0; j < vcount; ++j) {
183 out << V(m[i].vector(j));
184 }
185 }
186 out << AnsiColor::blue << "}" << AnsiColor::normal;
187 return out;
188}
189} // namespace std
190
191#undef VECTOR_NAMESPACE
192
193#endif // VECIO_H
194
195// vim: ft=cpp