GNU Octave  9.1.0
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-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_byte_swap_h)
27 #define octave_byte_swap_h 1
28 
29 #include "octave-config.h"
30 
31 static inline void
32 swap_bytes (void *ptr, unsigned int i, unsigned int j)
33 {
34  char *t = static_cast<char *> (ptr);
35 
36  char tmp = t[i];
37  t[i] = t[j];
38  t[j] = tmp;
39 }
40 
41 template <int n>
42 void
43 swap_bytes (void *ptr)
44 {
45  for (int i = 0; i < n/2; i++)
46  swap_bytes (ptr, i, n-1-i);
47 }
48 
49 template <>
50 inline void
51 swap_bytes<1> (void *)
52 { }
53 
54 template <>
55 inline void
56 swap_bytes<2> (void *ptr)
57 {
58  swap_bytes (ptr, 0, 1);
59 }
60 
61 template <>
62 inline void
63 swap_bytes<4> (void *ptr)
64 {
65  swap_bytes (ptr, 0, 3);
66  swap_bytes (ptr, 1, 2);
67 }
68 
69 template <>
70 inline void
71 swap_bytes<8> (void *ptr)
72 {
73  swap_bytes (ptr, 0, 7);
74  swap_bytes (ptr, 1, 6);
75  swap_bytes (ptr, 2, 5);
76  swap_bytes (ptr, 3, 4);
77 }
78 
79 template <int n>
80 void
81 swap_bytes (void *ptr, int len)
82 {
83  char *t = static_cast<char *> (ptr);
84 
85  for (int i = 0; i < len; i++)
86  {
87  swap_bytes<n> (t);
88  t += n;
89  }
90 }
91 
92 template <>
93 inline void
94 swap_bytes<1> (void *, int)
95 { }
96 
97 #endif
void swap_bytes< 2 >(void *ptr)
Definition: byte-swap.h:56
void swap_bytes< 8 >(void *ptr)
Definition: byte-swap.h:71
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:63
void swap_bytes(void *ptr)
Definition: byte-swap.h:43
void swap_bytes< 1 >(void *)
Definition: byte-swap.h:51
octave_idx_type n
Definition: mx-inlines.cc:761
F77_RET_T len
Definition: xerbla.cc:61