Block-Structured AMR Software Framework
AMReX_IntConv.H
Go to the documentation of this file.
1 #ifndef AMREX_INTCONV_H_
2 #define AMREX_INTCONV_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_FPC.H>
6 #include <AMReX_FabConv.H>
7 
8 #include <iostream>
9 #include <cstring>
10 #include <cstdint>
11 
12 namespace amrex {
13 
14  std::int16_t swapBytes (std::int16_t value);
15  std::int32_t swapBytes (std::int32_t value);
16  std::int64_t swapBytes (std::int64_t value);
17 
18  std::uint16_t swapBytes (std::uint16_t value);
19  std::uint32_t swapBytes (std::uint32_t value);
20  std::uint64_t swapBytes (std::uint64_t value);
21 
22  template <typename To, typename From>
23  void writeIntData (const From* data, std::size_t size, std::ostream& os,
24  const amrex::IntDescriptor& id)
25  {
26  To value;
27  bool swapEndian = (id.order() != amrex::FPC::NativeIntDescriptor().order());
28  for (std::size_t j = 0; j < size; ++j) {
29  value = static_cast<To>(data[j]);
30  if (swapEndian) { value = swapBytes(value); }
31  os.write((char*) &value, sizeof(To));
32  }
33  }
34 
35  template <typename To, typename From>
36  void readIntData (To* data, std::size_t size, std::istream& is,
37  const amrex::IntDescriptor& id)
38  {
39  From value;
40  bool swapEndian = (id.order() != amrex::FPC::NativeIntDescriptor().order());
41  for (std::size_t j = 0; j < size; ++j) {
42  is.read((char*) &value, sizeof(From));
43  if (swapEndian) { value = swapBytes(value); }
44  data[j] = static_cast<To>(value);
45  }
46  }
47 }
48 
49 #endif
static const IntDescriptor & NativeIntDescriptor()
Returns a constant reference to an IntDescriptor describing the native "int" under which AMReX was co...
Definition: AMReX_FPC.cpp:76
A Descriptor of the Long Integer type.
Definition: AMReX_FabConv.H:29
Ordering order() const
Returns the ordering.
Definition: AMReX_FabConv.cpp:26
AMREX_GPU_HOST_DEVICE Long size(T const &b) noexcept
integer version
Definition: AMReX_GpuRange.H:26
Definition: AMReX_Amr.cpp:49
void writeIntData(const From *data, std::size_t size, std::ostream &os, const amrex::IntDescriptor &id)
Definition: AMReX_IntConv.H:23
std::int16_t swapBytes(std::int16_t val)
Definition: AMReX_IntConv.cpp:5
void readIntData(To *data, std::size_t size, std::istream &is, const amrex::IntDescriptor &id)
Definition: AMReX_IntConv.H:36