3Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
5This file is part of Octave.
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.
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
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/>.
45// FIXME -- could probably eliminate some code duplication by
46// clever use of templates.
48#define BITOPX(OP, FNAME, RET) \
50 int nelx = x.numel (); \
51 int nely = y.numel (); \
53 bool is_scalar_op = (nelx == 1 || nely == 1); \
55 dim_vector dvx = x.dims (); \
56 dim_vector dvy = y.dims (); \
58 bool is_array_op = (dvx == dvy); \
60 if (is_array_op || is_scalar_op) \
65 result.resize (dvx); \
67 result.resize (dvy); \
69 for (int i = 0; i < nelx; i++) \
71 for (int k = 0; k < nely; k++) \
72 result(i+k) = x(i) OP y(k); \
74 result(i) = x(i) OP y(i); \
79 error ("%s: size of x and y must match, or one operand must be a scalar", FNAME); \
82#define BITOP(OP, FNAME) \
84 octave_value retval; \
86 int nargin = args.length (); \
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 ())) \
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 ()); \
104 if (! (arg0_is_int || arg1_is_int)) \
106 uint64NDArray x (args(0).array_value ()); \
107 uint64NDArray y (args(1).array_value ()); \
109 BITOPX (OP, FNAME, uint64NDArray); \
110 retval = retval.array_value (); \
114 int p = (arg0_is_int ? 1 : 0); \
115 int q = (arg0_is_int ? 0 : 1); \
117 NDArray dx = args(p).array_value (); \
119 if (args(q).type_id () == octave_uint64_matrix::static_type_id () \
120 || args(q).type_id () == octave_uint64_scalar::static_type_id ()) \
122 uint64NDArray x (dx); \
123 uint64NDArray y = args(q).uint64_array_value (); \
125 BITOPX (OP, FNAME, uint64NDArray); \
127 else if (args(q).type_id () == octave_uint32_matrix::static_type_id () \
128 || args(q).type_id () == octave_uint32_scalar::static_type_id ()) \
130 uint32NDArray x (dx); \
131 uint32NDArray y = args(q).uint32_array_value (); \
133 BITOPX (OP, FNAME, uint32NDArray); \
135 else if (args(q).type_id () == octave_uint16_matrix::static_type_id () \
136 || args(q).type_id () == octave_uint16_scalar::static_type_id ()) \
138 uint16NDArray x (dx); \
139 uint16NDArray y = args(q).uint16_array_value (); \
141 BITOPX (OP, FNAME, uint16NDArray); \
143 else if (args(q).type_id () == octave_uint8_matrix::static_type_id () \
144 || args(q).type_id () == octave_uint8_scalar::static_type_id ()) \
146 uint8NDArray x (dx); \
147 uint8NDArray y = args(q).uint8_array_value (); \
149 BITOPX (OP, FNAME, uint8NDArray); \
151 else if (args(q).type_id () == octave_int64_matrix::static_type_id () \
152 || args(q).type_id () == octave_int64_scalar::static_type_id ()) \
154 int64NDArray x (dx); \
155 int64NDArray y = args(q).int64_array_value (); \
157 BITOPX (OP, FNAME, int64NDArray); \
159 else if (args(q).type_id () == octave_int32_matrix::static_type_id () \
160 || args(q).type_id () == octave_int32_scalar::static_type_id ()) \
162 int32NDArray x (dx); \
163 int32NDArray y = args(q).int32_array_value (); \
165 BITOPX (OP, FNAME, int32NDArray); \
167 else if (args(q).type_id () == octave_int16_matrix::static_type_id () \
168 || args(q).type_id () == octave_int16_scalar::static_type_id ()) \
170 int16NDArray x (dx); \
171 int16NDArray y = args(q).int16_array_value (); \
173 BITOPX (OP, FNAME, int16NDArray); \
175 else if (args(q).type_id () == octave_int8_matrix::static_type_id () \
176 || args(q).type_id () == octave_int8_scalar::static_type_id ()) \
178 int8NDArray x (dx); \
179 int8NDArray y = args(q).int8_array_value (); \
181 BITOPX (OP, FNAME, int8NDArray); \
184 error ("%s: invalid operand type", FNAME); \
187 else if (args(0).class_name () == args(1).class_name ()) \
189 if (args(0).type_id () == octave_uint64_matrix::static_type_id () \
190 || args(0).type_id () == octave_uint64_scalar::static_type_id ()) \
192 uint64NDArray x = args(0).uint64_array_value (); \
193 uint64NDArray y = args(1).uint64_array_value (); \
195 BITOPX (OP, FNAME, uint64NDArray); \
197 else if (args(0).type_id () == octave_uint32_matrix::static_type_id () \
198 || args(0).type_id () == octave_uint32_scalar::static_type_id ()) \
200 uint32NDArray x = args(0).uint32_array_value (); \
201 uint32NDArray y = args(1).uint32_array_value (); \
203 BITOPX (OP, FNAME, uint32NDArray); \
205 else if (args(0).type_id () == octave_uint16_matrix::static_type_id () \
206 || args(0).type_id () == octave_uint16_scalar::static_type_id ()) \
208 uint16NDArray x = args(0).uint16_array_value (); \
209 uint16NDArray y = args(1).uint16_array_value (); \
211 BITOPX (OP, FNAME, uint16NDArray); \
213 else if (args(0).type_id () == octave_uint8_matrix::static_type_id () \
214 || args(0).type_id () == octave_uint8_scalar::static_type_id ()) \
216 uint8NDArray x = args(0).uint8_array_value (); \
217 uint8NDArray y = args(1).uint8_array_value (); \
219 BITOPX (OP, FNAME, uint8NDArray); \
221 else if (args(0).type_id () == octave_int64_matrix::static_type_id () \
222 || args(0).type_id () == octave_int64_scalar::static_type_id ()) \
224 int64NDArray x = args(0).int64_array_value (); \
225 int64NDArray y = args(1).int64_array_value (); \
227 BITOPX (OP, FNAME, int64NDArray); \
229 else if (args(0).type_id () == octave_int32_matrix::static_type_id () \
230 || args(0).type_id () == octave_int32_scalar::static_type_id ()) \
232 int32NDArray x = args(0).int32_array_value (); \
233 int32NDArray y = args(1).int32_array_value (); \
235 BITOPX (OP, FNAME, int32NDArray); \
237 else if (args(0).type_id () == octave_int16_matrix::static_type_id () \
238 || args(0).type_id () == octave_int16_scalar::static_type_id ()) \
240 int16NDArray x = args(0).int16_array_value (); \
241 int16NDArray y = args(1).int16_array_value (); \
243 BITOPX (OP, FNAME, int16NDArray); \
245 else if (args(0).type_id () == octave_int8_matrix::static_type_id () \
246 || args(0).type_id () == octave_int8_scalar::static_type_id ()) \
248 int8NDArray x = args(0).int8_array_value (); \
249 int8NDArray y = args(1).int8_array_value (); \
251 BITOPX (OP, FNAME, int8NDArray); \
254 error ("%s: invalid operand type", FNAME); \
257 error ("%s: must have matching operand types", FNAME); \
264DEFUN (bitand, args, ,
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\
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\
286DEFUN (bitxor, args, ,
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\
298bitshift (double a, int n, int64_t mask)
300 // In the name of bug-for-bug compatibility.
302 return -bitshift (-a, n, mask);
305 return (static_cast<int64_t> (a) << n) & mask;
307 return (static_cast<int64_t> (a) >> -n) & mask;
309 return static_cast<int64_t> (a) & mask;
313bitshift (float a, int n, int64_t mask)
315 // In the name of bug-for-bug compatibility.
317 return -bitshift (-a, n, mask);
320 return (static_cast<int64_t> (a) << n) & mask;
322 return (static_cast<int64_t> (a) >> -n) & mask;
324 return static_cast<int64_t> (a) & mask;
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
331#define DO_BITSHIFT(T) \
336 if (n.all_integers (d1, d2)) \
338 int m_nel = m.numel (); \
339 int n_nel = n.numel (); \
341 bool is_scalar_op = (m_nel == 1 || n_nel == 1); \
343 dim_vector m_dv = m.dims (); \
344 dim_vector n_dv = n.dims (); \
346 bool is_array_op = (m_dv == n_dv); \
348 if (is_array_op || is_scalar_op) \
350 T ## NDArray result; \
353 result.resize (m_dv); \
355 result.resize (n_dv); \
357 for (int i = 0; i < m_nel; i++) \
359 for (int k = 0; k < n_nel; k++) \
360 if (static_cast<int> (n(k)) >= bits_in_type) \
363 result(i+k) = bitshift (m(i), static_cast<int> (n(k)), mask); \
365 if (static_cast<int> (n(i)) >= bits_in_type) \
368 result(i) = bitshift (m(i), static_cast<int> (n(i)), mask); \
373 error ("bitshift: size of A and N must match, or one operand must be a scalar"); \
376 error ("bitshift: expecting second argument to be integer"); \
379#define DO_UBITSHIFT(T, N) \
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); \
393#define DO_SBITSHIFT(T, N) \
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); \
403 mask = mask | octave_ ## T :: min (); /* FIXME: 2's complement only? */ \
408DEFUN (bitshift, args, ,
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\
416@var{n} must be in the range [1,log2(bitmax)+1] usually [1,33]\n\
420bitshift (eye (3), 1)\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\
433@c bitshift ([1, 10], 2, [3,4])\n\
437@seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}\n\
442 int nargin = args.length ();
444 if (nargin == 2 || nargin == 3)
448 NDArray n = args(1).array_value ();
451 error ("bitshift: expecting integer as second argument");
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");
462 nbits = args(2).int_value ();
465 error ("bitshift: expecting integer as third argument");
467 error ("bitshift: number of bits to mask must be positive");
475 octave_value m_arg = args(0);
476 std::string cname = m_arg.class_name ();
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")
496 nbits = (nbits < 53 ? nbits : 53);
497 int64_t mask = 0x1FFFFFFFFFFFFFLL;
499 mask = mask >> (53 - nbits);
502 int bits_in_type = 64;
503 NDArray m = m_arg.array_value ();
507 error ("bitshift: not defined for %s objects", cname.c_str ());
515DEFUN (bitmax, args, ,
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\
523 if (args.length () != 0)
526 retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL));
530DEFUN (intmax, args, ,
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\
538signed 8-bit integer.\n\
540signed 16-bit integer.\n\
542signed 32-bit integer.\n\
544signed 64-bit integer.\n\
546unsigned 8-bit integer.\n\
548unsigned 16-bit integer.\n\
550unsigned 32-bit integer.\n\
552unsigned 64-bit integer.\n\
555The default for @var{type} is @code{uint32}.\n\
556@seealso{intmin, bitmax}\n\
560 std::string cname = "int32";
561 int nargin = args.length ();
563 if (nargin == 1 && args(0).is_string ())
564 cname = args(0).string_value ();
565 else if (nargin != 0)
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 ());
588 error ("intmax: not defined for '%s' objects", cname.c_str ());
593DEFUN (intmin, args, ,
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\
601signed 8-bit integer.\n\
603signed 16-bit integer.\n\
605signed 32-bit integer.\n\
607signed 64-bit integer.\n\
609unsigned 8-bit integer.\n\
611unsigned 16-bit integer.\n\
613unsigned 32-bit integer.\n\
615unsigned 64-bit integer.\n\
618The default for @var{type} is @code{uint32}.\n\
619@seealso{intmax, bitmax}\n\
623 std::string cname = "int32";
624 int nargin = args.length ();
626 if (nargin == 1 && args(0).is_string ())
627 cname = args(0).string_value ();
628 else if (nargin != 0)
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 ());
651 error ("intmin: not defined for '%s' objects", cname.c_str ());