4908
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "str-vec.h" |
|
28 #include "quit.h" |
|
29 |
|
30 #include "defun.h" |
|
31 #include "error.h" |
|
32 #include "ov.h" |
|
33 #include "ov-uint64.h" |
|
34 |
|
35 |
|
36 // XXX FIXME XXX -- could probably eliminate some code duplication by |
|
37 // clever use of templates. |
|
38 |
|
39 #define BITOP(OP, FNAME) \ |
|
40 \ |
|
41 octave_value retval; \ |
|
42 \ |
|
43 int nargin = args.length (); \ |
|
44 \ |
|
45 if (nargin == 2) \ |
|
46 { \ |
|
47 uint64NDArray x = args(0).uint64_array_value (); \ |
|
48 uint64NDArray y = args(1).uint64_array_value (); \ |
|
49 \ |
|
50 if (! error_state) \ |
|
51 { \ |
|
52 int nelx = x.numel (); \ |
|
53 int nely = y.numel (); \ |
|
54 \ |
|
55 bool is_scalar_op = (nelx == 1 || nely == 1); \ |
|
56 \ |
|
57 dim_vector dvx = x.dims (); \ |
|
58 dim_vector dvy = y.dims (); \ |
|
59 \ |
|
60 bool is_array_op = (dvx == dvy); \ |
|
61 \ |
|
62 if (is_array_op || is_scalar_op) \ |
|
63 { \ |
|
64 uint64NDArray result; \ |
|
65 \ |
|
66 if (nelx != 1) \ |
|
67 result.resize (dvx); \ |
|
68 else \ |
|
69 result.resize (dvy); \ |
|
70 \ |
|
71 for (int i = 0; i < nelx; i++) \ |
|
72 if (is_scalar_op) \ |
|
73 for (int k = 0; k < nely; k++) \ |
|
74 result(i+k) = x(i) OP y(k); \ |
|
75 else \ |
|
76 result(i) = x(i) OP y(i); \ |
|
77 \ |
|
78 retval = result; \ |
|
79 } \ |
|
80 else \ |
|
81 error ("%s: size of x and y must match, or one operand must be a scalar", FNAME); \ |
|
82 } \ |
|
83 else \ |
|
84 error ("%s: expecting uint64 arguments", FNAME); \ |
|
85 } \ |
|
86 else \ |
|
87 print_usage (FNAME); \ |
|
88 \ |
|
89 return retval |
|
90 |
|
91 DEFUN (bitand, args, , |
|
92 "-*- texinfo -*-\n\ |
|
93 @deftypefn {Built-in Function} {} bitand (@var{x}, @var{y})\n\ |
|
94 calculates the bitwise AND of nonnegative integers.\n\ |
|
95 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
96 @end deftypefn\n\ |
|
97 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
98 { |
|
99 BITOP (&, "bitand"); |
|
100 } |
|
101 |
|
102 DEFUN (bitor, args, , |
|
103 "-*- texinfo -*-\n\ |
|
104 @deftypefn {Built-in Function} {} bitor (@var{x}, @var{y})\n\ |
|
105 calculates the bitwise OR of nonnegative integers.\n\ |
|
106 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
107 @end deftypefn\n\ |
|
108 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
109 { |
|
110 BITOP (|, "bitor"); |
|
111 } |
|
112 |
|
113 DEFUN (bitxor, args, , |
|
114 "-*- texinfo -*-\n\ |
|
115 @deftypefn {Built-in Function} {} bitxor (@var{x}, @var{y})\n\ |
|
116 calculates the bitwise XOR of nonnegative integers.\n\ |
|
117 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
118 @end deftypefn\n\ |
|
119 @seealso{bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
120 { |
|
121 BITOP (^, "bitxor"); |
|
122 } |
|
123 |
|
124 DEFUN (bitcmp, args, , |
|
125 "-*- texinfo -*-\n\ |
|
126 @deftypefn {Built-in Function} {} bitcmp (@var{a}, @var{k})\n\ |
|
127 returns the @var{k}-bit complement of integers in @var{a}. If\n\ |
|
128 @var{k} is omitted k = log2(bitmax) is assumed.\n\ |
|
129 \n\ |
|
130 @example\n\ |
|
131 bitcmp (7, 4)\n\ |
|
132 @result{} 8\n\ |
|
133 dec2bin (11)\n\ |
|
134 @result{} 1011\n\ |
|
135 dec2bin (bitcmp (11))\n\ |
|
136 @result{} 11111111111111111111111111110100\n\ |
|
137 @end example\n\ |
|
138 \n\ |
|
139 @end deftypefn\n\ |
|
140 @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
141 { |
|
142 octave_value retval; |
|
143 error ("not implemented"); |
|
144 return retval; |
|
145 } |
|
146 |
|
147 DEFUN (bitget, args, , |
|
148 "-*- texinfo -*-\n\ |
|
149 @deftypefn {Function File} {} bitget (@var{a}, @var{n})\n\ |
|
150 returns the status of bit(s) @var{n} of unsigned integers in @var{a}\n\ |
|
151 the lowest significant bit is @var{n} = 1.\n\ |
|
152 \n\ |
|
153 @example\n\ |
|
154 bitget (100,8:-1:1)\n\ |
|
155 @result{} 0 1 1 0 0 1 0 0\n\ |
|
156 @end example\n\ |
|
157 @end deftypefn\n\ |
|
158 @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax}") |
|
159 { |
|
160 octave_value retval; |
|
161 error ("not implemented"); |
|
162 return retval; |
|
163 } |
|
164 |
|
165 DEFUN (bitset, args, , |
|
166 "-*- texinfo -*-\n\ |
|
167 @deftypefn {Function File} {} bitset (@var{a}, @var{n})\n\ |
|
168 @deftypefnx {Function File} {} bitset (@var{a}, @var{n}, @var{v})\n\ |
|
169 sets or resets bit(s) @var{n} of unsigned integers in @var{a}.\n\ |
|
170 @var{v} = 0 resets and @var{v} = 1 sets the bits.\n\ |
|
171 The lowest significant bit is: @var{n} = 1\n\ |
|
172 \n\ |
|
173 @example\n\ |
|
174 dec2bin (bitset (10, 1))\n\ |
|
175 @result{} 1011\n\ |
|
176 @end example\n\ |
|
177 \n\ |
|
178 @end deftypefn\n\ |
|
179 @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax}") |
|
180 { |
|
181 octave_value retval; |
|
182 error ("not implemented"); |
|
183 return retval; |
|
184 } |
|
185 |
|
186 #define DO_BITSHIFT(T) \ |
|
187 do \ |
|
188 { \ |
|
189 T ## NDArray m = m_arg.T ## _array_value (); \ |
|
190 \ |
|
191 if (! error_state) \ |
|
192 { \ |
|
193 double d1, d2; \ |
|
194 \ |
|
195 if (n.all_integers (d1, d2)) \ |
|
196 { \ |
|
197 int m_nel = m.numel (); \ |
|
198 int n_nel = n.numel (); \ |
|
199 \ |
|
200 bool is_scalar_op = (m_nel == 1 || n_nel == 1); \ |
|
201 \ |
|
202 dim_vector m_dv = m.dims (); \ |
|
203 dim_vector n_dv = n.dims (); \ |
|
204 \ |
|
205 bool is_array_op = (m_dv == n_dv); \ |
|
206 \ |
|
207 if (is_array_op || is_scalar_op) \ |
|
208 { \ |
|
209 T ## NDArray result; \ |
|
210 \ |
|
211 if (m_nel != 1) \ |
|
212 result.resize (m_dv); \ |
|
213 else \ |
|
214 result.resize (n_dv); \ |
|
215 \ |
|
216 for (int i = 0; i < m_nel; i++) \ |
|
217 if (is_scalar_op) \ |
|
218 for (int k = 0; k < n_nel; k++) \ |
|
219 result(i+k) = bitshift (m(i), static_cast<int> (n(k))); \ |
|
220 else \ |
|
221 result(i) = bitshift (m(i), static_cast<int> (n(i))); \ |
|
222 \ |
|
223 retval = result; \ |
|
224 } \ |
|
225 else \ |
|
226 error ("bitshift: size of A and N must match, or one operand must be a scalar"); \ |
|
227 } \ |
|
228 else \ |
|
229 error ("bitshift: expecting second argument to be integer"); \ |
|
230 } \ |
|
231 } \ |
|
232 while (0) |
|
233 |
|
234 DEFUN (bitshift, args, , |
|
235 "-*- texinfo -*-\n\ |
|
236 @deftypefn {Function File} {} bitshift (@var{a}, @var{k})\n\ |
|
237 @deftypefnx {Function File} {} bitshift (@var{a}, @var{k}, @var{n})\n\ |
|
238 return a @var{k} bit shift of @var{n}- digit unsigned\n\ |
|
239 integers in @var{a}. A positive @var{k} leads to a left shift.\n\ |
|
240 A negative value to a right shift. If @var{N} is omitted it defaults\n\ |
|
241 to log2(bitmax)+1. \n\ |
|
242 @var{N} must be in range [1,log2(bitmax)+1] usually [1,33]\n\ |
|
243 \n\ |
|
244 @example\n\ |
|
245 bitshift (eye (3), 1))\n\ |
|
246 @result{}\n\ |
|
247 @group\n\ |
|
248 2 0 0\n\ |
|
249 0 2 0\n\ |
|
250 0 0 2\n\ |
|
251 @end group\n\ |
|
252 \n\ |
|
253 bitshift (10, [-2, -1, 0, 1, 2])\n\ |
|
254 @result{} 2 5 10 20 40\n\ |
|
255 \n\ |
|
256 bitshift ([1, 10], 2, [3,4])\n\ |
|
257 @result{} 4 8\n\ |
|
258 @end example\n\ |
|
259 @end deftypefn\n\ |
|
260 @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}") |
|
261 { |
|
262 octave_value retval; |
|
263 |
|
264 int nargin = args.length (); |
|
265 |
|
266 if (nargin == 2) |
|
267 { |
|
268 NDArray n = args(1).array_value (); |
|
269 |
|
270 octave_value m_arg = args(0); |
|
271 |
|
272 std::string cname = m_arg.class_name (); |
|
273 |
|
274 if (cname == "uint8") |
|
275 DO_BITSHIFT (uint8); |
|
276 else if (cname == "uint16") |
|
277 DO_BITSHIFT (uint16); |
|
278 else if (cname == "uint32") |
|
279 DO_BITSHIFT (uint32); |
|
280 else if (cname == "uint64") |
|
281 DO_BITSHIFT (uint64); |
|
282 else |
|
283 error ("bitshift: not defined for %s objects", cname.c_str ()); |
|
284 } |
|
285 else |
|
286 print_usage ("bitshift"); |
|
287 |
|
288 return retval; |
|
289 } |
|
290 |
|
291 DEFUN (bitmax, args, , |
|
292 "-*- texinfo -*-\n\ |
|
293 @deftypefn {Built-in Function} {} bitmax (@var{x}, @var{y})\n\ |
|
294 @end deftypefn") |
|
295 { |
|
296 octave_value retval; |
|
297 error ("not implemented"); |
|
298 return retval; |
|
299 } |
|
300 |
|
301 /* |
|
302 ;;; Local Variables: *** |
|
303 ;;; mode: C++ *** |
|
304 ;;; End: *** |
|
305 */ |