GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
byte-swap.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_byte_swap_h)
24 #define octave_byte_swap_h 1
25 
26 #include "octave-config.h"
27 
28 static inline void
29 swap_bytes (void *ptr, unsigned int i, unsigned int j)
30 {
31  char *t = static_cast<char *> (ptr);
32 
33  char tmp = t[i];
34  t[i] = t[j];
35  t[j] = tmp;
36 }
37 
38 template <int n>
39 void
40 swap_bytes (void *ptr)
41 {
42  for (int i = 0; i < n/2; i++)
43  swap_bytes (ptr, i, n-1-i);
44 }
45 
46 template <>
47 inline void
48 swap_bytes<1> (void *)
49 { }
50 
51 template <>
52 inline void
53 swap_bytes<2> (void *ptr)
54 {
55  swap_bytes (ptr, 0, 1);
56 }
57 
58 template <>
59 inline void
60 swap_bytes<4> (void *ptr)
61 {
62  swap_bytes (ptr, 0, 3);
63  swap_bytes (ptr, 1, 2);
64 }
65 
66 template <>
67 inline void
68 swap_bytes<8> (void *ptr)
69 {
70  swap_bytes (ptr, 0, 7);
71  swap_bytes (ptr, 1, 6);
72  swap_bytes (ptr, 2, 5);
73  swap_bytes (ptr, 3, 4);
74 }
75 
76 template <int n>
77 void
78 swap_bytes (void *ptr, int len)
79 {
80  char *t = static_cast<char *> (ptr);
81 
82  for (int i = 0; i < len; i++)
83  {
84  swap_bytes<n> (t);
85  t += n;
86  }
87 }
88 
89 template <>
90 inline void
91 swap_bytes<1> (void *, int)
92 { }
93 
94 #endif
static void swap_bytes(void *ptr, unsigned int i, unsigned int j)
Definition: byte-swap.h:29
void swap_bytes< 1 >(void *)
Definition: byte-swap.h:48
void swap_bytes< 8 >(void *ptr)
Definition: byte-swap.h:68
void swap_bytes< 2 >(void *ptr)
Definition: byte-swap.h:53
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:60
double tmp
Definition: data.cc:6252
for i
Definition: data.cc:5264