[67a5bd]: / bitand.cc  Maximize  Restore  History

Download this file

188 lines (168 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (C) 2000 Kai Habel
**
** This program 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 of the License, or
** (at your option) any later version.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
*/
/*
INSTALLATION
- copy this file and the Makefile to directory of octave's LOADPATH
- compile this file:
make
*/
#include <sys/types.h>
#include <strstream>
#include <climits>
#include <algorithm>
#include <octave/oct.h>
#include <octave/lo-ieee.h>
typedef unsigned long bitop_int;
const unsigned int ULONG_SIZE=CHAR_BIT*sizeof(bitop_int);
const unsigned int BIT_AND = 1;
const unsigned int BIT_OR = 2;
const unsigned int BIT_XOR = 3;
double
scalar_bitop(double x,double y,unsigned int op) {
double a=octave_NaN;
if ((x>=0)&&(x<=ULONG_MAX)&&(y>=0)&&(y<=ULONG_MAX)) {
bitop_int xval=static_cast<bitop_int>( floor(x) );
bitop_int yval=static_cast<bitop_int>( floor(y) );
if (op ==BIT_AND)
a = static_cast<double>(xval & yval);
else if (op==BIT_OR)
a = static_cast<double>(xval | yval);
else if (op==BIT_XOR)
a = static_cast<double>(xval ^ yval);
}
return(a);
}
octave_value_list
bitop(Matrix xmat,Matrix ymat,unsigned int op) {
octave_value_list retval;
bool is_scalar_op=false,is_matrix_op=false;
unsigned int xr=xmat.rows();
unsigned int yr=ymat.rows();
unsigned int xc=xmat.columns();
unsigned int yc=ymat.columns();
if ( (xr*xc)==1 || (yr*yc)==1)
is_scalar_op=true;
if ( (xr==yr)&&(xc==yc) )
is_matrix_op=true;
if (is_matrix_op || is_scalar_op) {
unsigned int i,j,k,l;
unsigned int r=max(xr,yr),c=max(xc,yc);
Matrix a(r,c);
for(i=0;i<xr;i++) {
for(j=0;j<xc;j++) {
if (is_scalar_op) {
for(k=0;k<yr;k++) {
for(l=0;l<yc;l++) {
a(i+k,j+l)=scalar_bitop( xmat(i,j),ymat(k,l),op );
}
}
}
else {
// is_matrix_op
a(i,j)=scalar_bitop( xmat(i,j),ymat(i,j),op );
}
}
}
retval(0)=a;
}
else
error("size of x and y must match, or one operand must be a scalar");
return(retval);
}
DEFUN_DLD (bitand, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{A} =} bitand (@var{x}, @var{y})\n\
calculates the bitwise AND of nonnegative integers.\n\
@var{x},@var{y} must be in range [0..bitmax]\n\
@seealso{bitor,bitxor,bitset,bitget,bitcmp,bitshift,bitmax}\n\
@end deftypefn")
{
octave_value_list retval;
int nargin = args.length();
if (!(nargin==2)) {
print_usage ("bitand");
return retval;
}
if (args(0).is_real_type()&&args(1).is_real_type()) {
Matrix x = args(0).matrix_value();
Matrix y = args(1).matrix_value();
retval=bitop(x,y,BIT_AND);
}
else
error("both operands must be of real data type");
return retval;
}
DEFUN_DLD (bitor, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{A} =} bitor (@var{x}, @var{y})\n\
calculates the bitwise OR of nonnegative integers.\n\
@var{x},@var{y} must be in range [0..bitmax]\n\
@seealso{bitor,bitxor,bitset,bitget,bitcmp,bitshift,bitmax}\n\
@end deftypefn")
{
octave_value_list retval;
int nargin = args.length();
if (!(nargin==2)) {
print_usage ("bitor");
return retval;
}
if (args(0).is_real_type()&&args(1).is_real_type()) {
Matrix x = args(0).matrix_value();
Matrix y = args(1).matrix_value();
retval=bitop(x,y,BIT_OR);
}
else
error("both operands must be of real data type");
return retval;
}
DEFUN_DLD (bitxor, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{A} =} bitxor (@var{x}, @var{y})\n\
calculates the bitwise XOR of nonnegative integers.\n\
@var{x},@var{y} must be in range [0..bitmax]\n\
@seealso{bitand,bitor,bitset,bitget,bitcmp,bitshift,bitmax}\n\
@end deftypefn")
{
octave_value_list retval;
int nargin = args.length();
if (!(nargin==2)) {
print_usage ("bitxor");
return retval;
}
if (args(0).is_real_type()&&args(1).is_real_type()) {
Matrix x = args(0).matrix_value();
Matrix y = args(1).matrix_value();
retval=bitop(x,y,BIT_XOR);
}
else
error("both operands must be of real data type");
return retval;
}
DEFUN_DLD (bitmax, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {@var{A} =} bitmax\n\
returns the the maximum unsigned integer.
@seealso{bitand,bitor,bitxor,bitset,bitget,bitcmp,bitshift}\n\
@end deftypefn")
{
octave_value_list retval;
if (args.length()!=0)
print_usage ("bitmax");
else
retval(0)=octave_value(static_cast<double>(ULONG_MAX));
return retval;
}