GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
byte-swap.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
31static inline void
32swap_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
41template <int n>
42void
43swap_bytes (void *ptr)
44{
45 for (int i = 0; i < n/2; i++)
46 swap_bytes (ptr, i, n-1-i);
47}
48
49template <>
50inline void
51swap_bytes<1> (void *)
52{ }
53
54template <>
55inline void
56swap_bytes<2> (void *ptr)
57{
58 swap_bytes (ptr, 0, 1);
59}
60
61template <>
62inline void
63swap_bytes<4> (void *ptr)
64{
65 swap_bytes (ptr, 0, 3);
66 swap_bytes (ptr, 1, 2);
67}
68
69template <>
70inline void
71swap_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
79template <int n>
80void
81swap_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
92template <>
93inline void
94swap_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< 1 >(void *)
Definition byte-swap.h:51
F77_RET_T len
Definition xerbla.cc:61