Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:50:14

0001 #ifndef FIRMWARE_utils_emulator_io_h
0002 #define FIRMWARE_utils_emulator_io_h
0003 
0004 #include <fstream>
0005 #include <vector>
0006 #include "DataFormats/L1TParticleFlow/interface/datatypes.h"
0007 
0008 namespace l1ct {
0009 
0010   template <typename T>
0011   inline bool writeVar(const T &src, std::fstream &to) {
0012     to.write(reinterpret_cast<const char *>(&src), sizeof(T));
0013     return to.good();
0014   }
0015 
0016   template <typename T>
0017   inline bool readVar(std::fstream &from, T &to) {
0018     from.read(reinterpret_cast<char *>(&to), sizeof(T));
0019     return from.good();
0020   }
0021 
0022   template <typename T>
0023   bool writeAP(const T &src, std::fstream &to) {
0024     for (unsigned int i = 0, n = T::width; i < n; i += 32) {
0025       ap_uint<32> word = src(std::min(i + 31, n - 1), i);
0026       uint32_t w32 = word.to_uint();
0027       if (!writeVar(w32, to))
0028         return false;
0029     }
0030     return true;
0031   }
0032 
0033   template <typename T>
0034   bool readAP(std::fstream &from, T &to) {
0035     uint32_t w32;
0036     for (unsigned int i = 0, n = T::width; i < n; i += 32) {
0037       if (!readVar(from, w32))
0038         return false;
0039       ap_uint<32> word = w32;
0040       to(std::min(i + 31, n - 1), i) = word(std::min(31u, n - i - 1), 0);
0041     }
0042     return true;
0043   }
0044 
0045   template <typename T>
0046   bool writeObj(const T &obj, std::fstream &to) {
0047     return writeAP(obj.pack(), to);
0048   }
0049 
0050   template <typename T>
0051   bool readObj(std::fstream &from, T &obj) {
0052     ap_uint<T::BITWIDTH> packed;
0053     if (!readAP(from, packed))
0054       return false;
0055     obj = T::unpack(packed);
0056     return true;
0057   }
0058 
0059   template <typename T>
0060   bool writeMany(const std::vector<T> &objs, std::fstream &to) {
0061     uint32_t number = objs.size();
0062     writeVar(number, to);
0063     for (uint32_t i = 0; i < number; ++i) {
0064       objs[i].write(to);
0065     }
0066     return to.good();
0067   }
0068 
0069   template <int NB>
0070   bool writeMany(const std::vector<ap_uint<NB>> &objs, std::fstream &to) {
0071     uint32_t number = objs.size();
0072     writeVar(number, to);
0073     for (uint32_t i = 0; i < number; ++i) {
0074       writeAP(objs[i], to);
0075     }
0076     return to.good();
0077   }
0078 
0079   template <typename T>
0080   bool readMany(std::fstream &from, std::vector<T> &objs) {
0081     uint32_t number = 0;
0082     readVar(from, number);
0083     objs.resize(number);
0084     for (uint32_t i = 0; i < number; ++i)
0085       objs[i].read(from);
0086     return from.good();
0087   }
0088 
0089   template <int NB>
0090   bool readMany(std::fstream &from, std::vector<ap_uint<NB>> &objs) {
0091     uint32_t number = 0;
0092     readVar(from, number);
0093     objs.resize(number);
0094     for (uint32_t i = 0; i < number; ++i)
0095       readAP(from, objs[i]);
0096     return from.good();
0097   }
0098 
0099 }  // namespace l1ct
0100 
0101 #endif