EMAN2
Static Public Member Functions | Static Private Attributes | List of all members
EMAN::ByteOrder Class Reference

ByteOrder defines functions to work on big/little endian byte orders. More...

#include <byteorder.h>

Static Public Member Functions

static bool is_host_big_endian ()
 
static bool is_float_big_endian (float small_number)
 given a small floating number, return whether the number is in big endian or not. More...
 
template<class T >
static bool is_data_big_endian (const T *small_num_addr)
 given a pointer to a reasonable small integer number, return whether the number is big endian or not. More...
 
template<class T >
static void become_big_endian (T *data, size_t n=1)
 convert data from host byte order to big endian order. More...
 
template<class T >
static void become_little_endian (T *data, size_t n=1)
 convert data from host byte order to little endian order. More...
 
template<class T >
static void swap_bytes (T *data, size_t n=1)
 swap the byte order of data with 'n' T-type elements. More...
 

Static Private Attributes

static bool is_host_endian_checked = false
 
static bool host_big_endian = false
 

Detailed Description

ByteOrder defines functions to work on big/little endian byte orders.

The byte order is the order in which bytes are stored to create larger data types such as the int and long values. Different kinds of computers use different byte order conventions.

There are 2 major byte orders: big-endian and little-endian.

big-endian (like SGI) store the most significant bytes (i.e. the bytes that hold the largest part of the value) first. little-endian (like x86) store the most significant byte last.

The host byte order is the byte order used on the current host.

ByteOrder class defines functions for checking running-host byte-order, checking data byte-order, convert data from one byte-order to another byte-order.

Definition at line 58 of file byteorder.h.

Member Function Documentation

◆ become_big_endian()

template<class T >
static void EMAN::ByteOrder::become_big_endian ( T *  data,
size_t  n = 1 
)
inlinestatic

◆ become_little_endian()

template<class T >
static void EMAN::ByteOrder::become_little_endian ( T *  data,
size_t  n = 1 
)
inlinestatic

convert data from host byte order to little endian order.

'n' is the number of elements of type T.

Definition at line 122 of file byteorder.h.

123 {
124 if (is_host_big_endian()) {
125 swap_bytes < T > (data, n);
126 }
127 }

References is_host_big_endian().

◆ is_data_big_endian()

template<class T >
static bool EMAN::ByteOrder::is_data_big_endian ( const T *  small_num_addr)
inlinestatic

given a pointer to a reasonable small integer number, return whether the number is big endian or not.

For a n-bit integer, the number should < (2 ^ (n/2)). e.g., for 'int', number should < 65535; for 'short', number should < 255.

Definition at line 76 of file byteorder.h.

77 {
78 if (!small_num_addr) {
79 return false;
80 }
81
82 bool data_big_endian = false;
83 size_t typesize = sizeof(T);
84 char *d = (char *) small_num_addr;
85
86 if (is_host_big_endian()) {
87 data_big_endian = false;
88 for (size_t i = typesize / 2; i < typesize; i++)
89 {
90 if (d[i] != 0) {
91 data_big_endian = true;
92 break;
93 }
94 }
95 }
96 else {
97 data_big_endian = true;
98 for (size_t j = 0; j < typesize / 2; j++) {
99 if (d[j] != 0) {
100 data_big_endian = false;
101 break;
102 }
103 }
104 }
105
106 return data_big_endian;
107 }

References is_host_big_endian().

Referenced by EMAN::DM4IO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::Df3IO::is_valid(), and EMAN::EmIO::is_valid().

◆ is_float_big_endian()

bool ByteOrder::is_float_big_endian ( float  small_number)
static

given a small floating number, return whether the number is in big endian or not.

If a number is smaller than 65535, it is defined as a "small" number here.

Definition at line 59 of file byteorder.cpp.

60{
61 bool is_big = false;
62
63 if (Util::goodf(&f) && f > 0 && f < 65535.0 && f == floor(f)) {
64 is_big = is_host_big_endian();
65 }
66 else {
67 is_big = !is_host_big_endian();
68 }
69
70 return is_big;
71}
static int goodf(const float *p_f)
Check whether a number is a good float.
Definition: util.h:1112

References EMAN::Util::goodf(), and is_host_big_endian().

Referenced by EMAN::SpiderIO::is_valid(), and EMAN::SingleSpiderIO::is_valid().

◆ is_host_big_endian()

bool ByteOrder::is_host_big_endian ( )
static

Definition at line 40 of file byteorder.cpp.

41{
43 int one = 1;
44 char *p_one = (char *) (&one);
45
46 if (p_one[0] == 1 && p_one[1] == 0 && p_one[2] == 0 && p_one[3] == 0) {
47 host_big_endian = false;
48 }
49 else {
50 host_big_endian = true;
51 }
52
54 }
55
56 return host_big_endian;
57}
static bool is_host_endian_checked
Definition: byteorder.h:149
static bool host_big_endian
Definition: byteorder.h:150

References host_big_endian, and is_host_endian_checked.

Referenced by become_big_endian(), EMAN::Gatan::TagTable::become_host_endian(), EMAN::GatanDM4::TagTable::become_host_endian(), EMAN::ImageIO::become_host_endian(), become_little_endian(), EMAN::DM3IO::DM3IO(), EMAN::DM4IO::DM4IO(), EMAN::EmIO::EmIO(), EMAN::FitsIO::FitsIO(), EMAN::Gatan2IO::Gatan2IO(), EMAN::MrcIO::generate_machine_stamp(), EMAN::IcosIO::IcosIO(), EMAN::ImagicIO::ImagicIO(), EMAN::ImagicIO2::ImagicIO2(), is_data_big_endian(), is_float_big_endian(), EMAN::DM4IO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::ImagicIO2::is_valid(), EMAN::PifIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::SingleSpiderIO::is_valid(), EMAN::EmIO::is_valid(), EMAN::OmapIO::is_valid(), EMAN::LstFastIO::LstFastIO(), EMAN::LstIO::LstIO(), EMAN::ImagicIO2::make_header_host_endian(), EMAN::ImagicIO::make_header_host_endian(), EMAN::MrcIO::MrcIO(), EMAN::SpiderIO::need_swap(), EMAN::OmapIO::OmapIO(), EMAN::PifIO::PifIO(), EMAN::Util::recv_broadcast(), EMAN::EMData::save_byteorder_to_dict(), EMAN::VtkIO::VtkIO(), EMAN::XplorIO::XplorIO(), and EMAN::XYZIO::XYZIO().

◆ swap_bytes()

template<class T >
static void EMAN::ByteOrder::swap_bytes ( T *  data,
size_t  n = 1 
)
inlinestatic

Member Data Documentation

◆ host_big_endian

bool ByteOrder::host_big_endian = false
staticprivate

Definition at line 150 of file byteorder.h.

Referenced by is_host_big_endian().

◆ is_host_endian_checked

bool ByteOrder::is_host_endian_checked = false
staticprivate

Definition at line 149 of file byteorder.h.

Referenced by is_host_big_endian().


The documentation for this class was generated from the following files: