Warning, /include/Vc/IO is written in an unsupported language. File is not indexed.
0001 /* This file is part of the Vc library. {{{
0002 Copyright © 2009-2015 Matthias Kretz <kretz@kde.org>
0003
0004 Redistribution and use in source and binary forms, with or without
0005 modification, are permitted provided that the following conditions are met:
0006 * Redistributions of source code must retain the above copyright
0007 notice, this list of conditions and the following disclaimer.
0008 * Redistributions in binary form must reproduce the above copyright
0009 notice, this list of conditions and the following disclaimer in the
0010 documentation and/or other materials provided with the distribution.
0011 * Neither the names of contributing organizations nor the
0012 names of its contributors may be used to endorse or promote products
0013 derived from this software without specific prior written permission.
0014
0015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
0016 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0017 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0018 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
0019 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0021 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0022 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0024 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025
0026 }}}*/
0027
0028 #ifndef VC_IO_
0029 #define VC_IO_
0030
0031 #include "common/types.h"
0032 #include "common/simdarrayfwd.h"
0033 #include "common/memoryfwd.h"
0034 #include <iostream>
0035
0036 #if defined(__GNUC__) && !defined(_WIN32) && defined(_GLIBCXX_OSTREAM)
0037 #define Vc_HACK_OSTREAM_FOR_TTY 1
0038 #endif
0039
0040 #ifdef Vc_HACK_OSTREAM_FOR_TTY
0041 #include <unistd.h>
0042 #include <ext/stdio_sync_filebuf.h>
0043 #endif
0044
0045 namespace Vc_VERSIONED_NAMESPACE
0046 {
0047 namespace
0048 {
0049 #ifdef Vc_HACK_OSTREAM_FOR_TTY
0050 class hacked_ostream : public std::ostream
0051 {
0052 public:
0053 using std::ostream::_M_streambuf;
0054 };
0055 bool mayUseColor(const std::ostream &os) __attribute__((__const__));
0056 bool mayUseColor(const std::ostream &os)
0057 {
0058 std::basic_streambuf<char> *hack1 =
0059 const_cast<std::basic_streambuf<char> *>(os.*(&hacked_ostream::_M_streambuf));
0060 __gnu_cxx::stdio_sync_filebuf<char> *hack =
0061 dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char> *>(hack1);
0062 if (!hack) {
0063 return false;
0064 }
0065 FILE *file = hack->file();
0066 return 1 == isatty(fileno(file));
0067 }
0068 #else
0069 bool mayUseColor(const std::ostream &) { return false; }
0070 #endif
0071 } // anonymous namespace
0072
0073 namespace AnsiColor
0074 {
0075 struct Type
0076 {
0077 const char *data;
0078 };
0079 static const Type green = {"\033[1;40;32m"};
0080 static const Type yellow = {"\033[1;40;33m"};
0081 static const Type blue = {"\033[1;40;34m"};
0082 static const Type normal = {"\033[0m"};
0083
0084 inline std::ostream &operator<<(std::ostream &out, const Type &c)
0085 {
0086 if (mayUseColor(out)) {
0087 out << c.data;
0088 }
0089 return out;
0090 }
0091 } // namespace AnsiColor
0092
0093 /**
0094 * \ingroup Vectors
0095 * \headerfile IO <Vc/IO>
0096 *
0097 * Prints the contents of a vector into a stream object.
0098 *
0099 * \code
0100 * const Vc::int_v v(Vc::IndexesFromZero);
0101 * std::cout << v << std::endl;
0102 * \endcode
0103 * will output (with SSE):
0104 \verbatim
0105 [0, 1, 2, 3]
0106 \endverbatim
0107 *
0108 * \param out Any standard C++ ostream object. For example std::cout or a
0109 * std::stringstream object.
0110 * \param v Any Vc::Vector object.
0111 * \return The ostream object: to chain multiple stream operations.
0112 *
0113 * \note With the GNU standard library this function will check whether the
0114 * output stream is a tty in which case it colorizes the output.
0115 */
0116 template <typename T, typename Abi>
0117 inline std::ostream &operator<<(std::ostream &out, const Vc::Vector<T, Abi> &v)
0118 {
0119 using TT = typename std::conditional<std::is_same<T, char>::value ||
0120 std::is_same<T, unsigned char>::value ||
0121 std::is_same<T, signed char>::value,
0122 int,
0123 T>::type;
0124 out << AnsiColor::green << '[';
0125 out << TT(v[0]);
0126 for (size_t i = 1; i < v.Size; ++i) {
0127 out << ", " << TT(v[i]);
0128 }
0129 out << ']' << AnsiColor::normal;
0130 return out;
0131 }
0132
0133 /**
0134 * \ingroup Masks
0135 * \headerfile IO <Vc/IO>
0136 *
0137 * Prints the contents of a mask into a stream object.
0138 *
0139 * \code
0140 * const Vc::short_m m = Vc::short_v::IndexesFromZero() < 3;
0141 * std::cout << m << std::endl;
0142 * \endcode
0143 * will output (with SSE):
0144 \verbatim
0145 m[1110 0000]
0146 \endverbatim
0147 *
0148 * \param out Any standard C++ ostream object. For example std::cout or a
0149 * std::stringstream object.
0150 * \param m Any Vc::Mask object.
0151 * \return The ostream object: to chain multiple stream operations.
0152 *
0153 * \note With the GNU standard library this function will check whether the
0154 * output stream is a tty in which case it colorizes the output.
0155 */
0156 template <typename T, typename Abi>
0157 inline std::ostream &operator<<(std::ostream &out, const Vc::Mask<T, Abi> &m)
0158 {
0159 out << AnsiColor::blue << "m[";
0160 for (unsigned int i = 0; i < m.Size; ++i) {
0161 if (i > 0 && (i % 4) == 0) {
0162 out << ' ';
0163 }
0164 if (m[i]) {
0165 out << AnsiColor::yellow << '1';
0166 } else {
0167 out << AnsiColor::blue << '0';
0168 }
0169 }
0170 out << AnsiColor::blue << ']' << AnsiColor::normal;
0171 return out;
0172 }
0173
0174 namespace Common
0175 {
0176 #ifdef DOXYGEN
0177 /**
0178 * \ingroup Utilities
0179 * \headerfile dox.h <Vc/IO>
0180 *
0181 * Prints the contents of a Memory object into a stream object.
0182 *
0183 * \code
0184 * Vc::Memory<int_v, 10> m;
0185 * for (int i = 0; i < m.entriesCount(); ++i) {
0186 * m[i] = i;
0187 * }
0188 * std::cout << m << std::endl;
0189 * \endcode
0190 * will output (with SSE):
0191 \verbatim
0192 {[0, 1, 2, 3] [4, 5, 6, 7] [8, 9, 0, 0]}
0193 \endverbatim
0194 *
0195 * \param s Any standard C++ ostream object. For example std::cout or a std::stringstream object.
0196 * \param m Any Vc::Memory object.
0197 * \return The ostream object: to chain multiple stream operations.
0198 *
0199 * \note With the GNU standard library this function will check whether the
0200 * output stream is a tty in which case it colorizes the output.
0201 *
0202 * \warning Please do not forget that printing a large memory object can take a long time.
0203 */
0204 template<typename V, typename Parent, typename Dimension, typename RM>
0205 inline std::ostream &operator<<(std::ostream &s, const Vc::MemoryBase<V, Parent, Dimension, RM> &m);
0206 #endif
0207
0208 template<typename V, typename Parent, typename RM>
0209 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 1, RM> &m )
0210 {
0211 out << AnsiColor::blue << '{' << AnsiColor::normal;
0212 for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
0213 out << V(m.vector(i));
0214 }
0215 out << AnsiColor::blue << '}' << AnsiColor::normal;
0216 return out;
0217 }
0218
0219 template<typename V, typename Parent, typename RM>
0220 inline std::ostream &operator<<(std::ostream &out, const MemoryBase<V, Parent, 2, RM> &m )
0221 {
0222 out << AnsiColor::blue << '{' << AnsiColor::normal;
0223 for (size_t i = 0; i < m.rowsCount(); ++i) {
0224 if (i > 0) {
0225 out << "\n ";
0226 }
0227 const size_t vcount = m[i].vectorsCount();
0228 for (size_t j = 0; j < vcount; ++j) {
0229 out << V(m[i].vector(j));
0230 }
0231 }
0232 out << AnsiColor::blue << '}' << AnsiColor::normal;
0233 return out;
0234 }
0235 } // namespace Common
0236
0237 template<typename T, std::size_t N>
0238 inline std::ostream &operator<<(std::ostream &out, const SimdArray<T, N> &v)
0239 {
0240 out << AnsiColor::green << '<' << v[0];
0241 for (size_t i = 1; i < N; ++i) {
0242 if (i % 4 == 0) out << " |";
0243 out << ' ' << v[i];
0244 }
0245 return out << '>' << AnsiColor::normal;
0246 }
0247
0248 template<typename T, std::size_t N>
0249 inline std::ostream &operator<<(std::ostream &out, const SimdMaskArray<T, N> &m)
0250 {
0251 out << AnsiColor::blue << "«";
0252 for (size_t i = 0; i < N; ++i) {
0253 if (i > 0 && (i % 4) == 0) {
0254 out << ' ';
0255 }
0256 if ( m[i] ) {
0257 out << AnsiColor::yellow << '1';
0258 } else {
0259 out << AnsiColor::blue << '0';
0260 }
0261 }
0262 return out << AnsiColor::blue << "»" << AnsiColor::normal;
0263 }
0264 }
0265
0266 #endif // VC_IO_
0267
0268 // vim: ft=cpp foldmethod=marker