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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
2 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
3 ## This file is part of Octave.
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
4 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6869
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6869
diff changeset
8 ## your option) any later version.
6869
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
9 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
13 ## General Public License for more details.
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
14 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6869
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6869
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6869
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
18
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
19 ## -*- texinfo -*-
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
25 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
26 ## @example
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
27 ## @group
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
28 ## swapbytes (uint16 (1:4))
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
29 ## @result{} [ 256 512 768 1024]
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
30 ## @end group
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
31 ## @end example
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
32 ##
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
33 ## @seealso{typecast, cast}
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
34 ## @end deftypefn
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
35
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
36 function y = swapbytes (x)
12847
619c1895e3e0 codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
37
6869
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
38 if (nargin != 1)
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
39 print_usage ();
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
40 endif
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
44 y = x;
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
53 nb = 8;
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
56 endif
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
59 endif
12847
619c1895e3e0 codesprint: Add tests for swapbytes.m
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
60
6869
f9c893831e68 [project @ 2007-09-06 16:38:44 by dbateman]
dbateman
parents:
diff changeset
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