GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
byte-swap.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_byte_swap_h)
24 #define octave_byte_swap_h 1
25 
26 static inline void
27 swap_bytes (void *ptr, unsigned int i, unsigned int j)
28 {
29  char *t = static_cast<char *> (ptr);
30 
31  char tmp = t[i];
32  t[i] = t[j];
33  t[j] = tmp;
34 }
35 
36 template <int n>
37 void
38 swap_bytes (void *ptr)
39 {
40  for (int i = 0; i < n/2; i++)
41  swap_bytes (ptr, i, n-1-i);
42 }
43 
44 template <>
45 inline void
46 swap_bytes<1> (void *)
47 {
48 }
49 
50 template <>
51 inline void
52 swap_bytes<2> (void *ptr)
53 {
54  swap_bytes (ptr, 0, 1);
55 }
56 
57 template <>
58 inline void
59 swap_bytes<4> (void *ptr)
60 {
61  swap_bytes (ptr, 0, 3);
62  swap_bytes (ptr, 1, 2);
63 }
64 
65 template <>
66 inline void
67 swap_bytes<8> (void *ptr)
68 {
69  swap_bytes (ptr, 0, 7);
70  swap_bytes (ptr, 1, 6);
71  swap_bytes (ptr, 2, 5);
72  swap_bytes (ptr, 3, 4);
73 }
74 
75 template <int n>
76 void
77 swap_bytes (void *ptr, int len)
78 {
79  char *t = static_cast<char *> (ptr);
80 
81  for (int i = 0; i < len; i++)
82  {
83  swap_bytes<n> (t);
84  t += n;
85  }
86 }
87 
88 template <>
89 inline void
90 swap_bytes<1> (void *, int)
91 {
92 }
93 
94 #endif
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:27
void swap_bytes< 1 >(void *)
Definition: byte-swap.h:46
void swap_bytes< 8 >(void *ptr)
Definition: byte-swap.h:67
void swap_bytes< 2 >(void *ptr)
Definition: byte-swap.h:52
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:59