changelog shortlog tags changeset files revisions annotate raw

src/bitfcns.cc

changeset 10289: 4b124317dc38
parent:357cff83985d
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (35 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
4
5This file is part of Octave.
6
7Octave is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12Octave is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with Octave; see the file COPYING. If not, see
19<http://www.gnu.org/licenses/>.
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#include "ov-uint32.h"
35#include "ov-uint16.h"
36#include "ov-uint8.h"
37#include "ov-int64.h"
38#include "ov-int32.h"
39#include "ov-int16.h"
40#include "ov-int8.h"
41#include "ov-scalar.h"
42#include "ov-re-mat.h"
43#include "ov-bool.h"
44
45// FIXME -- could probably eliminate some code duplication by
46// clever use of templates.
47
48#define BITOPX(OP, FNAME, RET) \
49 { \
50 int nelx = x.numel (); \
51 int nely = y.numel (); \
52 \
53 bool is_scalar_op = (nelx == 1 || nely == 1); \
54 \
55 dim_vector dvx = x.dims (); \
56 dim_vector dvy = y.dims (); \
57 \
58 bool is_array_op = (dvx == dvy); \
59 \
60 if (is_array_op || is_scalar_op) \
61 { \
62 RET result; \
63 \
64 if (nelx != 1) \
65 result.resize (dvx); \
66 else \
67 result.resize (dvy); \
68 \
69 for (int i = 0; i < nelx; i++) \
70 if (is_scalar_op) \
71 for (int k = 0; k < nely; k++) \
72 result(i+k) = x(i) OP y(k); \
73 else \
74 result(i) = x(i) OP y(i); \
75 \
76 retval = result; \
77 } \
78 else \
79 error ("%s: size of x and y must match, or one operand must be a scalar", FNAME); \
80 }
81
82#define BITOP(OP, FNAME) \
83 \
84 octave_value retval; \
85 \
86 int nargin = args.length (); \
87 \
88 if (nargin == 2) \
89 { \
90 if ((args(0).class_name () == octave_scalar::static_class_name ()) \
91 || (args(0).class_name () == octave_bool::static_class_name ()) \
92 || (args(1).class_name () == octave_scalar::static_class_name ()) \
93 || (args(1).class_name () == octave_bool::static_class_name ())) \
94 { \
95 bool arg0_is_int = (args(0).class_name () != \
96 octave_scalar::static_class_name () && \
97 args(0).class_name () != \
98 octave_bool::static_class_name ()); \
99 bool arg1_is_int = (args(1).class_name () != \
100 octave_scalar::static_class_name () && \
101 args(1).class_name () != \
102 octave_bool::static_class_name ()); \
103 \
104 if (! (arg0_is_int || arg1_is_int)) \
105 { \
106 uint64NDArray x (args(0).array_value ()); \
107 uint64NDArray y (args(1).array_value ()); \
108 if (! error_state) \
109 BITOPX (OP, FNAME, uint64NDArray); \
110 retval = retval.array_value (); \
111 } \
112 else \
113 { \
114 int p = (arg0_is_int ? 1 : 0); \
115 int q = (arg0_is_int ? 0 : 1); \
116 \
117 NDArray dx = args(p).array_value (); \
118 \
119 if (args(q).type_id () == octave_uint64_matrix::static_type_id () \
120 || args(q).type_id () == octave_uint64_scalar::static_type_id ()) \
121 { \
122 uint64NDArray x (dx); \
123 uint64NDArray y = args(q).uint64_array_value (); \
124 if (! error_state) \
125 BITOPX (OP, FNAME, uint64NDArray); \
126 } \
127 else if (args(q).type_id () == octave_uint32_matrix::static_type_id () \
128 || args(q).type_id () == octave_uint32_scalar::static_type_id ()) \
129 { \
130 uint32NDArray x (dx); \
131 uint32NDArray y = args(q).uint32_array_value (); \
132 if (! error_state) \
133 BITOPX (OP, FNAME, uint32NDArray); \
134 } \
135 else if (args(q).type_id () == octave_uint16_matrix::static_type_id () \
136 || args(q).type_id () == octave_uint16_scalar::static_type_id ()) \
137 { \
138 uint16NDArray x (dx); \
139 uint16NDArray y = args(q).uint16_array_value (); \
140 if (! error_state) \
141 BITOPX (OP, FNAME, uint16NDArray); \
142 } \
143 else if (args(q).type_id () == octave_uint8_matrix::static_type_id () \
144 || args(q).type_id () == octave_uint8_scalar::static_type_id ()) \
145 { \
146 uint8NDArray x (dx); \
147 uint8NDArray y = args(q).uint8_array_value (); \
148 if (! error_state) \
149 BITOPX (OP, FNAME, uint8NDArray); \
150 } \
151 else if (args(q).type_id () == octave_int64_matrix::static_type_id () \
152 || args(q).type_id () == octave_int64_scalar::static_type_id ()) \
153 { \
154 int64NDArray x (dx); \
155 int64NDArray y = args(q).int64_array_value (); \
156 if (! error_state) \
157 BITOPX (OP, FNAME, int64NDArray); \
158 } \
159 else if (args(q).type_id () == octave_int32_matrix::static_type_id () \
160 || args(q).type_id () == octave_int32_scalar::static_type_id ()) \
161 { \
162 int32NDArray x (dx); \
163 int32NDArray y = args(q).int32_array_value (); \
164 if (! error_state) \
165 BITOPX (OP, FNAME, int32NDArray); \
166 } \
167 else if (args(q).type_id () == octave_int16_matrix::static_type_id () \
168 || args(q).type_id () == octave_int16_scalar::static_type_id ()) \
169 { \
170 int16NDArray x (dx); \
171 int16NDArray y = args(q).int16_array_value (); \
172 if (! error_state) \
173 BITOPX (OP, FNAME, int16NDArray); \
174 } \
175 else if (args(q).type_id () == octave_int8_matrix::static_type_id () \
176 || args(q).type_id () == octave_int8_scalar::static_type_id ()) \
177 { \
178 int8NDArray x (dx); \
179 int8NDArray y = args(q).int8_array_value (); \
180 if (! error_state) \
181 BITOPX (OP, FNAME, int8NDArray); \
182 } \
183 else \
184 error ("%s: invalid operand type", FNAME); \
185 } \
186 } \
187 else if (args(0).class_name () == args(1).class_name ()) \
188 { \
189 if (args(0).type_id () == octave_uint64_matrix::static_type_id () \
190 || args(0).type_id () == octave_uint64_scalar::static_type_id ()) \
191 { \
192 uint64NDArray x = args(0).uint64_array_value (); \
193 uint64NDArray y = args(1).uint64_array_value (); \
194 if (! error_state) \
195 BITOPX (OP, FNAME, uint64NDArray); \
196 } \
197 else if (args(0).type_id () == octave_uint32_matrix::static_type_id () \
198 || args(0).type_id () == octave_uint32_scalar::static_type_id ()) \
199 { \
200 uint32NDArray x = args(0).uint32_array_value (); \
201 uint32NDArray y = args(1).uint32_array_value (); \
202 if (! error_state) \
203 BITOPX (OP, FNAME, uint32NDArray); \
204 } \
205 else if (args(0).type_id () == octave_uint16_matrix::static_type_id () \
206 || args(0).type_id () == octave_uint16_scalar::static_type_id ()) \
207 { \
208 uint16NDArray x = args(0).uint16_array_value (); \
209 uint16NDArray y = args(1).uint16_array_value (); \
210 if (! error_state) \
211 BITOPX (OP, FNAME, uint16NDArray); \
212 } \
213 else if (args(0).type_id () == octave_uint8_matrix::static_type_id () \
214 || args(0).type_id () == octave_uint8_scalar::static_type_id ()) \
215 { \
216 uint8NDArray x = args(0).uint8_array_value (); \
217 uint8NDArray y = args(1).uint8_array_value (); \
218 if (! error_state) \
219 BITOPX (OP, FNAME, uint8NDArray); \
220 } \
221 else if (args(0).type_id () == octave_int64_matrix::static_type_id () \
222 || args(0).type_id () == octave_int64_scalar::static_type_id ()) \
223 { \
224 int64NDArray x = args(0).int64_array_value (); \
225 int64NDArray y = args(1).int64_array_value (); \
226 if (! error_state) \
227 BITOPX (OP, FNAME, int64NDArray); \
228 } \
229 else if (args(0).type_id () == octave_int32_matrix::static_type_id () \
230 || args(0).type_id () == octave_int32_scalar::static_type_id ()) \
231 { \
232 int32NDArray x = args(0).int32_array_value (); \
233 int32NDArray y = args(1).int32_array_value (); \
234 if (! error_state) \
235 BITOPX (OP, FNAME, int32NDArray); \
236 } \
237 else if (args(0).type_id () == octave_int16_matrix::static_type_id () \
238 || args(0).type_id () == octave_int16_scalar::static_type_id ()) \
239 { \
240 int16NDArray x = args(0).int16_array_value (); \
241 int16NDArray y = args(1).int16_array_value (); \
242 if (! error_state) \
243 BITOPX (OP, FNAME, int16NDArray); \
244 } \
245 else if (args(0).type_id () == octave_int8_matrix::static_type_id () \
246 || args(0).type_id () == octave_int8_scalar::static_type_id ()) \
247 { \
248 int8NDArray x = args(0).int8_array_value (); \
249 int8NDArray y = args(1).int8_array_value (); \
250 if (! error_state) \
251 BITOPX (OP, FNAME, int8NDArray); \
252 } \
253 else \
254 error ("%s: invalid operand type", FNAME); \
255 } \
256 else \
257 error ("%s: must have matching operand types", FNAME); \
258 } \
259 else \
260 print_usage (); \
261 \
262 return retval
263
264DEFUN (bitand, args, ,
265 "-*- texinfo -*-\n\
266@deftypefn {Built-in Function} {} bitand (@var{x}, @var{y})\n\
267Return the bitwise AND of non-negative integers.\n\
268@var{x}, @var{y} must be in the range [0,bitmax]\n\
269@seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
270@end deftypefn")
271{
272 BITOP (&, "bitand");
273}
274
275DEFUN (bitor, args, ,
276 "-*- texinfo -*-\n\
277@deftypefn {Built-in Function} {} bitor (@var{x}, @var{y})\n\
278Return the bitwise OR of non-negative integers.\n\
279@var{x}, @var{y} must be in the range [0,bitmax]\n\
280@seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
281@end deftypefn")
282{
283 BITOP (|, "bitor");
284}
285
286DEFUN (bitxor, args, ,
287 "-*- texinfo -*-\n\
288@deftypefn {Built-in Function} {} bitxor (@var{x}, @var{y})\n\
289Return the bitwise XOR of non-negative integers.\n\
290@var{x}, @var{y} must be in the range [0,bitmax]\n\
291@seealso{bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
292@end deftypefn")
293{
294 BITOP (^, "bitxor");
295}
296
297static int64_t
298bitshift (double a, int n, int64_t mask)
299{
300 // In the name of bug-for-bug compatibility.
301 if (a < 0)
302 return -bitshift (-a, n, mask);
303
304 if (n > 0)
305 return (static_cast<int64_t> (a) << n) & mask;
306 else if (n < 0)
307 return (static_cast<int64_t> (a) >> -n) & mask;
308 else
309 return static_cast<int64_t> (a) & mask;
310}
311
312static int64_t
313bitshift (float a, int n, int64_t mask)
314{
315 // In the name of bug-for-bug compatibility.
316 if (a < 0)
317 return -bitshift (-a, n, mask);
318
319 if (n > 0)
320 return (static_cast<int64_t> (a) << n) & mask;
321 else if (n < 0)
322 return (static_cast<int64_t> (a) >> -n) & mask;
323 else
324 return static_cast<int64_t> (a) & mask;
325}
326
327// Note that the bitshift operators are undefined if shifted by more
328// bits than in the type, so we need to test for the size of the
329// shift.
330
331#define DO_BITSHIFT(T) \
332 if (! error_state) \
333 { \
334 double d1, d2; \
335 \
336 if (n.all_integers (d1, d2)) \
337 { \
338 int m_nel = m.numel (); \
339 int n_nel = n.numel (); \
340 \
341 bool is_scalar_op = (m_nel == 1 || n_nel == 1); \
342 \
343 dim_vector m_dv = m.dims (); \
344 dim_vector n_dv = n.dims (); \
345 \
346 bool is_array_op = (m_dv == n_dv); \
347 \
348 if (is_array_op || is_scalar_op) \
349 { \
350 T ## NDArray result; \
351 \
352 if (m_nel != 1) \
353 result.resize (m_dv); \
354 else \
355 result.resize (n_dv); \
356 \
357 for (int i = 0; i < m_nel; i++) \
358 if (is_scalar_op) \
359 for (int k = 0; k < n_nel; k++) \
360 if (static_cast<int> (n(k)) >= bits_in_type) \
361 result(i+k) = 0; \
362 else \
363 result(i+k) = bitshift (m(i), static_cast<int> (n(k)), mask); \
364 else \
365 if (static_cast<int> (n(i)) >= bits_in_type) \
366 result(i) = 0; \
367 else \
368 result(i) = bitshift (m(i), static_cast<int> (n(i)), mask); \
369 \
370 retval = result; \
371 } \
372 else \
373 error ("bitshift: size of A and N must match, or one operand must be a scalar"); \
374 } \
375 else \
376 error ("bitshift: expecting second argument to be integer"); \
377 }
378
379#define DO_UBITSHIFT(T, N) \
380 do \
381 { \
382 int bits_in_type = octave_ ## T :: nbits (); \
383 T ## NDArray m = m_arg.T ## _array_value (); \
384 octave_ ## T mask = octave_ ## T::max (); \
385 if ((N) < bits_in_type) \
386 mask = bitshift (mask, (N) - bits_in_type); \
387 else if ((N) < 1) \
388 mask = 0; \
389 DO_BITSHIFT (T); \
390 } \
391 while (0)
392
393#define DO_SBITSHIFT(T, N) \
394 do \
395 { \
396 int bits_in_type = octave_ ## T :: nbits (); \
397 T ## NDArray m = m_arg.T ## _array_value (); \
398 octave_ ## T mask = octave_ ## T::max (); \
399 if ((N) < bits_in_type) \
400 mask = bitshift (mask, (N) - bits_in_type); \
401 else if ((N) < 1) \
402 mask = 0; \
403 mask = mask | octave_ ## T :: min (); /* FIXME: 2's complement only? */ \
404 DO_BITSHIFT (T); \
405 } \
406 while (0)
407
408DEFUN (bitshift, args, ,
409 "-*- texinfo -*-\n\
410@deftypefn {Built-in Function} {} bitshift (@var{a}, @var{k})\n\
411@deftypefnx {Built-in Function} {} bitshift (@var{a}, @var{k}, @var{n})\n\
412Return a @var{k} bit shift of @var{n}-digit unsigned\n\
413integers in @var{a}. A positive @var{k} leads to a left shift.\n\
414A negative value to a right shift. If @var{n} is omitted it defaults\n\
415to log2(bitmax)+1.\n\
416@var{n} must be in the range [1,log2(bitmax)+1] usually [1,33]\n\
417\n\
418@example\n\
419@group\n\
420bitshift (eye (3), 1)\n\
421@result{}\n\
422@group\n\
4232 0 0\n\
4240 2 0\n\
4250 0 2\n\
426@end group\n\
427\n\
428bitshift (10, [-2, -1, 0, 1, 2])\n\
429@result{} 2 5 10 20 40\n\
430@c FIXME -- restore this example when third arg is allowed to be an array.\n\
431@c \n\
432@c \n\
433@c bitshift ([1, 10], 2, [3,4])\n\
434@c @result{} 4 8\n\
435@end group\n\
436@end example\n\
437@seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}\n\
438@end deftypefn")
439{
440 octave_value retval;
441
442 int nargin = args.length ();
443
444 if (nargin == 2 || nargin == 3)
445 {
446 int nbits = 64;
447
448 NDArray n = args(1).array_value ();
449
450 if (error_state)
451 error ("bitshift: expecting integer as second argument");
452 else
453 {
454 if (nargin == 3)
455 {
456 // FIXME -- for compatibility, we should accept an array
457 // or a scalar as the third argument.
458 if (args(2).numel () > 1)
459 error ("bitshift: expecting scalar integer as third argument");
460 else
461 {
462 nbits = args(2).int_value ();
463
464 if (error_state)
465 error ("bitshift: expecting integer as third argument");
466 else if (nbits < 0)
467 error ("bitshift: number of bits to mask must be positive");
468 }
469 }
470 }
471
472 if (error_state)
473 return retval;
474
475 octave_value m_arg = args(0);
476 std::string cname = m_arg.class_name ();
477
478 if (cname == "uint8")
479 DO_UBITSHIFT (uint8, nbits < 8 ? nbits : 8);
480 else if (cname == "uint16")
481 DO_UBITSHIFT (uint16, nbits < 16 ? nbits : 16);
482 else if (cname == "uint32")
483 DO_UBITSHIFT (uint32, nbits < 32 ? nbits : 32);
484 else if (cname == "uint64")
485 DO_UBITSHIFT (uint64, nbits < 64 ? nbits : 64);
486 else if (cname == "int8")
487 DO_SBITSHIFT (int8, nbits < 8 ? nbits : 8);
488 else if (cname == "int16")
489 DO_SBITSHIFT (int16, nbits < 16 ? nbits : 16);
490 else if (cname == "int32")
491 DO_SBITSHIFT (int32, nbits < 32 ? nbits : 32);
492 else if (cname == "int64")
493 DO_SBITSHIFT (int64, nbits < 64 ? nbits : 64);
494 else if (cname == "double")
495 {
496 nbits = (nbits < 53 ? nbits : 53);
497 int64_t mask = 0x1FFFFFFFFFFFFFLL;
498 if (nbits < 53)
499 mask = mask >> (53 - nbits);
500 else if (nbits < 1)
501 mask = 0;
502 int bits_in_type = 64;
503 NDArray m = m_arg.array_value ();
504 DO_BITSHIFT ( );
505 }
506 else
507 error ("bitshift: not defined for %s objects", cname.c_str ());
508 }
509 else
510 print_usage ();
511
512 return retval;
513}
514
515DEFUN (bitmax, args, ,
516 "-*- texinfo -*-\n\
517@deftypefn {Built-in Function} {} bitmax ()\n\
518Return the largest integer that can be represented as a floating point\n\
519value. On IEEE-754 compatible systems, @code{bitmax} is @code{2^53 - 1}.\n\
520@end deftypefn")
521{
522 octave_value retval;
523 if (args.length () != 0)
524 print_usage ();
525 else
526 retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL));
527 return retval;
528}
529
530DEFUN (intmax, args, ,
531 "-*- texinfo -*-\n\
532@deftypefn {Built-in Function} {} intmax (@var{type})\n\
533Return the largest integer that can be represented in an integer type.\n\
534The variable @var{type} can be\n\
535\n\
536@table @code\n\
537@item int8\n\
538signed 8-bit integer.\n\
539@item int16\n\
540signed 16-bit integer.\n\
541@item int32\n\
542signed 32-bit integer.\n\
543@item int64\n\
544signed 64-bit integer.\n\
545@item uint8\n\
546unsigned 8-bit integer.\n\
547@item uint16\n\
548unsigned 16-bit integer.\n\
549@item uint32\n\
550unsigned 32-bit integer.\n\
551@item uint64\n\
552unsigned 64-bit integer.\n\
553@end table\n\
554\n\
555The default for @var{type} is @code{uint32}.\n\
556@seealso{intmin, bitmax}\n\
557@end deftypefn")
558{
559 octave_value retval;
560 std::string cname = "int32";
561 int nargin = args.length ();
562
563 if (nargin == 1 && args(0).is_string ())
564 cname = args(0).string_value ();
565 else if (nargin != 0)
566 {
567 print_usage ();
568 return retval;
569 }
570
571 if (cname == "uint8")
572 retval = octave_uint8 (std::numeric_limits<uint8_t>::max ());
573 else if (cname == "uint16")
574 retval = octave_uint16 (std::numeric_limits<uint16_t>::max ());
575 else if (cname == "uint32")
576 retval = octave_uint32 (std::numeric_limits<uint32_t>::max ());
577 else if (cname == "uint64")
578 retval = octave_uint64 (std::numeric_limits<uint64_t>::max ());
579 else if (cname == "int8")
580 retval = octave_int8 (std::numeric_limits<int8_t>::max ());
581 else if (cname == "int16")
582 retval = octave_int16 (std::numeric_limits<int16_t>::max ());
583 else if (cname == "int32")
584 retval = octave_int32 (std::numeric_limits<int32_t>::max ());
585 else if (cname == "int64")
586 retval = octave_int64 (std::numeric_limits<int64_t>::max ());
587 else
588 error ("intmax: not defined for '%s' objects", cname.c_str ());
589
590 return retval;
591}
592
593DEFUN (intmin, args, ,
594 "-*- texinfo -*-\n\
595@deftypefn {Built-in Function} {} intmin (@var{type})\n\
596Return the smallest integer that can be represented in an integer type.\n\
597The variable @var{type} can be\n\
598\n\
599@table @code\n\
600@item int8\n\
601signed 8-bit integer.\n\
602@item int16\n\
603signed 16-bit integer.\n\
604@item int32\n\
605signed 32-bit integer.\n\
606@item int64\n\
607signed 64-bit integer.\n\
608@item uint8\n\
609unsigned 8-bit integer.\n\
610@item uint16\n\
611unsigned 16-bit integer.\n\
612@item uint32\n\
613unsigned 32-bit integer.\n\
614@item uint64\n\
615unsigned 64-bit integer.\n\
616@end table\n\
617\n\
618The default for @var{type} is @code{uint32}.\n\
619@seealso{intmax, bitmax}\n\
620@end deftypefn")
621{
622 octave_value retval;
623 std::string cname = "int32";
624 int nargin = args.length ();
625
626 if (nargin == 1 && args(0).is_string ())
627 cname = args(0).string_value ();
628 else if (nargin != 0)
629 {
630 print_usage ();
631 return retval;
632 }
633
634 if (cname == "uint8")
635 retval = octave_uint8 (std::numeric_limits<uint8_t>::min ());
636 else if (cname == "uint16")
637 retval = octave_uint16 (std::numeric_limits<uint16_t>::min());
638 else if (cname == "uint32")
639 retval = octave_uint32 (std::numeric_limits<uint32_t>::min ());
640 else if (cname == "uint64")
641 retval = octave_uint64 (std::numeric_limits<uint64_t>::min ());
642 else if (cname == "int8")
643 retval = octave_int8 (std::numeric_limits<int8_t>::min ());
644 else if (cname == "int16")
645 retval = octave_int16 (std::numeric_limits<int16_t>::min ());
646 else if (cname == "int32")
647 retval = octave_int32 (std::numeric_limits<int32_t>::min ());
648 else if (cname == "int64")
649 retval = octave_int64 (std::numeric_limits<int64_t>::min ());
650 else
651 error ("intmin: not defined for '%s' objects", cname.c_str ());
652
653 return retval;
654}