Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/swapbytes.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | df437a52bcaf |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19401
diff
changeset
|
1 ## Copyright (C) 2007-2015 David Bateman |
6869 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6869 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6869 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} swapbytes (@var{x}) | |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
21 ## Swap the byte order on values, converting from little endian to big endian |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
22 ## and vice versa. |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
24 ## For example: |
6869 | 25 ## |
26 ## @example | |
27 ## @group | |
28 ## swapbytes (uint16 (1:4)) | |
29 ## @result{} [ 256 512 768 1024] | |
30 ## @end group | |
31 ## @end example | |
32 ## | |
33 ## @seealso{typecast, cast} | |
34 ## @end deftypefn | |
35 | |
36 function y = swapbytes (x) | |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
37 |
6869 | 38 if (nargin != 1) |
39 print_usage (); | |
40 endif | |
41 | |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 cls = class (x); |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 if (strcmp (cls, "int8") || strcmp (cls, "uint8") || isempty (x)) |
6869 | 44 y = x; |
45 else | |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
46 if (strcmp (cls, "int16") || strcmp (cls, "uint16")) |
6869 | 47 nb = 2; |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 elseif (strcmp (cls, "single") |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 || strcmp (cls, "int32") || strcmp (cls, "uint32")) |
6869 | 50 nb = 4; |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 elseif (strcmp (cls, "double") |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
52 || strcmp (cls, "int64") || strcmp (cls, "uint64")) |
6869 | 53 nb = 8; |
54 else | |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 error ("swapbytes: invalid object of class '%s'", cls); |
6869 | 56 endif |
57 y = reshape (typecast (reshape (typecast (x(:), "uint8"), nb, numel (x)) | |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 ([nb : -1 : 1], :) (:), cls), size (x)); |
6869 | 59 endif |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 |
6869 | 61 endfunction |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 |
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
63 |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 %!assert (swapbytes (uint16 (1:4)), uint16 ([256 512 768 1024])) |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
65 %!test |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
66 %! assert (swapbytes (swapbytes (pi)), pi); |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
67 %! assert (swapbytes (swapbytes (single (pi))), single (pi)); |
12847
619c1895e3e0
codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 |
19401
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 ## Test input validation |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 %!error swapbytes () |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 %!error swapbytes (1, 2) |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 %!error <invalid object of class 'cell'> swapbytes ({1}) |
827ac8b5ae07
swapbytes.m: Add support for 'single' type.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
73 |