Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:11

0001 /*
0002  * SPDX-PackageName: "covfie, a part of the ACTS project"
0003  * SPDX-FileCopyrightText: 2022 CERN
0004  * SPDX-License-Identifier: MPL-2.0
0005  */
0006 
0007 #pragma once
0008 
0009 #include <cassert>
0010 #include <iostream>
0011 #include <memory>
0012 #include <sstream>
0013 
0014 enum class fp_type {
0015     IEEE754_SINGLE = 1,
0016     IEEE754_DOUBLE = 2
0017 };
0018 
0019 template <typename T>
0020 class fp_type_id
0021 {
0022 };
0023 
0024 template <>
0025 class fp_type_id<float>
0026 {
0027     static constexpr fp_type value = fp_type::IEEE754_SINGLE;
0028 };
0029 
0030 template <>
0031 class fp_type_id<double>
0032 {
0033     static constexpr fp_type value = fp_type::IEEE754_DOUBLE;
0034 };
0035 
0036 namespace covfie::utility {
0037 static constexpr uint32_t MAGIC_HEADER = 0xC04F1EAB;
0038 static constexpr uint32_t MAGIC_FOOTER = 0xC04F1E70;
0039 
0040 template <typename T>
0041 T read_binary(std::istream & fs)
0042 {
0043     static_assert(
0044         std::is_standard_layout_v<T>, "Binary IO type must be standard layout!"
0045     );
0046 
0047     assert(fs.good() && !fs.eof() && !fs.fail() && !fs.bad());
0048 
0049     T rv;
0050 
0051     fs.read(reinterpret_cast<char *>(&rv), sizeof(T));
0052 
0053     return rv;
0054 }
0055 
0056 inline std::ostream & write_io_header(std::ostream & fs, uint32_t hdr)
0057 {
0058     fs.write(
0059         reinterpret_cast<const char *>(&MAGIC_HEADER),
0060         sizeof(decltype(MAGIC_HEADER))
0061     );
0062     fs.write(reinterpret_cast<const char *>(&hdr), sizeof(decltype(hdr)));
0063 
0064     return fs;
0065 }
0066 
0067 inline std::istream & read_io_header(std::istream & fs, uint32_t hdr)
0068 {
0069     uint32_t hdr1, hdr2;
0070 
0071     hdr1 = read_binary<uint32_t>(fs);
0072     hdr2 = read_binary<uint32_t>(fs);
0073 
0074     if (hdr1 != MAGIC_HEADER) {
0075         std::stringstream err;
0076         err << "Deserialization of covfie vector field due to non-matching "
0077                "global header (should be 0x"
0078             << std::hex << std::uppercase << MAGIC_HEADER << ", but was 0x"
0079             << hdr1 << ")";
0080         throw std::runtime_error(err.str());
0081     }
0082 
0083     if (hdr2 != hdr) {
0084         std::stringstream err;
0085         err << "Deserialization of covfie vector field due to non-matching "
0086                "backend header (should be 0x"
0087             << std::hex << std::uppercase << hdr << ", but was 0x" << hdr1
0088             << ")";
0089         throw std::runtime_error(err.str());
0090     }
0091 
0092     return fs;
0093 }
0094 
0095 inline std::ostream & write_io_footer(std::ostream & fs, uint32_t ftr)
0096 {
0097     ftr += 0x20000000;
0098     fs.write(
0099         reinterpret_cast<const char *>(&MAGIC_FOOTER),
0100         sizeof(decltype(MAGIC_FOOTER))
0101     );
0102     fs.write(reinterpret_cast<const char *>(&ftr), sizeof(decltype(ftr)));
0103 
0104     return fs;
0105 }
0106 
0107 inline std::istream & read_io_footer(std::istream & fs, uint32_t ftr)
0108 {
0109     ftr += 0x20000000;
0110     uint32_t ftr1 = 0, ftr2 = 0;
0111 
0112     ftr1 = read_binary<uint32_t>(fs);
0113     ftr2 = read_binary<uint32_t>(fs);
0114 
0115     if (ftr1 != MAGIC_FOOTER) {
0116         std::stringstream err;
0117         err << "Deserialization of covfie vector field due to non-matching "
0118                "global footer (should be 0x"
0119             << std::hex << std::uppercase << MAGIC_FOOTER << ", but was 0x"
0120             << ftr1 << ")";
0121         throw std::runtime_error(err.str());
0122     }
0123 
0124     if (ftr2 != ftr) {
0125         std::stringstream err;
0126         err << "Deserialization of covfie vector field due to non-matching "
0127                "backend footer (should be 0x"
0128             << std::hex << std::uppercase << ftr << ", but was 0x" << ftr2
0129             << ")";
0130         throw std::runtime_error(err.str());
0131     }
0132 
0133     return fs;
0134 }
0135 }