]> git.uio.no Git - u/mrichter/AliRoot.git/blame - Vc/include/Vc/IO
- support compilation with GCC 4.1 and 4.2
[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
27#ifdef __GNUC__
28#include <unistd.h>
29#include <ext/stdio_sync_filebuf.h>
30#endif
31
32#include "internal/namespace.h"
33
34namespace
35{
36 namespace AnsiColor
37 {
38 struct Type { const char *data; };
39 static const Type green = { "\033[1;40;32m" };
40 static const Type yellow = { "\033[1;40;33m" };
41 static const Type blue = { "\033[1;40;34m" };
42 static const Type normal = { "\033[0m" };
43 } // namespace AnsiColor
44
45#ifdef __GNUC__
46 class hacked_ostream : public std::ostream
47 {
48 public:
49 using std::ostream::_M_streambuf;
50 };
51 static bool mayUseColor(const std::ostream &os)
52 __attribute__((__const__));
53 static bool mayUseColor(const std::ostream &os)
54 {
55 std::basic_streambuf<char> *hack1 = const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
56 __gnu_cxx::stdio_sync_filebuf<char> *hack = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
57 if (!hack) {
58 return false;
59 }
60 FILE *file = hack->file();
61 return 1 == isatty(fileno(file));
62 }
63#else
64 static bool mayUseColor(const std::ostream &) { return false; }
65#endif
66} // anonymous namespace
67
68namespace std
69{
6936ae04 70static inline std::ostream &operator<<(std::ostream &out, const AnsiColor::Type &c)
f22341db 71{
72 if (mayUseColor(out)) {
73 out << c.data;
74 }
75 return out;
76}
77
78template<typename T>
6936ae04 79static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<T> &v)
f22341db 80{
81 out << AnsiColor::green << "[";
82 out << v[0];
83 for (int i = 1; i < v.Size; ++i) {
84 out << ", " << v[i];
85 }
86 out << "]" << AnsiColor::normal;
87 return out;
88}
89
6936ae04 90static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<char> &v)
f22341db 91{
92 out << AnsiColor::green << "[";
93 out << int(v[0]);
94 for (int i = 1; i < v.Size; ++i) {
95 out << ", " << int(v[i]);
96 }
97 out << "]" << AnsiColor::normal;
98 return out;
99}
6936ae04 100static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Vector<unsigned char> &v)
f22341db 101{
102 out << AnsiColor::green << "[";
103 out << int(v[0]);
104 for (int i = 1; i < v.Size; ++i) {
105 out << ", " << int(v[i]);
106 }
107 out << "]" << AnsiColor::normal;
108 return out;
109}
110
111#ifdef VC_HAVE_FMA
112template<typename T>
6936ae04 113static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::VectorMultiplication<T> &v)
f22341db 114{
115 return out << VECTOR_NAMESPACE::Vector<T>(v);
116}
117#endif
118
119#ifdef VC_IMPL_AVX
120template<unsigned int VectorSize, size_t RegisterWidth>
6936ae04 121static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Mask<VectorSize, RegisterWidth> &m)
f22341db 122#else
123template<unsigned int VectorSize>
6936ae04 124static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Mask<VectorSize> &m)
f22341db 125#endif
126{
127 out << AnsiColor::blue << "m[";
128 for (unsigned int i = 0; i < VectorSize; ++i) {
129 if (i > 0 && (i % 4) == 0) {
130 out << " ";
131 }
132 if ( m[i] ) {
133 out << AnsiColor::yellow << '1';
134 } else {
135 out << AnsiColor::blue << '0';
136 }
137 }
138 out << AnsiColor::blue << "]" << AnsiColor::normal;
139 return out;
140}
141#if VC_IMPL_SSE
6936ae04 142static std::ostream &operator<<(std::ostream &out, const VECTOR_NAMESPACE::Float8Mask &m)
f22341db 143{
144 out << AnsiColor::blue << "m[";
145 for (unsigned int i = 0; i < 8; ++i) {
146 if (i > 0 && (i % 4) == 0) {
147 out << " ";
148 }
149 if ( m[i] ) {
150 out << AnsiColor::yellow << '1';
151 } else {
152 out << AnsiColor::blue << '0';
153 }
154 }
155 out << AnsiColor::blue << "]" << AnsiColor::normal;
156 return out;
157}
158#endif
159
160template<typename V, typename Parent, typename RM>
6936ae04 161static std::ostream &operator<<(std::ostream &out, const Vc::MemoryBase<V, Parent, 1, RM> &m )
f22341db 162{
163 out << AnsiColor::blue << "{" << AnsiColor::normal;
164 for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
165 out << V(m.vector(i));
166 }
167 out << AnsiColor::blue << "}" << AnsiColor::normal;
168 return out;
169}
170
171template<typename V, typename Parent, typename RM>
6936ae04 172static std::ostream &operator<<(std::ostream &out, const Vc::MemoryBase<V, Parent, 2, RM> &m )
f22341db 173{
174 out << AnsiColor::blue << "{" << AnsiColor::normal;
175 for (size_t i = 0; i < m.rowsCount(); ++i) {
176 if (i > 0) {
177 out << "\n ";
178 }
179 const size_t vcount = m[i].vectorsCount();
180 for (size_t j = 0; j < vcount; ++j) {
181 out << V(m[i].vector(j));
182 }
183 }
184 out << AnsiColor::blue << "}" << AnsiColor::normal;
185 return out;
186}
187} // namespace std
188
189#undef VECTOR_NAMESPACE
190
191#endif // VECIO_H
192
193// vim: ft=cpp