Mercurial > hg > octave-nkf
changeset 4950:0896307b1b48
[project @ 2004-08-31 21:26:35 by jwe]
author | jwe |
---|---|
date | Tue, 31 Aug 2004 21:26:35 +0000 |
parents | f6b63ff1119b |
children | 0a21e1bf18c4 |
files | scripts/ChangeLog scripts/general/bitcmp.m scripts/general/bitget.m scripts/general/bitset.m scripts/general/isa.m |
diffstat | 5 files changed, 145 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,6 +1,17 @@ -2004-08-31 2004-08-30 David Bateman <dbateman@free.fr> - - * (general/repmat.m): Adapt to allow integer types. +2004-08-31 John W. Eaton <jwe@octave.org> + + * general/isa.m: New function, from Octave-forge. + +2004-08-31 David Bateman <dbateman@free.fr> + + * general/bitcmp.m, general/bitget.m, general/bitset.m: Remove + limitation on the use of int64 and uint64 types, and the use + of the eval. + + * general/bitset.m: Remove superfluous cast to return type, as bug + in .^ with integer types is fixed. + + * general/repmat.m: Adapt to allow integer types. 2004-08-31 Paul Kienzle <pkienzle@users.sf.net>
--- a/scripts/general/bitcmp.m +++ b/scripts/general/bitcmp.m @@ -39,25 +39,47 @@ usage ("bitcmp (A, n)"); endif - cname = class (A); - if (strcmp (cname, "double")) + if (isa (A, "double")) Bmax = bitmax; Amax = log2 (Bmax) + 1; - elseif strcmp ("uint", substr (cname, 1, 4)) - Bmax = intmax (cname); - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 1);"]); + _conv = @double; else - Bmax = eval ([cname, " (-1);"]); - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 2);"]); + if (isa (A, "uint8")) + Amax = 8; + _conv = @uint8; + elseif (isa (A, "uint16")) + Amax = 16; + _conv = @uint16; + elseif (isa (A, "uint32")) + Amax = 32; + _conv = @uint32; + elseif (isa (A, "uint64")) + Amax = 64; + _conv = @uint64; + elseif (isa (A, "int8")) + Amax = 8; + _conv = @int8; + elseif (isa (A, "int16")) + Amax = 16; + _conv = @int16; + elseif (isa (A, "int32")) + Amax = 32; + _conv = @int32; + elseif (isa (A, "int64")) + Amax = 64; + _conv = @int64; + else + error ("invalid class %s", class (A)); + endif + Bmax = intmax (class (A)); endif - Aone = eval ([ cname, "(1);"]); if (nargin == 2) - m = eval ([cname, " (n(:));"]); - if (any (m < Aone) || any (m > Amax)) + m = double (n(:)); + if (any (m < 1) || any (m > Amax)) error ("n must be in the range [1,%d]", Amax); endif - X = bitxor (A, bitshift (Bmax, -n)); + X = bitxor (A, bitshift (Bmax, -int8(n))); else X = bitxor (A, Bmax); endif
--- a/scripts/general/bitget.m +++ b/scripts/general/bitget.m @@ -34,21 +34,43 @@ usage ("bitget (A, n)"); endif - cname = class (A); - if (strcmp (cname, "double")) - Amax = log2 (bitmax) + 1; - elseif strcmp ("uint", substr (cname, 1, 4)) - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 1);"]); + if (isa (A, "double")) + Amax = log2 (Bmax) + 1; + _conv = @double; else - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 2);"]); + if (isa (A, "uint8")) + Amax = 8; + _conv = @uint8; + elseif (isa (A, "uint16")) + Amax = 16; + _conv = @uint16; + elseif (isa (A, "uint32")) + Amax = 32; + _conv = @uint32; + elseif (isa (A, "uint64")) + Amax = 64; + _conv = @uint64; + elseif (isa (A, "int8")) + Amax = 8; + _conv = @int8; + elseif (isa (A, "int16")) + Amax = 16; + _conv = @int16; + elseif (isa (A, "int32")) + Amax = 32; + _conv = @int32; + elseif (isa (A, "int64")) + Amax = 64; + _conv = @int64; + else + error ("invalid class %s", class (A)); + endif endif - Aone = eval ([ cname, "(1);"]); - m = eval ([cname, " (n(:));"]); - if (any (m < Aone) || any (m > Amax)) + m = double (n(:)); + if (any (m < 1) || any (m > Amax)) error ("n must be in the range [1,%d]", Amax); endif - X = eval (["bitand (A, ", cname, " (2) .^ (", cname, " (n) - Aone)) != ", cname, "(0);"]); - + X = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0); endfunction
--- a/scripts/general/bitset.m +++ b/scripts/general/bitset.m @@ -41,29 +41,51 @@ value = 1; endif - cname = class (A); - if (strcmp (cname, "double")) + if (isa (A, "double")) Bmax = bitmax; Amax = log2 (Bmax) + 1; - elseif strcmp("uint", substr (cname, 1, 4)) - Bmax = intmax (cname); - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 1);"]); + _conv = @double; else - Bmax = eval ([cname, " (-1);"]); - Amax = eval ([cname, " (log2 (double (intmax (cname))) + 2);"]); + if (isa (A, "uint8")) + Amax = 8; + _conv = @uint8; + elseif (isa (A, "uint16")) + Amax = 16; + _conv = @uint16; + elseif (isa (A, "uint32")) + Amax = 32; + _conv = @uint32; + elseif (isa (A, "uint64")) + Amax = 64; + _conv = @uint64; + elseif (isa (A, "int8")) + Amax = 8; + _conv = @int8; + elseif (isa (A, "int16")) + Amax = 16; + _conv = @int16; + elseif (isa (A, "int32")) + Amax = 32; + _conv = @int32; + elseif (isa (A, "int64")) + Amax = 64; + _conv = @int64; + else + error ("invalid class %s", class (A)); + endif + Bmax = intmax (class (A)); endif - Aone = eval ([ cname, "(1);"]); - m = eval ([cname, " (n(:));"]); - if (any (m < Aone) || any (m > Amax)) + m = double (n(:)); + if (any (m < 1) || any (m > Amax)) error ("n must be in the range [1,%d]", Amax); endif - ## XXX FIXME XXX Need extra cast to cname due to bad return value of .^ - X = eval (["bitand (A, Bmax - ", cname, " (", cname, " (2) .^ (", cname, " (n) - Aone)));"]); + mask = bitshift (_conv (1), uint8 (n) - uint8 (1)); + X = bitxor (A, bitand (A, mask)); if (value) - X = eval (["bitor (A, ", cname, " (2) .^ (", cname, " (n) - Aone));"]); + X = bitor (A, mask); endif endfunction
new file mode 100644 --- /dev/null +++ b/scripts/general/isa.m @@ -0,0 +1,30 @@ +## Copyright (C) 2004 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2, or (at your option) +## any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place - Suite 330, Boston, MA +## 02111-1307, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} isa (@var{x}, @var{class}) +## Return true if @var{x} is a value from the class @var{class}. +## @end deftypefn + +## Author: Paul Kienzle <pkienzle@users.sf.net> +## Adapted-by: jwe + +function retval = isa (x, cname) + retval = strcmp (class (f), cname); +endfunction