Line data Source code
1 : #include <cstdint> 2 : #include <sstream> 3 : 4 : #include "Tools/Exception/exception.hpp" 5 : #include "Tools/compute_bytes.h" 6 : 7 : size_t 8 475 : spu::tools::compute_bytes(const size_t n_elmts, const std::type_index type) 9 : { 10 475 : if (type == typeid(int8_t)) 11 3 : return n_elmts * sizeof(int8_t); 12 472 : else if (type == typeid(uint8_t)) 13 345 : return n_elmts * sizeof(uint8_t); 14 127 : else if (type == typeid(int16_t)) 15 0 : return n_elmts * sizeof(int16_t); 16 127 : else if (type == typeid(uint16_t)) 17 0 : return n_elmts * sizeof(uint16_t); 18 127 : else if (type == typeid(int32_t)) 19 27 : return n_elmts * sizeof(int32_t); 20 100 : else if (type == typeid(uint32_t)) 21 84 : return n_elmts * sizeof(uint32_t); 22 16 : else if (type == typeid(int64_t)) 23 0 : return n_elmts * sizeof(int64_t); 24 16 : else if (type == typeid(uint64_t)) 25 16 : return n_elmts * sizeof(uint64_t); 26 0 : else if (type == typeid(float)) 27 0 : return n_elmts * sizeof(float); 28 0 : else if (type == typeid(double)) 29 0 : return n_elmts * sizeof(double); 30 : else 31 : { 32 0 : std::stringstream message; 33 0 : message << "This should never happen."; 34 0 : throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str()); 35 0 : } 36 : } 37 : 38 : std::vector<size_t> 39 202 : spu::tools::compute_bytes(const std::vector<size_t>& n_elmts, const std::vector<std::type_index>& type) 40 : { 41 202 : if (n_elmts.size() != type.size()) 42 : { 43 0 : std::stringstream message; 44 0 : message << "'n_elmts.size()' has to be equal to 'type.size()' ('n_elmts.size()' = " << n_elmts.size() 45 0 : << ", 'type.size()' = " << type.size() << ")."; 46 0 : throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str()); 47 0 : } 48 : 49 202 : std::vector<size_t> bytes(n_elmts.size()); 50 551 : for (size_t i = 0; i < n_elmts.size(); i++) 51 349 : bytes[i] = spu::tools::compute_bytes(n_elmts[i], type[i]); 52 202 : return bytes; 53 0 : }