Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===- subzero/crosstest/vectors.h - Common SIMD vector utilies -*- C++ -*-===// | |
|
Jim Stichnoth
2014/07/18 23:45:47
While you're touching this, could you add the lice
wala
2014/07/19 00:11:14
Done.
| |
| 2 // | |
| 3 // The Subzero Code Generator | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // This file provides declarations for PNaCl portable SIMD vector types. In | |
| 11 // addition, this file provides utilies that may be useful for crosstesting | |
| 12 // vector code. | |
| 13 // | |
| 14 //===----------------------------------------------------------------------===// | |
| 15 | |
| 16 #ifndef VECTORS_H | |
| 17 #define VECTORS_H | |
| 18 | |
| 19 #include <stdint.h> | |
| 20 #include <string> | |
| 21 #include <sstream> | |
| 22 | |
| 23 #include "vectors.def" | |
| 24 | |
| 25 // PNaCl portable vector types | |
| 26 // Types declared: v4si32, v4ui32, v8si16, v8ui16, v16si8, v16ui8, v4f32 | |
| 27 #define X(ty, eltty, castty) typedef eltty ty __attribute__((vector_size(16))); | |
| 28 VECTOR_TYPE_TABLE | |
| 29 #undef X | |
| 30 | |
| 31 // i1 vector types are not native C++ SIMD vector types. Instead, for | |
| 32 // testing, they are expanded by the test code into native 128 bit | |
| 33 // SIMD vector types with the appropriate number of elements. | |
| 34 // Representing the types in Vectors<> requires a unique label for each | |
| 35 // type which this declaration provides. | |
| 36 // Types declared: v4i1, v8i1, v16i1 | |
| 37 #define X(ty, expandedty, numelements) class ty; | |
| 38 I1_VECTOR_TYPE_TABLE | |
| 39 #undef X | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 template <typename T> struct Vectors; | |
| 44 | |
| 45 // Vectors<T> provides information about a vector type with label T: | |
| 46 // * Vectors<T>::Ty is the C++ vector type | |
| 47 // * Vectors<T>::ElementTy is the C++ element type | |
| 48 // * Vectors<T>::CastTy is a type that is safe to cast elements to and from | |
| 49 // and is used for getting the representation of elements in ostreams | |
| 50 // * Vectors<T>::NumElements is the number of elements | |
| 51 // * Vectors<T>::TypeName is a string that names the type | |
| 52 | |
| 53 #define DECLARE_VECTOR_TYPE(LABEL, TY, ELTTY, CASTTY, NUM_ELEMENTS) \ | |
| 54 template <> struct Vectors<LABEL> { \ | |
| 55 typedef TY Ty; \ | |
| 56 typedef ELTTY ElementTy; \ | |
| 57 typedef CASTTY CastTy; \ | |
| 58 static const size_t NumElements; \ | |
| 59 static const char *const TypeName; \ | |
| 60 }; \ | |
| 61 const size_t Vectors<LABEL>::NumElements = NUM_ELEMENTS; \ | |
| 62 const char *const Vectors<LABEL>::TypeName = #LABEL; | |
| 63 | |
| 64 #define X(ty, eltty, castty) \ | |
| 65 DECLARE_VECTOR_TYPE(ty, ty, eltty, castty, (sizeof(ty) / sizeof(eltty))) | |
| 66 VECTOR_TYPE_TABLE | |
| 67 #undef X | |
| 68 | |
| 69 #define X(ty, expandedty, numelements) \ | |
| 70 DECLARE_VECTOR_TYPE(ty, expandedty, bool, int64_t, numelements) | |
| 71 I1_VECTOR_TYPE_TABLE | |
| 72 #undef X | |
| 73 | |
| 74 #undef DECLARE_VECTOR_TYPE | |
| 75 | |
| 76 // Return a string representation of the vector. | |
| 77 template <typename T> | |
| 78 std::string vectAsString(const typename Vectors<T>::Ty Vect) { | |
| 79 std::ostringstream OS; | |
| 80 for (size_t i = 0; i < Vectors<T>::NumElements; ++i) { | |
| 81 if (i > 0) | |
| 82 OS << " "; | |
| 83 OS << (typename Vectors<T>::CastTy)Vect[i]; | |
| 84 } | |
| 85 return OS.str(); | |
| 86 } | |
| 87 | |
| 88 // In some crosstests, test vectors are deterministically constructed by | |
| 89 // selecting elements from a pool of scalar values based on a | |
| 90 // pseudorandom sequence. Testing all possible combinations of scalar | |
| 91 // values from the value pool is often not tractable. | |
| 92 // | |
| 93 // TODO: Replace with a portable PRNG from C++11. | |
| 94 class PRNG { | |
| 95 public: | |
| 96 PRNG(uint32_t Seed = 1) : State(Seed) {} | |
| 97 | |
| 98 uint32_t operator()() { | |
| 99 // Lewis, Goodman, and Miller (1969) | |
| 100 State = (16807 * State) % 2147483647; | |
| 101 return State; | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 uint32_t State; | |
| 106 }; | |
| 107 | |
| 108 } // end anonymous namespace | |
| 109 | |
| 110 #endif // VECTORS_H | |
| OLD | NEW |