ChimeraTK-DeviceAccess 03.26.00
Loading...
Searching...
No Matches
IEEE754_SingleConverter.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include "NumericConverter.h"
6
7#include <boost/numeric/conversion/cast.hpp>
8
9#include <memory.h>
10
11#include <cfloat> // for float limits
12
13namespace ChimeraTK {
14
20 template<typename CookedType, typename RAW_ITERATOR, typename COOKED_ITERATOR>
22 const RAW_ITERATOR& raw_begin, const RAW_ITERATOR& raw_end, const COOKED_ITERATOR& cooked_begin) const {
23 // Note: IEEE754_SingleConverter must be instantiable for all raw user types but can only be used for int32_t
24 assert((std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int32_t>::value));
25
26 static_assert(std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int8_t>::value ||
27 std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int16_t>::value ||
28 std::is_same<typename std::iterator_traits<RAW_ITERATOR>::value_type, int32_t>::value,
29 "RAW_ITERATOR template argument must be an iterator with value type equal to int8_t, int16_t or int32_t.");
30
31 static_assert(std::is_same<typename std::iterator_traits<COOKED_ITERATOR>::value_type, CookedType>::value,
32 "COOKED_ITERATOR template argument must be an iterator with value type equal to the CookedType template "
33 "argument.");
34
36 }
37
38 template<typename CookedType, typename RAW_ITERATOR, typename COOKED_ITERATOR>
40 static void impl(const RAW_ITERATOR& raw_begin, const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin);
41 };
42
44 template<typename CookedType>
45 CookedType scalarToCooked(int32_t const& raw) const {
46 CookedType cooked;
47 vectorToCooked<CookedType>(&raw, (&raw) + 1, &cooked);
48 return cooked;
49 }
50
51 template<typename CookedType>
52 uint32_t toRaw(CookedType cookedValue) const;
53
54 explicit IEEE754_SingleConverter(const std::string& = "") {}
55
56 // all IEEE754_SingleConverters are the same
57 bool operator!=(const IEEE754_SingleConverter& /*other*/) const { return false; }
58 bool operator==(const IEEE754_SingleConverter& /*other*/) const { return true; }
59 };
60
61 template<typename CookedType, typename RAW_ITERATOR, typename COOKED_ITERATOR>
63 const RAW_ITERATOR& raw_begin, const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin) {
64 for(auto it = raw_begin; it != raw_end; ++it) {
65 // Step 1: convert the raw data to the "generic" representation in the CPU: float
66 float genericRepresentation;
67 memcpy(&genericRepresentation, &(*it), sizeof(float));
68
69 // Step 2: convert the float to the cooked type
70 *cooked_begin = numeric::convert<CookedType>(genericRepresentation);
71 ++cooked_begin;
72 }
73 }
74
75 template<typename CookedType>
76 uint32_t IEEE754_SingleConverter::toRaw(CookedType cookedValue) const {
77 // step 1: convert from cooked to the generic representation in the CPU
78 // (float)
79 float genericRepresentation = numeric::convert<float>(cookedValue);
80
81 // step 2: reinterpret float to int32 to send it to the device
82 int32_t rawValue;
83 memcpy(&rawValue, &genericRepresentation, sizeof(float));
84
85 return rawValue;
86 }
87
88 template<typename RAW_ITERATOR, typename COOKED_ITERATOR>
89 struct IEEE754_SingleConverter::vectorToCooked_impl<std::string, RAW_ITERATOR, COOKED_ITERATOR> {
90 static void impl(const RAW_ITERATOR& raw_begin, const RAW_ITERATOR& raw_end, COOKED_ITERATOR cooked_begin) {
91 for(auto it = raw_begin; it != raw_end; ++it) {
92 // Step 1: convert the raw data to the "generic" representation in the CPU: float
93 float genericRepresentation;
94 memcpy(&genericRepresentation, &(*it), sizeof(float));
95
96 // Step 2: convert the float to the cooked type
97 *cooked_begin = std::to_string(genericRepresentation);
98 ++cooked_begin;
99 }
100 }
101 };
102
103 template<>
104 [[nodiscard]] uint32_t IEEE754_SingleConverter::toRaw(std::string cookedValue) const;
105
106} // namespace ChimeraTK
@ raw
Raw access: disable any possible conversion from the original hardware data type into the given UserT...
STL namespace.
std::string to_string(const std::string &v)
static void impl(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, COOKED_ITERATOR cooked_begin)
static void impl(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, COOKED_ITERATOR cooked_begin)
Needs to have the same interface as FixedPointConverter, except for the constructor.
IEEE754_SingleConverter(const std::string &="")
bool operator!=(const IEEE754_SingleConverter &) const
CookedType scalarToCooked(int32_t const &raw) const
Inefficient convenience function for converting a single value to cooked.
uint32_t toRaw(CookedType cookedValue) const
bool operator==(const IEEE754_SingleConverter &) const
void vectorToCooked(const RAW_ITERATOR &raw_begin, const RAW_ITERATOR &raw_end, const COOKED_ITERATOR &cooked_begin) const