Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
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
12namespace 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 if (!os.write(reinterpret_cast<char const*>(&value), sizeof(To))) {
32 amrex::Error("Failed to write integer data.");
33 }
34 }
35 }
36
37 template <typename To, typename From>
38 void readIntData (To* data, std::size_t size, std::istream& is,
39 const amrex::IntDescriptor& id)
40 {
41 From value;
42 bool swapEndian = (id.order() != amrex::FPC::NativeIntDescriptor().order());
43 for (std::size_t j = 0; j < size; ++j) {
44 if (!is.read(reinterpret_cast<char*>(&value), sizeof(From))) {
45 amrex::Error("Failed to read integer data.");
46 }
47 if (swapEndian) { value = swapBytes(value); }
48 data[j] = static_cast<To>(value);
49 }
50 }
51}
52
53#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
Definition AMReX_Amr.cpp:50
void writeIntData(const From *data, std::size_t size, std::ostream &os, const amrex::IntDescriptor &id)
Definition AMReX_IntConv.H:23
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:235
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:38