NPL
Neurological Programs and Libraries
byteswap.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file byteswap.h
9  *
10  *****************************************************************************/
11 
12 #ifndef BYTESWAP_H
13 #define BYTESWAP_H
14 
15 #include <stdexcept>
16 #include "npltypes.h"
17 
18 namespace npl {
19 
20 template <typename T>
21 union Bytes
22 {
23  T iv;
24  unsigned char bytes[sizeof(T)];
25 } __attribute__((packed));
26 
27 inline
29 {
30  Bytes<int32_t> tmp;
31  tmp.iv = 1;
32 
33  if(tmp.bytes[0] == 1)
34  return true;
35  else
36  return false;
37 }
38 
39 inline
40 bool big_endian()
41 {
42  Bytes<int32_t> tmp;
43  tmp.iv = 1;
44 
45  if(tmp.bytes[3] == 1)
46  return true;
47  else
48  return false;
49 }
50 
51 template <typename T>
52 void swap(T* val)
53 {
54  Bytes<T> tmp;
55  tmp.iv = *val;
56  for(size_t ii=0; ii<sizeof(T)/2; ii++)
57  std::swap(tmp.bytes[sizeof(T)-ii-1], tmp.bytes[ii]);
58  *val = tmp.iv;
59 }
60 
61 template <>
62 inline
63 void swap<signed char>(signed char* val)
64 {
65  (void)val;
66 }
67 
68 
69 template <>
70 inline
71 void swap<char>(char* val)
72 {
73  (void)val;
74 }
75 
76 template <>
77 inline
78 void swap<uint8_t>(uint8_t* val)
79 {
80  (void)val;
81 }
82 
83 // swap the real and imaginary parts individualy
84 template <>
85 inline
87 {
88  Bytes<long double> real;
89  Bytes<long double> imag;
90 
91  real.iv = val->real();
92  imag.iv = val->imag();
93 
94  for(size_t ii=0; ii<sizeof(long double)/2; ii++) {
95  std::swap(real.bytes[sizeof(long double)-ii-1], real.bytes[ii]);
96  std::swap(imag.bytes[sizeof(long double)-ii-1], imag.bytes[ii]);
97  }
98 
99  val->real(real.iv);
100  val->imag(imag.iv);
101 }
102 
103 // swap the real and imaginary parts individualy
104 template <>
105 inline
107 {
108  Bytes<double> real;
109  Bytes<double> imag;
110 
111  real.iv = val->real();
112  imag.iv = val->imag();
113 
114  for(size_t ii=0; ii<sizeof(double)/2; ii++) {
115  std::swap(real.bytes[sizeof(double)-ii-1], real.bytes[ii]);
116  std::swap(imag.bytes[sizeof(double)-ii-1], imag.bytes[ii]);
117  }
118 
119  val->real(real.iv);
120  val->imag(imag.iv);
121 }
122 
123 // swap the real and imaginary parts individualy
124 template <>
125 inline
127 {
128  Bytes<float> real;
129  Bytes<float> imag;
130 
131  real.iv = val->real();
132  imag.iv = val->imag();
133 
134  for(size_t ii=0; ii<sizeof(float)/2; ii++) {
135  std::swap(real.bytes[sizeof(float)-ii-1], real.bytes[ii]);
136  std::swap(imag.bytes[sizeof(float)-ii-1], imag.bytes[ii]);
137  }
138 
139  val->real(real.iv);
140  val->imag(imag.iv);
141 }
142 
143 // do no byte swapping
144 template <>
145 inline
147 {
148  (void)val;
149 }
150 
151 template <>
152 inline
153 void swap<rgb_t>(rgb_t* val)
154 {
155  (void)val;
156 }
157 
158 } // npl
159 
160 #endif //BYTESWAP_H
void swap< cquad_t >(cquad_t *val)
Definition: byteswap.h:86
Definition: accessors.h:29
void swap< signed char >(signed char *val)
Definition: byteswap.h:63
void swap(T *val)
Definition: byteswap.h:52
class npl::MatMap __attribute__
void swap< rgb_t >(rgb_t *val)
Definition: byteswap.h:153
void swap< uint8_t >(uint8_t *val)
Definition: byteswap.h:78
void swap< char >(char *val)
Definition: byteswap.h:71
void swap< cfloat_t >(cfloat_t *val)
Definition: byteswap.h:126
void swap< rgba_t >(rgba_t *val)
Definition: byteswap.h:146
void swap< cdouble_t >(cdouble_t *val)
Definition: byteswap.h:106
bool little_endian()
Definition: byteswap.h:28
unsigned char bytes[sizeof(T)]
Definition: byteswap.h:24
bool big_endian()
Definition: byteswap.h:40