annotate scripts/general/bitset.m @ 20654: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 4bb41929286b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
1 ## Copyright (C) 2004-2015 David Bateman
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
2 ## Copyright (C) 2012 Jordi GutiƩrrez Hermoso
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
3 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
4 ## This file is part of Octave.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
5 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
14 ## General Public License for more details.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
15 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
16 ## 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: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
19
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
21 ## @deftypefn {Function File} {@var{C} =} bitset (@var{A}, @var{n})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
22 ## @deftypefnx {Function File} {@var{C} =} bitset (@var{A}, @var{n}, @var{val})
20193
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
23 ## Set or reset bit(s) @var{n} of the unsigned integers in @var{A}.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
24 ##
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
25 ## @var{val} = 0 resets and @var{val} = 1 sets the bits.
20193
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
26 ## The least significant bit is @var{n} = 1. All variables must be the same
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
27 ## size or scalars.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
28 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
29 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7017
diff changeset
30 ## @group
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4916
diff changeset
31 ## dec2bin (bitset (10, 1))
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
32 ## @result{} 1011
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7017
diff changeset
33 ## @end group
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
34 ## @end example
20521
4bb41929286b Deprecate bitmax.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20193
diff changeset
35 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, intmax, flintmax}
5053
c08cb1098afc [project @ 2004-10-19 23:10:54 by jwe]
jwe
parents: 4950
diff changeset
36 ## @end deftypefn
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
37
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
38 function C = bitset (A, n, val)
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4916
diff changeset
39
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
40 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
41 print_usage ();
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
42 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
43
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
44 if (any (A(:) < 0))
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
45 error ("bitset: A must be >= 0");
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
46 endif
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
47
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
48 sz = size (A);
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
49
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
50 if (nargin == 2)
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
51 val = true (sz);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
52 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
53
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
54 cl = class (A);
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
55
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
56 if (isfloat (A) && isreal (A))
20521
4bb41929286b Deprecate bitmax.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20193
diff changeset
57 Bmax = flintmax (cl);
17391
7d8d194f3f63 bitget.m, bitset.m: Make max bit index consistent
Mike Miller <mtmiller@ieee.org>
parents: 14643
diff changeset
58 Amax = ceil (log2 (Bmax));
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
59 elseif (isinteger (A))
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
60 Bmax = intmax (cl);
17391
7d8d194f3f63 bitget.m, bitset.m: Make max bit index consistent
Mike Miller <mtmiller@ieee.org>
parents: 14643
diff changeset
61 Amax = ceil (log2 (Bmax));
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
62 else
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
63 error ("bitset: invalid class %s", cl);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
64 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
65
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
66 if (any ((n < 1)(:)) || any ((n > Amax)(:)))
12480
139f993936af Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
67 error ("bitset: N must be in the range [1,%d]", Amax);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
68 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
69
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
70 mask = bitshift (cast (1, cl), uint8 (n) - uint8 (1));
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
71
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
72 on = logical (val);
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
73 off = !on;
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
74
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
75 if (isscalar (mask))
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
76 onmask = mask;
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
77 offmask = mask;
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
78 else
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
79 if (! size_equal (A, n))
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
80 error ("bitset: N must be scalar or the same size as A");
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
81 endif
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
82 onmask = mask(on);
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
83 offmask = mask(off);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
84 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
85
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
86 C = zeros (sz, cl);
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
87 C(on) = bitor (A(on), onmask);
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
88 C(off) = bitand (A(off), bitcmp (offmask));
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
89
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
90 endfunction
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
91
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
92
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
93 %!test
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
94 %! assert (bitset ([0, 10], [3, 3]), [4, 14]);
17392
e09cd91168d1 Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents: 17391
diff changeset
95 %! assert (bitset (single ([0, 10]), [3, 3]), single ([4, 14]));
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
96 %! pfx = {"", "u"};
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
97 %! for i = 1:2
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
98 %! for prec = [8, 16, 32, 64]
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
99 %! fcn = str2func (sprintf ("%sint%d", pfx{i}, prec));
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
100 %! assert (bitset (fcn ([0, 10]), [3, 3]), fcn ([4, 14]));
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
101 %! endfor
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
102 %! endfor
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
103
14633
056ea51070b8 Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14363
diff changeset
104 ## Bug #36458
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
105 %!assert (bitset (uint8 ([1, 2;3 4]), 1, [0 1; 0 1]), uint8 ([0, 3; 2 5]))
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
106
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
107 %!error bitset (1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
108 %!error bitset (1, 2, 3, 4)
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
109 %!error <A must be .= 0> bitset (-1, 2)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
110 %!error <invalid class char> bitset ("1", 2)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
111 %!error <N must be in the range \[1,53\]> bitset (0, 0)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
112 %!error <N must be in the range \[1,53\]> bitset (0, 55)
17392
e09cd91168d1 Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents: 17391
diff changeset
113 %!error <N must be in the range \[1,24\]> bitset (single (0), 0)
e09cd91168d1 Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents: 17391
diff changeset
114 %!error <N must be in the range \[1,24\]> bitset (single (0), 26)
14643
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
115 %!error <N must be in the range \[1,8\]> bitset (uint8 (0), 0)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
116 %!error <N must be in the range \[1,8\]> bitset (uint8 (0), 9)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
117 %!error <N must be in the range \[1,7\]> bitset (int8 (0), 9)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
118 %!error <N must be in the range \[1,15\]> bitset (int16 (0), 17)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
119 %!error <N must be in the range \[1,16\]> bitset (uint16 (0), 17)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
120 %!error <N must be in the range \[1,31\]> bitset (int32 (0), 33)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
121 %!error <N must be in the range \[1,32\]> bitset (uint32 (0), 33)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
122 %!error <N must be in the range \[1,63\]> bitset (int64 (0), 65)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
123 %!error <N must be in the range \[1,64\]> bitset (uint64 (0), 65)
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
124 %!error <N must be scalar or the same size as A> bitset (uint8 (1), [1 3])
d4b4080faa47 bitset.m: Update function to check for negative inputs.
Rik <octave@nomad.inbox5.com>
parents: 14639
diff changeset
125 %!error <N must be scalar or the same size as A> bitset (uint8 (1:3), [1 3])
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12480
diff changeset
126