1960
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1960
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1960
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_byte_swap_h) |
|
25 #define octave_byte_swap_h 1 |
|
26 |
3215
|
27 // XXX FIXME XXX -- not sure these volatile qualifiers are really |
|
28 // needed or appropriate here. |
|
29 |
1960
|
30 static inline void |
3215
|
31 swap_bytes (volatile void *ptr, unsigned int i, unsigned int j) |
1960
|
32 { |
3215
|
33 volatile char *t = static_cast<volatile char *> (ptr); |
3145
|
34 |
1960
|
35 char tmp = t[i]; |
|
36 t[i] = t[j]; |
|
37 t[j] = tmp; |
|
38 } |
|
39 |
4944
|
40 template <int n> |
|
41 void |
|
42 swap_bytes (volatile void *ptr) |
1960
|
43 { |
4944
|
44 for (size_t i = 0; i < n/2; i++) |
|
45 swap_bytes (ptr, i, n-1-i); |
|
46 } |
3145
|
47 |
4944
|
48 template <> |
|
49 inline void |
|
50 swap_bytes <1> (volatile void *) |
|
51 { |
|
52 } |
|
53 |
|
54 template <> |
|
55 inline void |
|
56 swap_bytes <2> (volatile void *ptr) |
|
57 { |
|
58 swap_bytes (ptr, 0, 1); |
1960
|
59 } |
|
60 |
4944
|
61 template <> |
|
62 inline void |
|
63 swap_bytes <4> (volatile void *ptr) |
1960
|
64 { |
4944
|
65 swap_bytes (ptr, 0, 3); |
|
66 swap_bytes (ptr, 1, 2); |
1960
|
67 } |
|
68 |
4944
|
69 template <> |
|
70 inline void |
|
71 swap_bytes <8> (volatile void *ptr) |
1960
|
72 { |
4944
|
73 swap_bytes (ptr, 0, 7); |
|
74 swap_bytes (ptr, 1, 6); |
|
75 swap_bytes (ptr, 2, 5); |
|
76 swap_bytes (ptr, 3, 4); |
1960
|
77 } |
|
78 |
4944
|
79 template <int n> |
|
80 void |
|
81 swap_bytes (volatile void *ptr, int len) |
1960
|
82 { |
3215
|
83 volatile char *t = static_cast<volatile char *> (ptr); |
3145
|
84 |
1960
|
85 for (int i = 0; i < len; i++) |
|
86 { |
4944
|
87 swap_bytes<n> (t); |
|
88 t += n; |
1960
|
89 } |
|
90 } |
|
91 |
4944
|
92 template <> |
|
93 inline void |
|
94 swap_bytes<1> (volatile void *, int) |
1960
|
95 { |
|
96 } |
|
97 |
|
98 #endif |
|
99 |
|
100 /* |
|
101 ;;; Local Variables: *** |
|
102 ;;; mode: C++ *** |
|
103 ;;; End: *** |
|
104 */ |