Mercurial > hg > octave-nkf
annotate scripts/general/bitset.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 | 4bb41929286b |
children |
rev | line source |
---|---|
19898
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 | 3 ## |
7016 | 4 ## This file is part of Octave. |
4916 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
4916 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
4916 | 19 |
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}) |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
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:
19898
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. |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
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:
19898
diff
changeset
|
27 ## size or scalars. |
4916 | 28 ## |
29 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
30 ## @group |
4920 | 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 | 34 ## @end example |
20696 | 35 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, intmax, flintmax} |
5053 | 36 ## @end deftypefn |
4916 | 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 | 39 |
4916 | 40 if (nargin < 2 || nargin > 3) |
6046 | 41 print_usage (); |
4916 | 42 endif |
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 | 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 | 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)) |
20696 | 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 | 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 | 64 endif |
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 | 68 endif |
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 | 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 | 84 endif |
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 | 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 |