| 93 | |
| 94 | /// Reference object inserts or extracts sub-byte items |
| 95 | class reference { |
| 96 | /// Pointer to storage element |
| 97 | Storage *ptr_{nullptr}; |
| 98 | |
| 99 | /// Index into elements packed into Storage object |
| 100 | int idx_{0}; |
| 101 | |
| 102 | public: |
| 103 | |
| 104 | reference() = default; |
| 105 | |
| 106 | /// Ctor |
| 107 | CUTLASS_HOST_DEVICE |
| 108 | reference(Storage *ptr, int idx = 0): ptr_(ptr), idx_(idx) { } |
| 109 | |
| 110 | /// Assignment |
| 111 | CUTLASS_HOST_DEVICE |
| 112 | reference &operator=(T x) { |
| 113 | // `*ptr_ & kUpdateMask` will read ptr_ before write to it |
| 114 | // This means code pattern like |
| 115 | // |
| 116 | // ```cpp |
| 117 | // Array<half_t, N> result; |
| 118 | // result[0] = xxx; |
| 119 | // ``` |
| 120 | // |
| 121 | // Will leads to compiler warning on use of uninitialized member variable. Although we know |
| 122 | // this read of uninitialized member variable is harmeless. |
| 123 | |
| 124 | #if defined(__clang__) |
| 125 | # pragma clang diagnostic push |
| 126 | # pragma clang diagnostic ignored "-Wuninitialized" |
| 127 | #elif defined(__GNUC__) |
| 128 | # pragma GCC diagnostic push |
| 129 | # pragma GCC diagnostic ignored "-Wuninitialized" |
| 130 | # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 131 | #endif |
| 132 | |
| 133 | Storage item = (reinterpret_cast<Storage const &>(x) & kMask); |
| 134 | |
| 135 | Storage kUpdateMask = Storage(~(kMask << (idx_ * sizeof_bits<T>::value))); |
| 136 | |
| 137 | *ptr_ = Storage(((*ptr_ & kUpdateMask) | (item << idx_ * sizeof_bits<T>::value))); |
| 138 | |
| 139 | #if defined(__clang__) |
| 140 | # pragma clang diagnostic pop |
| 141 | #elif defined(__GNUC__) |
| 142 | # pragma GCC diagnostic pop |
| 143 | #endif |
| 144 | |
| 145 | return *this; |
| 146 | } |
| 147 | |
| 148 | CUTLASS_HOST_DEVICE |
| 149 | T get() const { |
| 150 | Storage item = Storage((*ptr_ >> (idx_ * sizeof_bits<T>::value)) & kMask); |
| 151 | return reinterpret_cast<T const &>(item); |
| 152 | } |